Skip to content

Commit

Permalink
Removing exit code from grafana apikey list package
Browse files Browse the repository at this point in the history
  • Loading branch information
jtyr committed Jul 2, 2021
1 parent f06df78 commit f6f197e
Show file tree
Hide file tree
Showing 4 changed files with 12 additions and 14 deletions.
5 changes: 2 additions & 3 deletions cmd/grafana/apikey/apikeyList.go
Original file line number Diff line number Diff line change
Expand Up @@ -91,10 +91,9 @@ func checkListArgs(cmd *cobra.Command, args []string) error {

// runList runs the command's action.
func runList(cmd *cobra.Command, args []string) {
list, raw, ec, err := ak.List()
list, raw, err := ak.List()
if err != nil {
log.Errorf("failed to get API keys: %s", err)
log.Exit(ec)
log.Fatalf("failed to get API keys: %s", err)
}

oraFlag, err := cmd.Flags().GetBool("only-role-admin")
Expand Down
2 changes: 1 addition & 1 deletion pkg/grafana/apikey/apikeyDelete.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ func (a *APIKey) Delete() (string, int, error) {
return "", consts.ExitError, fmt.Errorf("failed to get client: %s", err)
}

list, _, _, err := a.List()
list, _, err := a.List()
if err != nil {
return "", consts.ExitError, fmt.Errorf("failed to get API key ID: %s", err)
}
Expand Down
15 changes: 7 additions & 8 deletions pkg/grafana/apikey/apikeyList.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ import (
"fmt"

_client "github.com/jtyr/gcapi/pkg/client"
"github.com/jtyr/gcapi/pkg/consts"
)

// ListItem described properties of individual List item returned by the API.
Expand All @@ -19,7 +18,7 @@ type ListItem struct {
type ListResp []ListItem

// List lists Grafana API keys and returns the list and the raw API response.
func (a *APIKey) List() (*ListResp, string, int, error) {
func (a *APIKey) List() (*ListResp, string, error) {
// Use Grafana API token
grafanaClientConfig := a.ClientConfig
grafanaClientConfig.Token = a.GrafanaToken
Expand All @@ -29,32 +28,32 @@ func (a *APIKey) List() (*ListResp, string, int, error) {
var err error
grafanaClientConfig.BaseURL, err = a.GetGrafanaAPIURL()
if err != nil {
return nil, "", consts.ExitError, fmt.Errorf("failed to get Grafana API URL: %s", err)
return nil, "", fmt.Errorf("failed to get Grafana API URL: %s", err)
}
} else {
grafanaClientConfig.BaseURL = a.BaseURL
}

client, err := _client.New(grafanaClientConfig)
if err != nil {
return nil, "", consts.ExitError, fmt.Errorf("failed to get client: %s", err)
return nil, "", fmt.Errorf("failed to get client: %s", err)
}

client.Endpoint = a.GrafanaEndpoint

body, statusCode, err := client.Get()
if err != nil {
if statusCode == 404 {
return nil, "", consts.ExitNotFound, fmt.Errorf("Grafana instance not found: %s", err)
return nil, "", fmt.Errorf("Grafana instance not found: %s", err)
}

return nil, "", consts.ExitError, err
return nil, "", err
}

var jsonData ListResp
if err := json.Unmarshal(body, &jsonData); err != nil {
return nil, "", consts.ExitError, fmt.Errorf("cannot parse API response as JSON: %s", err)
return nil, "", fmt.Errorf("cannot parse API response as JSON: %s", err)
}

return &jsonData, string(body), consts.ExitOk, nil
return &jsonData, string(body), nil
}
4 changes: 2 additions & 2 deletions pkg/grafana/apikey/apikeyList_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -74,9 +74,9 @@ func TestAPIKeyList(t *testing.T) {
a := New()
a.SetBaseURL(test.baseURL)

list, body, ec, err := a.List()
list, body, err := a.List()
if !test.expectingError && err != nil {
t.Errorf("Test [%d]: failed to call List (exit code=%d): %s", i, ec, err)
t.Errorf("Test [%d]: failed to call List: %s", i, err)
}

if list != nil && len(*list) == 0 {
Expand Down

0 comments on commit f6f197e

Please sign in to comment.