-
Notifications
You must be signed in to change notification settings - Fork 13
feat: list projects err handling #21
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
f4c65bd
6c25ca4
d5d9150
6bc6f21
4e3a1fd
90fd9e8
5657dbf
c943717
b3347df
2189e1c
3e0a218
cfa1dcf
a58f061
327369f
f378d35
cc9c53f
a628860
6097aba
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -2,36 +2,40 @@ package projects | |
|
|
||
| import ( | ||
| "context" | ||
| "errors" | ||
| "fmt" | ||
| "ld-cli/internal/projects" | ||
| "net/url" | ||
|
|
||
| "github.com/spf13/cobra" | ||
| "github.com/spf13/viper" | ||
|
|
||
| "ld-cli/internal/errors" | ||
| "ld-cli/internal/projects" | ||
| ) | ||
|
|
||
| func NewListCmd() *cobra.Command { | ||
| cmd := &cobra.Command{ | ||
| Use: "list", | ||
| Short: "Return a list of projects", | ||
| Long: "Return a list of projects", | ||
| RunE: runList, | ||
| Use: "list", | ||
| Short: "Return a list of projects", | ||
| Long: "Return a list of projects", | ||
| PreRunE: validate, | ||
| RunE: runList, | ||
| } | ||
|
|
||
| cmd.AddCommand() | ||
|
|
||
| return cmd | ||
| } | ||
|
|
||
| func runList(cmd *cobra.Command, args []string) error { | ||
| // TODO: handle missing flags | ||
| if viper.GetString("accessToken") == "" { | ||
| return errors.New("accessToken required") | ||
| } | ||
| if viper.GetString("baseUri") == "" { | ||
| return errors.New("baseUri required") | ||
| // validate ensures the flags are valid before using them. | ||
| func validate(cmd *cobra.Command, args []string) error { | ||
|
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I'd prefer to have this in the root command so we don't need it for each command that uses the global flags, but we can figure that out later. |
||
| _, err := url.ParseRequestURI(viper.GetString("baseUri")) | ||
| if err != nil { | ||
| return errors.ErrInvalidBaseURI | ||
|
Comment on lines
+29
to
+31
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. should we also make sure that it's a valid LD URI?
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I didn't want to do too much validation since it should only be an issue if someone is overriding the default, so they would be able to see the URL they put in. We could add more validation if we find people are confused. |
||
| } | ||
|
|
||
| return nil | ||
| } | ||
|
|
||
| // runList fetches a list of projects. | ||
| func runList(cmd *cobra.Command, args []string) error { | ||
| client := projects.NewClient( | ||
| viper.GetString("accessToken"), | ||
| viper.GetString("baseUri"), | ||
|
|
@@ -44,7 +48,6 @@ func runList(cmd *cobra.Command, args []string) error { | |
| return err | ||
| } | ||
|
|
||
| // TODO: should this return response and let caller output or pass in stdout-ish interface? | ||
| fmt.Fprintf(cmd.OutOrStdout(), string(response)+"\n") | ||
|
|
||
| return nil | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,24 +1,43 @@ | ||
| package cmd | ||
|
|
||
| import ( | ||
| "errors" | ||
| "fmt" | ||
| "os" | ||
|
|
||
| errs "ld-cli/internal/errors" | ||
|
|
||
| "github.com/spf13/cobra" | ||
| "github.com/spf13/viper" | ||
|
|
||
| "ld-cli/cmd/projects" | ||
| ) | ||
|
|
||
| var rootCmd = &cobra.Command{ | ||
| Use: "ldcli", | ||
| Short: "LaunchDarkly CLI", | ||
| Long: "LaunchDarkly CLI to control your feature flags", | ||
| Use: "ldcli", | ||
| Short: "LaunchDarkly CLI", | ||
| Long: "LaunchDarkly CLI to control your feature flags", | ||
| Version: "0.0.1", // TODO: set this based on release or use `cmd.SetVersionTemplate(s string)` | ||
|
|
||
| // Handle errors differently based on type. | ||
| // We don't want to show the usage if the user has the right structure but invalid data such as | ||
| // the wrong key. | ||
| SilenceUsage: true, | ||
| SilenceErrors: true, | ||
| } | ||
|
|
||
| func Execute() { | ||
| err := rootCmd.Execute() | ||
| if err != nil { | ||
| os.Exit(1) | ||
| switch { | ||
|
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The idea here is to let errors bubble up and handle them in one spot. We can set whatever error we want in the subcommands to make this switch statement simpler to work with. |
||
| case errors.Is(err, errs.ErrInvalidBaseURI): | ||
| fmt.Fprintln(os.Stderr, err.Error()) | ||
| case errors.Is(err, errs.ErrUnauthorized): | ||
|
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This more closely matches the stripe CLI. If there's a structural error like a missing flag or invalid command, it returns an error and shows usage. If it's a logical error like an invalid access token or project key, it shows the error but not usage. We can test this type of code once we have a higher level of dependency injection to pass in a mock API client from the highest level. Otherwise the tests will try to execute a real HTTP call.
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Nice this makes sense/I like this |
||
| fmt.Fprintln(os.Stderr, err.Error()) | ||
| default: | ||
| fmt.Println(rootCmd.ErrPrefix(), err.Error()) | ||
| fmt.Println(rootCmd.UsageString()) | ||
| } | ||
| } | ||
| } | ||
|
|
||
|
|
@@ -35,22 +54,29 @@ func init() { | |
| "", | ||
| "LaunchDarkly personal access token", | ||
| ) | ||
| err := viper.BindPFlag("accessToken", rootCmd.PersistentFlags().Lookup("accessToken")) | ||
| err := rootCmd.MarkPersistentFlagRequired("accessToken") | ||
| if err != nil { | ||
| panic(err) | ||
|
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. These errors should never happen, but we should see what they are in case they do. |
||
| } | ||
| err = viper.BindPFlag("accessToken", rootCmd.PersistentFlags().Lookup("accessToken")) | ||
| if err != nil { | ||
| os.Exit(1) | ||
| panic(err) | ||
| } | ||
|
|
||
| rootCmd.PersistentFlags().StringVarP( | ||
| &baseURI, | ||
| "baseUri", | ||
| "u", | ||
| "http://localhost:3000", | ||
| "https://app.launchdarkly.com", | ||
|
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This changes the default to production so others aren't surprised when using the CLI. |
||
| "LaunchDarkly base URI", | ||
| ) | ||
| err = viper.BindPFlag("baseUri", rootCmd.PersistentFlags().Lookup("baseUri")) | ||
| if err != nil { | ||
| os.Exit(1) | ||
| panic(err) | ||
| } | ||
|
|
||
| rootCmd.SetErrPrefix("") | ||
|
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This removes "Error: {msg}" and just shows the error message. |
||
|
|
||
| rootCmd.AddCommand(projects.NewProjectsCmd()) | ||
| rootCmd.AddCommand(setupCmd) | ||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,8 @@ | ||
| package errors | ||
|
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This package would shadow the standard library |
||
|
|
||
| import "errors" | ||
|
|
||
| var ( | ||
| ErrInvalidBaseURI = errors.New("baseUri is invalid") | ||
| ErrUnauthorized = errors.New("You are not authorized to make this request.") | ||
| ) | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -5,6 +5,8 @@ import ( | |
| "encoding/json" | ||
|
|
||
| ldapi "github.com/launchdarkly/api-client-go/v14" | ||
|
|
||
| "ld-cli/internal/errors" | ||
| ) | ||
|
|
||
| type Client interface { | ||
|
|
@@ -32,22 +34,19 @@ func (c ProjectsClient) List(ctx context.Context) (*ldapi.Projects, error) { | |
| Limit(2). | ||
| Execute() | ||
| if err != nil { | ||
| // TODO: make this nicer | ||
| return nil, err | ||
| } | ||
|
|
||
| return projects, nil | ||
| } | ||
|
|
||
| func ListProjects(ctx context.Context, client2 Client) ([]byte, error) { | ||
| projects, err := client2.List(ctx) | ||
| func ListProjects(ctx context.Context, client Client) ([]byte, error) { | ||
| projects, err := client.List(ctx) | ||
|
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Renamed. |
||
| if err != nil { | ||
| // 401 - should return unauthorized type error with body(?) | ||
| // 404 - should return not found type error with body | ||
| e, ok := err.(ldapi.GenericOpenAPIError) | ||
| if ok { | ||
| return e.Body(), err | ||
| if err.Error() == "401 Unauthorized" { | ||
|
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I couldn't set up a test with a |
||
| return nil, errors.ErrUnauthorized | ||
| } | ||
|
|
||
| return nil, err | ||
| } | ||
|
|
||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
These are handled in other places now.