Skip to content

Commit

Permalink
Return different exit code if the targetted item was not found
Browse files Browse the repository at this point in the history
  • Loading branch information
jtyr committed Apr 21, 2021
1 parent f0ff72d commit f0800d0
Show file tree
Hide file tree
Showing 14 changed files with 77 additions and 52 deletions.
5 changes: 3 additions & 2 deletions cmd/apikey/apikeyDelete.go
Original file line number Diff line number Diff line change
Expand Up @@ -57,9 +57,10 @@ func checkDeleteArgs(cmd *cobra.Command, args []string) error {

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

rawFlag, err := cmd.Flags().GetBool("raw")
Expand Down
6 changes: 3 additions & 3 deletions cmd/apikey/apikeyList.go
Original file line number Diff line number Diff line change
Expand Up @@ -62,10 +62,10 @@ func checkListArgs(cmd *cobra.Command, args []string) error {

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

oraFlag, err := cmd.Flags().GetBool("only-role-admin")
Expand Down
5 changes: 3 additions & 2 deletions cmd/grafana/apikey/apikeyDelete.go
Original file line number Diff line number Diff line change
Expand Up @@ -86,9 +86,10 @@ func checkDeleteArgs(cmd *cobra.Command, args []string) error {

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

rawFlag, err := cmd.Flags().GetBool("raw")
Expand Down
4 changes: 3 additions & 1 deletion cmd/grafana/apikey/apikeyList.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import (
"github.com/spf13/cobra"

"github.com/jtyr/gcapi/cmd/common"
"github.com/jtyr/gcapi/pkg/consts"
"github.com/jtyr/gcapi/pkg/grafana/apikey"
)

Expand Down Expand Up @@ -144,7 +145,8 @@ func runList(cmd *cobra.Command, args []string) {
}

if !printed && ak.Name != "" {
log.Fatal("API key not found")
log.Error("failed to list API keys: API key not found")
log.Exit(consts.ExitNotFound)
}
}

Expand Down
2 changes: 1 addition & 1 deletion cmd/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ var flags = rootFlags{}
var rootCmd = &cobra.Command{
Use: "gcapi-cli",
Short: "Tool to access Grafana Cloud API",
Long: "gcapi-cli is a tool that allows an easy access to the Grafana Cloud API and Grafana API.",
Long: "gcapi-cli is a tool that allows an easy access to the Grafana Cloud API (and Grafana API).",
SilenceErrors: true,
SilenceUsage: true,
Version: version.GetVersion(),
Expand Down
5 changes: 3 additions & 2 deletions cmd/stack/stackDelete.go
Original file line number Diff line number Diff line change
Expand Up @@ -49,9 +49,10 @@ func checkDeleteArgs(cmd *cobra.Command, args []string) error {

// runDelete runs the command's action.
func runDelete(cmd *cobra.Command, args []string) {
raw, err := st.Delete()
raw, ec, err := st.Delete()
if err != nil {
log.Fatalf("failed to delete stack: %s", err)
log.Errorf("failed to delete stack: %s", err)
log.Exit(ec)
}

rawFlag, err := cmd.Flags().GetBool("raw")
Expand Down
5 changes: 3 additions & 2 deletions cmd/stack/stackList.go
Original file line number Diff line number Diff line change
Expand Up @@ -58,9 +58,10 @@ func checkListArgs(cmd *cobra.Command, args []string) error {

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

rawFlag, err := cmd.Flags().GetBool("raw")
Expand Down
11 changes: 6 additions & 5 deletions pkg/apikey/apikeyDelete.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,25 +5,26 @@ import (
"fmt"

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

// Delete deletes the API key.
func (a *APIKey) Delete() (string, error) {
func (a *APIKey) Delete() (string, int, error) {
client, err := _client.New(a.ClientConfig)
if err != nil {
return "", fmt.Errorf("failed to get client: %s", err)
return "", consts.ExitError, fmt.Errorf("failed to get client: %s", err)
}

client.Endpoint = fmt.Sprintf(a.Endpoint+"/%s", a.OrgSlug, a.Name)

body, statusCode, err := client.Delete()
if err != nil {
if statusCode == 404 {
return "", errors.New("API key not found")
return "", consts.ExitNotFound, errors.New("API key not found")
}

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

return string(body), nil
return string(body), consts.ExitOk, nil
}
23 changes: 13 additions & 10 deletions pkg/apikey/apikeyList.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,8 @@ import (
"errors"
"fmt"

"github.com/jtyr/gcapi/pkg/client"
_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 @@ -20,10 +21,10 @@ type listResp struct {
}

// List returns the list of API keys and raw API response.
func (a *APIKey) List() (*[]ListItem, string, error) {
client, err := client.New(a.ClientConfig)
func (a *APIKey) List() (*[]ListItem, string, int, error) {
client, err := _client.New(a.ClientConfig)
if err != nil {
return nil, "", fmt.Errorf("failed to get client: %s", err)
return nil, "", consts.ExitError, fmt.Errorf("failed to get client: %s", err)
}

if a.Name == "" {
Expand All @@ -34,25 +35,27 @@ func (a *APIKey) List() (*[]ListItem, string, error) {

body, statusCode, err := client.Get()
if err != nil {
if statusCode == 404 {
return nil, "", errors.New("Org Slug not found")
if a.Name == "" && statusCode == 404 {
return nil, "", consts.ExitError, errors.New("Org Slug not found")
} else if a.Name != "" && statusCode == 404 {
return nil, "", consts.ExitNotFound, errors.New("key not found")
}

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

var jsonData listResp
if a.Name != "" {
jsonData.Items = append(jsonData.Items, ListItem{})

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

return &jsonData.Items, string(body), nil
return &jsonData.Items, string(body), consts.ExitOk, nil
}
10 changes: 10 additions & 0 deletions pkg/consts/consts.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
package consts

// ExitOk is the exit code when everything is OK.
const ExitOk = 0

// ExitError is the exit code when there was an error.
const ExitError = 1

// ExitNotFound is the exit code when the searched item was not found.
const ExitNotFound = 13
17 changes: 9 additions & 8 deletions pkg/grafana/apikey/apikeyDelete.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,11 @@ import (
"fmt"

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

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

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

list, _, err := a.List()
if err != nil {
return "", fmt.Errorf("failed to get API key ID: %s", err)
return "", consts.ExitError, fmt.Errorf("failed to get API key ID: %s", err)
}

keyID := -1
Expand All @@ -44,19 +45,19 @@ func (a *APIKey) Delete() (string, error) {
}

if keyID == -1 {
return "", errors.New("API key not found")
return "", consts.ExitNotFound, errors.New("API key not found")
}

client.Endpoint = fmt.Sprintf(a.GrafanaEndpoint+"/%d", keyID)

body, statusCode, err := client.Delete()
if err != nil {
if statusCode == 404 {
return "", errors.New("API key not found")
return "", consts.ExitNotFound, errors.New("API key not found")
}

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

return string(body), nil
return string(body), consts.ExitOk, nil
}
2 changes: 1 addition & 1 deletion pkg/grafana/grafana.go
Original file line number Diff line number Diff line change
Expand Up @@ -115,7 +115,7 @@ func (g *Grafana) GetGrafanaAPIURL() (string, error) {

stack.SetToken(g.ClientConfig.Token)

list, _, err := stack.List()
list, _, _, err := stack.List()
if err != nil {
return "", fmt.Errorf("failed to get stack details: %s", err)
}
Expand Down
11 changes: 6 additions & 5 deletions pkg/stack/stackDelete.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,25 +5,26 @@ import (
"fmt"

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

// Delete deletes the Stack.
func (s *Stack) Delete() (string, error) {
func (s *Stack) Delete() (string, int, error) {
client, err := _client.New(s.ClientConfig)
if err != nil {
return "", fmt.Errorf("failed to get client: %s", err)
return "", consts.ExitError, fmt.Errorf("failed to get client: %s", err)
}

client.Endpoint = fmt.Sprintf(s.Endpoint+"/%s", s.StackSlug)

body, statusCode, err := client.Delete()
if err != nil {
if statusCode == 404 {
return "", errors.New("API key not found")
return "", consts.ExitNotFound, errors.New("API key not found")
}

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

return string(body), nil
return string(body), consts.ExitOk, nil
}
23 changes: 13 additions & 10 deletions pkg/stack/stackList.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,8 @@ import (
"errors"
"fmt"

"github.com/jtyr/gcapi/pkg/client"
_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 @@ -30,10 +31,10 @@ type listResp struct {
}

// List returns the list of API keys and raw API response.
func (s *Stack) List() (*[]ListItem, string, error) {
client, err := client.New(s.ClientConfig)
func (s *Stack) List() (*[]ListItem, string, int, error) {
client, err := _client.New(s.ClientConfig)
if err != nil {
return nil, "", fmt.Errorf("failed to get client: %s", err)
return nil, "", consts.ExitError, fmt.Errorf("failed to get client: %s", err)
}

if s.StackSlug == "" {
Expand All @@ -44,11 +45,13 @@ func (s *Stack) List() (*[]ListItem, string, error) {

body, statusCode, err := client.Get()
if err != nil {
if statusCode == 404 {
return nil, "", errors.New("Stack Slug not found")
if s.StackSlug == "" && statusCode == 404 {
return nil, "", consts.ExitError, errors.New("Org Slug not found")
} else if s.StackSlug != "" && statusCode == 404 {
return nil, "", consts.ExitNotFound, errors.New("Stack not found")
}

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

var jsonData listResp
Expand All @@ -57,13 +60,13 @@ func (s *Stack) List() (*[]ListItem, string, error) {
jsonData.Items = append(jsonData.Items, ListItem{})

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

return &jsonData.Items, string(body), nil
return &jsonData.Items, string(body), consts.ExitOk, nil
}

0 comments on commit f0800d0

Please sign in to comment.