diff --git a/cmd/exam.go b/cmd/exam.go index 1288d31..c0bcd5a 100644 --- a/cmd/exam.go +++ b/cmd/exam.go @@ -44,13 +44,17 @@ var examCmd = &cobra.Command{ checkup := utils.Checkup(*checkRequest) fmt.Fprintf(w, "\n %s\t%d\t%d\t%v\t", checkup.Endpoint, checkup.Code, checkup.Result, checkup.Pass) if checkup.Pass == false { - exitCode = 1 + exitCode = 3 } } } w.Flush() fmt.Println() - defer os.Exit(exitCode) + if exitCode == 0 { + defer os.Exit(exitCode) + } else { + defer utils.CustomErrorOut("**exam failed**", exitCode) + } }, } diff --git a/cmd/listen.go b/cmd/listen.go index e924bf5..d7b9091 100644 --- a/cmd/listen.go +++ b/cmd/listen.go @@ -49,7 +49,7 @@ var listenCmd = &cobra.Command{ if checkup.Pass { defer os.Exit(0) } else { - defer os.Exit(1) + defer utils.CustomErrorOut("\n**checkup failed**", 2) } }, diff --git a/test.yml b/test.yml index f9cd03c..faaab98 100644 --- a/test.yml +++ b/test.yml @@ -5,4 +5,8 @@ tests: paths: - /farm - /something - - /else \ No newline at end of file + - /else + - /pie + - /apple + - /pear + - /banana \ No newline at end of file diff --git a/utils/checkup.go b/utils/checkup.go index 2661fd4..f8b7621 100644 --- a/utils/checkup.go +++ b/utils/checkup.go @@ -2,8 +2,6 @@ package utils import ( "encoding/base64" - "fmt" - "log" "net/http" "os" ) @@ -27,25 +25,31 @@ type CheckupResults struct { // Checkup ... utility function to run one test func Checkup(healthForm CheckupRequest) CheckupResults { request, err := http.NewRequest("GET", healthForm.Endpoint, nil) - if err != nil { - log.Fatalln(err) + ErrorCheck(err) + + if request.URL.Scheme == "" { + ErrorOut("You must use a supported protocol scheme like http or https") } + request.Header.Set("User-Agent", "CheckupCli/1.0") if healthForm.Auth { request.Header.Add("Authorization", "Basic "+addBasicAuth(os.Getenv("CU_USER"), os.Getenv("CU_PASS"))) healthForm.Client.CheckRedirect = addAuthOnRedirect } + resp, err := healthForm.Client.Do(request) if err != nil { - fmt.Printf("error in checkup function: %v", err) + ErrorCheck(err) } defer resp.Body.Close() + results := CheckupResults{ Endpoint: healthForm.Endpoint, Code: healthForm.Code, Result: resp.StatusCode, Pass: resp.StatusCode == healthForm.Code, } + return results } diff --git a/utils/errorcheck.go b/utils/errorcheck.go index 3bc1868..954541e 100644 --- a/utils/errorcheck.go +++ b/utils/errorcheck.go @@ -1,8 +1,25 @@ package utils +import ( + "fmt" + "os" +) + // ErrorCheck ... utility function to help stream line error checking func ErrorCheck(e error) { if e != nil { - panic(e) + ErrorOut(e) } } + +// ErrorOut .. function to hold the actual erroring out +func ErrorOut(err interface{}) { + fmt.Fprintln(os.Stderr, err) + os.Exit(1) +} + +// CustomErrorOut ... function to error using a custom string and exit code +func CustomErrorOut(customErr string, customExitCode int) { + fmt.Println(customErr) + os.Exit(customExitCode) +}