colorPrint for the output

Use the same format output for warnings even if they aren’t related to
a specific test ID
pull/6/head
Liz Rice 7 years ago
parent dcd416a521
commit 800c18ccf3

@ -38,6 +38,8 @@ const (
FAIL = "FAIL" FAIL = "FAIL"
// WARN could not carry out check. // WARN could not carry out check.
WARN = "WARN" WARN = "WARN"
// INFO informational message
INFO = "INFO"
// MASTER a master node // MASTER a master node
MASTER NodeType = "master" MASTER NodeType = "master"

@ -42,6 +42,14 @@ var (
// Used for variable substitution // Used for variable substitution
symbols = map[string]string{} symbols = map[string]string{}
// Print colors
colors = map[check.State]*color.Color{
check.PASS: color.New(color.FgGreen),
check.FAIL: color.New(color.FgRed),
check.WARN: color.New(color.FgYellow),
check.INFO: color.New(color.FgWhite),
}
) )
func runChecks(t check.NodeType) { func runChecks(t check.NodeType) {
@ -145,7 +153,7 @@ func verifyNodeType(t check.NodeType) {
for _, b := range binPath { for _, b := range binPath {
_, err := exec.LookPath(b) _, err := exec.LookPath(b)
if err != nil { if err != nil {
fmt.Fprintf(os.Stderr, "WARNING: %s: command not found on path - version check skipped\n", b) colorPrint(check.WARN, fmt.Sprintf("%s: command not found on path - version check skipped\n", b))
continue continue
} }
@ -153,12 +161,13 @@ func verifyNodeType(t check.NodeType) {
cmd := exec.Command(b, "--version") cmd := exec.Command(b, "--version")
out, _ = cmd.Output() out, _ = cmd.Output()
if matched, _ := regexp.MatchString(kubeVersion, string(out)); !matched { if matched, _ := regexp.MatchString(kubeVersion, string(out)); !matched {
fmt.Fprintf(os.Stderr, colorPrint(check.FAIL,
"%s unsupported version, expected %s, got %s\n", fmt.Sprintf(
b, "%s unsupported version, expected %s, got %s\n",
kubeVersion, b,
string(out), kubeVersion,
) string(out),
))
os.Exit(1) os.Exit(1)
} }
} }
@ -168,35 +177,34 @@ func verifyNodeType(t check.NodeType) {
cmd := exec.Command("ps", "-ef") cmd := exec.Command("ps", "-ef")
out, _ = cmd.Output() out, _ = cmd.Output()
if matched, _ := regexp.MatchString(".*"+b, string(out)); !matched { if matched, _ := regexp.MatchString(".*"+b, string(out)); !matched {
fmt.Fprintf(os.Stderr, "%s is not running\n", b) colorPrint(check.FAIL, fmt.Sprintf("%s is not running\n", b))
os.Exit(1) os.Exit(1)
} }
} }
for _, c := range confPath { for _, c := range confPath {
if _, err := os.Stat(c); os.IsNotExist(err) { if _, err := os.Stat(c); os.IsNotExist(err) {
fmt.Fprintf(os.Stderr, "WARNING: config file %s does not exist\n", c) colorPrint(check.WARN, fmt.Sprintf("config file %s does not exist\n", c))
// os.Exit(1)
} }
} }
} }
func prettyPrint(r *check.Controls, summary check.Summary) { // colorPrint outputs the state in a specific colour, along with a message string
colors := map[check.State]*color.Color{ func colorPrint(state check.State, s string) {
check.PASS: color.New(color.FgGreen), colors[state].Printf("[%s] ", state)
check.FAIL: color.New(color.FgRed), fmt.Printf("%s", s)
check.WARN: color.New(color.FgYellow), }
}
func prettyPrint(r *check.Controls, summary check.Summary) {
// Print checks and results. // Print checks and results.
fmt.Printf("[INFO] %s %s\n", r.ID, r.Text) colorPrint(check.INFO, fmt.Sprintf("%s %s\n", r.ID, r.Text))
for _, g := range r.Groups { for _, g := range r.Groups {
fmt.Printf("[INFO] %s %s\n", g.ID, g.Text) fmt.Printf("[INFO] %s %s\n", g.ID, g.Text)
for _, c := range g.Checks { for _, c := range g.Checks {
colors[c.State].Printf("[%s] ", c.State) colorPrint(c.State, fmt.Sprintf("%s %s\n", c.ID, c.Text))
fmt.Printf("%s %s\n", c.ID, c.Text)
} }
} }
fmt.Println() fmt.Println()
// Print remediations. // Print remediations.

Loading…
Cancel
Save