From 800c18ccf32b113e36d3eac0c8f2eb2fdacd7394 Mon Sep 17 00:00:00 2001 From: Liz Rice Date: Tue, 20 Jun 2017 09:54:17 +0100 Subject: [PATCH] colorPrint for the output MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Use the same format output for warnings even if they aren’t related to a specific test ID --- check/check.go | 2 ++ cmd/common.go | 46 +++++++++++++++++++++++++++------------------- 2 files changed, 29 insertions(+), 19 deletions(-) diff --git a/check/check.go b/check/check.go index ef0be6b..947f85c 100644 --- a/check/check.go +++ b/check/check.go @@ -38,6 +38,8 @@ const ( FAIL = "FAIL" // WARN could not carry out check. WARN = "WARN" + // INFO informational message + INFO = "INFO" // MASTER a master node MASTER NodeType = "master" diff --git a/cmd/common.go b/cmd/common.go index adb36e1..2885ace 100644 --- a/cmd/common.go +++ b/cmd/common.go @@ -42,6 +42,14 @@ var ( // Used for variable substitution 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) { @@ -145,7 +153,7 @@ func verifyNodeType(t check.NodeType) { for _, b := range binPath { _, err := exec.LookPath(b) 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 } @@ -153,12 +161,13 @@ func verifyNodeType(t check.NodeType) { cmd := exec.Command(b, "--version") out, _ = cmd.Output() if matched, _ := regexp.MatchString(kubeVersion, string(out)); !matched { - fmt.Fprintf(os.Stderr, - "%s unsupported version, expected %s, got %s\n", - b, - kubeVersion, - string(out), - ) + colorPrint(check.FAIL, + fmt.Sprintf( + "%s unsupported version, expected %s, got %s\n", + b, + kubeVersion, + string(out), + )) os.Exit(1) } } @@ -168,35 +177,34 @@ func verifyNodeType(t check.NodeType) { cmd := exec.Command("ps", "-ef") out, _ = cmd.Output() 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) } } for _, c := range confPath { if _, err := os.Stat(c); os.IsNotExist(err) { - fmt.Fprintf(os.Stderr, "WARNING: config file %s does not exist\n", c) - // os.Exit(1) + colorPrint(check.WARN, fmt.Sprintf("config file %s does not exist\n", c)) } } } -func prettyPrint(r *check.Controls, summary check.Summary) { - colors := map[check.State]*color.Color{ - check.PASS: color.New(color.FgGreen), - check.FAIL: color.New(color.FgRed), - check.WARN: color.New(color.FgYellow), - } +// colorPrint outputs the state in a specific colour, along with a message string +func colorPrint(state check.State, s string) { + colors[state].Printf("[%s] ", state) + fmt.Printf("%s", s) +} +func prettyPrint(r *check.Controls, summary check.Summary) { // 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 { fmt.Printf("[INFO] %s %s\n", g.ID, g.Text) for _, c := range g.Checks { - colors[c.State].Printf("[%s] ", c.State) - fmt.Printf("%s %s\n", c.ID, c.Text) + colorPrint(c.State, fmt.Sprintf("%s %s\n", c.ID, c.Text)) } } + fmt.Println() // Print remediations.