diff --git a/operations/show-operation.go b/operations/show-operation.go index 5a9f224..8cc1062 100644 --- a/operations/show-operation.go +++ b/operations/show-operation.go @@ -1,14 +1,10 @@ package operations import ( - "encoding/json" "fmt" "github.com/stefan-hudelmaier/checkson-cli/operations/auth" "github.com/stefan-hudelmaier/checkson-cli/output" "github.com/stefan-hudelmaier/checkson-cli/services" - "io/ioutil" - "log" - "net/http" ) type ShowOperation struct { @@ -21,32 +17,12 @@ type ShowOperationFlags struct { func (operation *ShowOperation) ShowOperation(checkName string, flags ShowOperationFlags) error { authToken, _ := auth.ReadAuthToken() - client := &http.Client{} - req, err := http.NewRequest("GET", services.getApiUrl(flags.DevMode, "api/checks/")+checkName, nil) + check, err := services.GetCheck(checkName, authToken, flags.DevMode) if err != nil { - return fmt.Errorf("problem preparing request: %w", err) - } - req.Header.Add("Authorization", "Bearer "+authToken) - - resp, err1 := client.Do(req) - if err1 != nil { - return fmt.Errorf("problem performing request: %w", err1) - } - - defer resp.Body.Close() - output.PrintStrings("Response status:", resp.Status) - - body, readErr := ioutil.ReadAll(resp.Body) - if readErr != nil { - panic(readErr) - } - - var check services.Check - jsonErr := json.Unmarshal(body, &check) - if jsonErr != nil { - log.Fatalf("unable to parse value: %q, error: %s", string(body), jsonErr.Error()) + return err } + // TODO: Property output check fmt.Println(check) output.PrintStrings(check.Name, check.DockerImage) diff --git a/services/check-service.go b/services/check-service.go index 606c34d..4818ac5 100644 --- a/services/check-service.go +++ b/services/check-service.go @@ -98,7 +98,6 @@ func ListRuns(authToken string, devMode bool) ([]Run, error) { if err1 != nil { return nil, fmt.Errorf("problem preparing request: %w", err1) } - addHeaders(req, authToken) resp, err2 := client.Do(req) @@ -146,7 +145,6 @@ func GetLog(checkName string, runId string, authToken string, devMode bool) (str if err3 != nil { return "", err3 } - output.Debugf("Response status: %s", resp.Status) body, err4 := ioutil.ReadAll(resp.Body) if err4 != nil { @@ -155,3 +153,38 @@ func GetLog(checkName string, runId string, authToken string, devMode bool) (str return string(body[:]), nil } + +func GetCheck(checkName string, authToken string, devMode bool) (*Check, error) { + + client := &http.Client{} + req, err := http.NewRequest("GET", getApiUrl(devMode, "api/checks/")+checkName, nil) + if err != nil { + return nil, fmt.Errorf("problem preparing request: %w", err) + } + addHeaders(req, authToken) + + resp, err1 := client.Do(req) + if err1 != nil { + return nil, fmt.Errorf("problem performing request: %w", err1) + } + + defer resp.Body.Close() + + err2 := handleRestResponse("Check", resp) + if err2 != nil { + return nil, err2 + } + + body, readErr := ioutil.ReadAll(resp.Body) + if readErr != nil { + return nil, readErr + } + + var check Check + jsonErr := json.Unmarshal(body, &check) + if jsonErr != nil { + return nil, jsonErr + } + + return &check, nil +}