chore: Refactor API client and pass into constructor fns - #64
Conversation
| ) | ||
|
|
||
| func NewCreateCmd() *cobra.Command { | ||
| func NewCreateCmd() (*cobra.Command, error) { |
There was a problem hiding this comment.
Constructor fns can return errors, so we bubble those up to the top level.
| ) | ||
|
|
||
| func NewCreateCmd() *cobra.Command { | ||
| func NewCreateCmd(client projects.Client) (*cobra.Command, error) { |
There was a problem hiding this comment.
This passes in the client that's stored in a closure. We need to do this because of how late cobra and viper bind the flags which aren't set yet in the root command. Since we pass in the access token, we don't have the value until we call the subcommand's Run function.
There was a problem hiding this comment.
do we need to do this for flags update/create as well? or will that be in a follow up pr
There was a problem hiding this comment.
Yeah, I'll do it in another PR. This was already getting pretty big.
|
|
||
| cmd.PersistentFlags().StringVarP( | ||
| &accessToken, | ||
| cmd.PersistentFlags().StringP( |
There was a problem hiding this comment.
This changes to a simpler function since we don't need the variable set.
| type Client interface { | ||
| Create(ctx context.Context, name string, key string) ([]byte, error) | ||
| List(ctx context.Context) ([]byte, error) | ||
| Create(ctx context.Context, accessToken, baseURI, name, key string) ([]byte, error) |
There was a problem hiding this comment.
These calls need more info since we can't pass them in until we call these methods.
k3llymariee
left a comment
There was a problem hiding this comment.
just a couple of q's to make sure i follow what's going on 😅
| ) | ||
|
|
||
| func NewCreateCmd() *cobra.Command { | ||
| func NewCreateCmd(client projects.Client) (*cobra.Command, error) { |
There was a problem hiding this comment.
do we need to do this for flags update/create as well? or will that be in a follow up pr
| ) | ||
|
|
||
| func newRootCommand() *cobra.Command { | ||
| func NewRootCommand(client projects.Client) (*cobra.Command, error) { |
There was a problem hiding this comment.
guessing this will also be updated to a more generic client (not specific to projects) in a follow up PR as well?
There was a problem hiding this comment.
Right. Making the changes to the flags commands will force that.
Refactoring the API client so we can pass it in to the commands. Now we can test the commands from the root without making real API calls.
This is the first of a few PRs to do all that and backfill tests.