Skip to content

Commit

Permalink
Converted all commands to service
Browse files Browse the repository at this point in the history
  • Loading branch information
stefan-hudelmaier committed Aug 25, 2022
1 parent 8d05905 commit 2657413
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 29 deletions.
30 changes: 3 additions & 27 deletions operations/show-operation.go
Original file line number Diff line number Diff line change
@@ -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 {
Expand All @@ -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)

Expand Down
37 changes: 35 additions & 2 deletions services/check-service.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -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 {
Expand All @@ -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
}

0 comments on commit 2657413

Please sign in to comment.