From 0aafacbbe27306b66a8f43499dd3de80d38b2d80 Mon Sep 17 00:00:00 2001 From: sourque Date: Sat, 14 Jan 2023 10:53:31 -0800 Subject: [PATCH] Add check numbers to output when applicable (#164) --- checks_linux.go | 76 ++++++++++++++++++++++++------------------------- output.go | 39 +++++++++++++++++++++---- score.go | 9 +++++- 3 files changed, 79 insertions(+), 45 deletions(-) diff --git a/checks_linux.go b/checks_linux.go index cdc6e231..73c5db3d 100644 --- a/checks_linux.go +++ b/checks_linux.go @@ -11,48 +11,48 @@ import ( func (c cond) AutoCheckUpdatesEnabled() (bool, error) { result, err := cond{ - Path: "/etc/apt/apt.conf.d/", - Value: `(?i)^\s*APT::Periodic::Update-Package-Lists\s+"1"\s*;\s*$`, - }.DirContains() - // If /etc/apt/ does not exist, try dnf (RHEL) - if err != nil { - autoConf, err := cond{ - Path: "/etc/dnf/automatic.conf", - }.PathExists() - if err != nil { - return false, err - } - if autoConf { - applyUpdates, err := cond{ - Path: "/etc/dnf/automatic.conf", - Value: `(?i)^\s*apply_updates\s*=\s*(1|on|yes|true)`, - }.FileContains() - if err != nil { - return false, err - } + Path: "/etc/apt/apt.conf.d/", + Value: `(?i)^\s*APT::Periodic::Update-Package-Lists\s+"1"\s*;\s*$`, + }.DirContains() + // If /etc/apt/ does not exist, try dnf (RHEL) + if err != nil { + autoConf, err := cond{ + Path: "/etc/dnf/automatic.conf", + }.PathExists() + if err != nil { + return false, err + } + if autoConf { + applyUpdates, err := cond{ + Path: "/etc/dnf/automatic.conf", + Value: `(?i)^\s*apply_updates\s*=\s*(1|on|yes|true)`, + }.FileContains() + if err != nil { + return false, err + } - autoTimer, err := cond{ - Path: "/etc/systemd/system/timers.target.wants/dnf-automatic.timer", - }.PathExists() - if err != nil { - return false, err - } + autoTimer, err := cond{ + Path: "/etc/systemd/system/timers.target.wants/dnf-automatic.timer", + }.PathExists() + if err != nil { + return false, err + } - if applyUpdates && autoTimer { - return true, nil - } + if applyUpdates && autoTimer { + return true, nil + } - autoInstallTimer, err := cond{ - Path: "/etc/systemd/system/timers.target.wants/dnf-automatic-install.timer", - }.PathExists() - if err != nil { - return false, err - } - return autoInstallTimer, nil - } + autoInstallTimer, err := cond{ + Path: "/etc/systemd/system/timers.target.wants/dnf-automatic-install.timer", + }.PathExists() + if err != nil { + return false, err + } + return autoInstallTimer, nil + } - } - return result, err + } + return result, err } // Command checks if a given shell command ran successfully (that is, did not diff --git a/output.go b/output.go index d86e2c14..f4a7792e 100644 --- a/output.go +++ b/output.go @@ -43,18 +43,35 @@ func ask(p ...interface{}) bool { func pass(p ...interface{}) { toPrint := fmt.Sprintln(p...) - printStr := printer(color.FgGreen, "PASS", toPrint) - fmt.Printf(printStr) + var printStr string + if checkCount != 0 { + printStr = printer(color.FgGreen, fmt.Sprintf("%s:%d", "PASS", checkCount), toPrint) + } else { + printStr = printer(color.FgGreen, "PASS", toPrint) + } + fmt.Print(printStr) } func fail(p ...interface{}) { toPrint := fmt.Sprintln(p...) - fmt.Printf(printer(color.FgRed, "FAIL", toPrint)) + var printStr string + if checkCount != 0 { + printStr = printer(color.FgRed, fmt.Sprintf("%s:%d", "FAIL", checkCount), toPrint) + } else { + printStr = printer(color.FgRed, "FAIL", toPrint) + } + fmt.Print(printStr) } func warn(p ...interface{}) { toPrint := fmt.Sprintln(p...) - fmt.Printf(printer(color.FgYellow, "WARN", toPrint)) + var printStr string + if checkCount != 0 { + printStr = printer(color.FgYellow, fmt.Sprintf("%s:%d", "WARN", checkCount), toPrint) + } else { + printStr = printer(color.FgYellow, "WARN", toPrint) + } + fmt.Print(printStr) } func debug(p ...interface{}) { @@ -68,7 +85,12 @@ func debug(p ...interface{}) { // with custom builds. if DEBUG_BUILD && debugEnabled { toPrint := fmt.Sprintln(p...) - printStr := printer(color.FgMagenta, "DBUG", toPrint) + var printStr string + if checkCount != 0 { + printStr = printer(color.FgMagenta, fmt.Sprintf("%s:%d", "DBUG", checkCount), toPrint) + } else { + printStr = printer(color.FgMagenta, "DBUG", toPrint) + } fmt.Printf(printStr) } } @@ -76,7 +98,12 @@ func debug(p ...interface{}) { func info(p ...interface{}) { if verboseEnabled { toPrint := fmt.Sprintln(p...) - printStr := printer(color.FgCyan, "INFO", toPrint) + var printStr string + if checkCount != 0 { + printStr = printer(color.FgCyan, fmt.Sprintf("%s:%d", "INFO", checkCount), toPrint) + } else { + printStr = printer(color.FgCyan, "INFO", toPrint) + } fmt.Printf(printStr) } } diff --git a/score.go b/score.go index d4f81c7c..d91dcc06 100644 --- a/score.go +++ b/score.go @@ -13,6 +13,10 @@ var ( conf = &config{} image = &imageData{} conn = &connData{} + + // checkCount keeps track of the current check being scored, and is used + // for identifying which check caused a given error or warning. + checkCount int ) // imageData is the current scoring data for the image. It is able to be @@ -190,9 +194,12 @@ func checkConfigData() { // scoreChecks runs through every check configured. func scoreChecks() { - for _, check := range conf.Check { + for i, check := range conf.Check { + // checkCount is the same as in printConfig, 1-based count + checkCount = i + 1 scoreCheck(check) } + checkCount = 0 info(fmt.Sprintf("Score: %d", image.Score)) }