Conversation
|
This pull request has been linked to Shortcut Story #235696: create command to list existing projects. |
| } | ||
|
|
||
| func runList(cmd *cobra.Command, args []string) error { | ||
| // TODO: handle missing flags |
There was a problem hiding this comment.
These are handled in other places now.
| if err != nil { | ||
| os.Exit(1) | ||
| switch { | ||
| case errors.Is(err, errs.ErrUnauthorized): |
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
Nice this makes sense/I like this
| err := rootCmd.MarkPersistentFlagRequired("accessToken") | ||
| if err != nil { | ||
| os.Exit(1) | ||
| panic(err) |
There was a problem hiding this comment.
These errors should never happen, but we should see what they are in case they do.
| @@ -0,0 +1,8 @@ | |||
| package errors | |||
There was a problem hiding this comment.
This package would shadow the standard library errors package, but the only place that should happen is at the highest level of the call stack. If you find you're creating errors.New then they should probably be defined in here instead.
| 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) |
| e, ok := err.(ldapi.GenericOpenAPIError) | ||
| if ok { | ||
| return e.Body(), err | ||
| if err.Error() == "401 Unauthorized" { |
There was a problem hiding this comment.
I couldn't set up a test with a GenericOpenAPIError that has specific data in it because its fields aren't exported. This seems simpler, at least for now.
| rootCmd.SetOut(actual) | ||
| rootCmd.SetErr(actual) | ||
| rootCmd.SetArgs([]string{"hello"}) | ||
| rootCmd.SetArgs([]string{"hello", "--accessToken", "test"}) |
There was a problem hiding this comment.
This is to get the tests to pass. I'll remove the hello world command in another PR.
| 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 { |
There was a problem hiding this comment.
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 := rootCmd.Execute() | ||
| if err != nil { | ||
| os.Exit(1) | ||
| switch { |
There was a problem hiding this comment.
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.
| "baseUri", | ||
| "u", | ||
| "http://localhost:3000", | ||
| "https://app.launchdarkly.com", |
There was a problem hiding this comment.
This changes the default to production so others aren't surprised when using the CLI.
| panic(err) | ||
| } | ||
|
|
||
| rootCmd.SetErrPrefix("") |
There was a problem hiding this comment.
This removes "Error: {msg}" and just shows the error message.
k3llymariee
left a comment
There was a problem hiding this comment.
one comment about validating the URI otherwise LGTM!
| if err != nil { | ||
| os.Exit(1) | ||
| switch { | ||
| case errors.Is(err, errs.ErrUnauthorized): |
There was a problem hiding this comment.
Nice this makes sense/I like this
| _, err := url.ParseRequestURI(viper.GetString("baseUri")) | ||
| if err != nil { | ||
| return errors.ErrInvalidBaseURI |
There was a problem hiding this comment.
should we also make sure that it's a valid LD URI?
There was a problem hiding this comment.
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.
Better error handling for missing flags and invalid responses.
You can see the results by running these commands