From 388d00313267e957575299f4067f2f6b1f72be83 Mon Sep 17 00:00:00 2001 From: nhyatt Date: Mon, 12 Feb 2024 07:26:51 -0600 Subject: [PATCH] removes duplicate code --- internal/webserver/httpTemplate.go | 44 ++++++++++-------------------- 1 file changed, 15 insertions(+), 29 deletions(-) diff --git a/internal/webserver/httpTemplate.go b/internal/webserver/httpTemplate.go index eed9aec..495bcf9 100644 --- a/internal/webserver/httpTemplate.go +++ b/internal/webserver/httpTemplate.go @@ -16,7 +16,7 @@ import ( const ( ERR_PARSE_TEMPLATE = "Template Parse Error" - ERR_EXEC_TEMPLATE = "Template Execution Error" + ERR_EXEC_TEMPLATE = "Template Execution Error" ) type webErrStruct struct { @@ -57,31 +57,8 @@ func tmpltWebRoot(w http.ResponseWriter, r *http.Request) { return } - var ( - msgBuffer bytes.Buffer - cves []cisa.VulStruct - num int = 3 - ) - - c := cisa.Read() - for _, i := range c.Vulnerabilities { - t, _ := time.Parse("2006-01-02", i.DateAdded) - if t.After(time.Now().Add(-time.Hour * 720)) { - cves = append(cves, i) - } - } - - if len(cves) < 3 { - num = len(cves) - } - - if err := tmplt.Execute(&msgBuffer, struct { - CVEs []cisa.VulStruct - }{ - CVEs: cves[len(cves)-num:], - }); err != nil { - config.Cfg.Log.Debug(ERR_EXEC_TEMPLATE, "error", err) - tmpltError(w, http.StatusInternalServerError, ERR_PARSE_TEMPLATE) + msgBuffer, err := processCVEData(tmplt, w) + if err != nil { return } @@ -124,6 +101,16 @@ func tmpltTXT(w http.ResponseWriter) { return } + msgBuffer, err := processCVEData(tmplt, w) + if err != nil { + return + } + + w.Header().Add("Content-Type", TYPE_TEXT_PLAIN) + w.Write(msgBuffer.Bytes()) //nolint:errcheck +} + +func processCVEData(tmplt *template.Template, w http.ResponseWriter) (bytes.Buffer, error) { var ( msgBuffer bytes.Buffer cves []cisa.VulStruct @@ -149,9 +136,8 @@ func tmpltTXT(w http.ResponseWriter) { }); err != nil { config.Cfg.Log.Debug(ERR_EXEC_TEMPLATE, "error", err) tmpltError(w, http.StatusInternalServerError, ERR_PARSE_TEMPLATE) - return + return msgBuffer, err } - w.Header().Add("Content-Type", TYPE_TEXT_PLAIN) - w.Write(msgBuffer.Bytes()) //nolint:errcheck + return msgBuffer, nil }