removes duplicate code

This commit is contained in:
Hyatt 2024-02-12 07:26:51 -06:00
parent 534aa6ad5b
commit 388d003132
Signed by: nhyatt
GPG Key ID: C50D0BBB5BC40BEA

View File

@ -16,7 +16,7 @@ import (
const ( const (
ERR_PARSE_TEMPLATE = "Template Parse Error" ERR_PARSE_TEMPLATE = "Template Parse Error"
ERR_EXEC_TEMPLATE = "Template Execution Error" ERR_EXEC_TEMPLATE = "Template Execution Error"
) )
type webErrStruct struct { type webErrStruct struct {
@ -57,31 +57,8 @@ func tmpltWebRoot(w http.ResponseWriter, r *http.Request) {
return return
} }
var ( msgBuffer, err := processCVEData(tmplt, w)
msgBuffer bytes.Buffer if err != nil {
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)
return return
} }
@ -124,6 +101,16 @@ func tmpltTXT(w http.ResponseWriter) {
return 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 ( var (
msgBuffer bytes.Buffer msgBuffer bytes.Buffer
cves []cisa.VulStruct cves []cisa.VulStruct
@ -149,9 +136,8 @@ func tmpltTXT(w http.ResponseWriter) {
}); err != nil { }); err != nil {
config.Cfg.Log.Debug(ERR_EXEC_TEMPLATE, "error", err) config.Cfg.Log.Debug(ERR_EXEC_TEMPLATE, "error", err)
tmpltError(w, http.StatusInternalServerError, ERR_PARSE_TEMPLATE) tmpltError(w, http.StatusInternalServerError, ERR_PARSE_TEMPLATE)
return return msgBuffer, err
} }
w.Header().Add("Content-Type", TYPE_TEXT_PLAIN) return msgBuffer, nil
w.Write(msgBuffer.Bytes()) //nolint:errcheck
} }