Skip to content

Commit

Permalink
Add check numbers to output when applicable (#164)
Browse files Browse the repository at this point in the history
  • Loading branch information
sourque authored Jan 14, 2023
1 parent 070abec commit 0aafacb
Show file tree
Hide file tree
Showing 3 changed files with 79 additions and 45 deletions.
76 changes: 38 additions & 38 deletions checks_linux.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
39 changes: 33 additions & 6 deletions output.go
Original file line number Diff line number Diff line change
Expand Up @@ -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{}) {
Expand All @@ -68,15 +85,25 @@ 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)
}
}

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)
}
}
Expand Down
9 changes: 8 additions & 1 deletion score.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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))
}

Expand Down

0 comments on commit 0aafacb

Please sign in to comment.