removes duplicate code
This commit is contained in:
parent
534aa6ad5b
commit
388d003132
@ -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
|
||||
}
|
||||
|
Loading…
x
Reference in New Issue
Block a user