Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 6 additions & 2 deletions cmd/exam.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
},
}

Expand Down
2 changes: 1 addition & 1 deletion cmd/listen.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}

},
Expand Down
6 changes: 5 additions & 1 deletion test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -5,4 +5,8 @@ tests:
paths:
- /farm
- /something
- /else
- /else
- /pie
- /apple
- /pear
- /banana
14 changes: 9 additions & 5 deletions utils/checkup.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,6 @@ package utils

import (
"encoding/base64"
"fmt"
"log"
"net/http"
"os"
)
Expand All @@ -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
}

Expand Down
19 changes: 18 additions & 1 deletion utils/errorcheck.go
Original file line number Diff line number Diff line change
@@ -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)
}