chore: Refactor client add tests - #66
Conversation
| return args | ||
| } | ||
|
|
||
| func ArgsValidProjectsCreate() []string { |
There was a problem hiding this comment.
Renamed these, but I think I'm going to replace them with the actual []string in the tests. See the flag cmd tests for examples -- I think they're easier to read.
| func runCreate(client flags.Client) func(*cobra.Command, []string) error { | ||
| return func(cmd *cobra.Command, args []string) error { | ||
| // rebind flags used in other subcommands | ||
| _ = viper.BindPFlag("data", cmd.Flags().Lookup("data")) |
There was a problem hiding this comment.
Rebind the common subcommand flags. We can refactor this later.
| viper.GetString("baseUri"), | ||
| ) | ||
| func runCreate(client flags.Client) func(*cobra.Command, []string) error { | ||
| return func(cmd *cobra.Command, args []string) error { |
There was a problem hiding this comment.
Bind the client in a closure similar to projects.
| } | ||
|
|
||
| updateCmd, err := NewUpdateCmd() | ||
| createCmd, err := NewCreateCmd(client) |
There was a problem hiding this comment.
Swapped these to alphabetize and to check that the last one created binds the shared flags while the rest are set to the zero value.
| ) | ||
|
|
||
| func NewRootCommand(client projects.Client) (*cobra.Command, error) { | ||
| func NewRootCommand(flagsClient flags.Client, projectsClient projects.Client) (*cobra.Command, error) { |
There was a problem hiding this comment.
We probably don't want to try to make one giant API client interface, although maybe we could end up composing one based off the smaller interfaces.
| // client creates an LD API client. It's not set as a field on the struct because the CLI flags | ||
| // are evaluated when running the command, not when executing the program. That means we don't have | ||
| // the flag values until the command's RunE method is called. | ||
| func (c FlagsClient) client(accessToken string, baseURI string) *ldapi.APIClient { |
There was a problem hiding this comment.
We can refactor this and the projects one to a single place.
|
|
||
| type MockClient struct { | ||
| mock.Mock | ||
| AccessToken string |
Refactor the flag cmd tests to inject a mock client.