-
Notifications
You must be signed in to change notification settings - Fork 9
feat!: update cli commands #211
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
Conversation
Pull Request Test Coverage Report for Build 4363037925Warning: This coverage report may be inaccurate.This pull request's base commit is no longer the HEAD commit of its target branch. This means it includes changes from outside the original pull request, including, potentially, unrelated coverage changes.
Details
💛 - Coveralls |
|
@sudo-suhas @StewartJingga can you take a look at this. If looks good we can merge this. This follows similar guidelines as we are using in Guardian for CLI. |
|
@ravisuhag I am on leave till 24th, will check once I am back. |
Makefile
Outdated
|
|
||
| clean-doc: | ||
| @echo "> cleaning up auto-generated docs" | ||
| @rm -rf ./docs/docs/reference/cli |
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.
| @rm -rf ./docs/docs/reference/cli | |
| @rm -rf ./docs/docs/reference/cli.md |
| Use: "view <id>", | ||
| Short: "view asset for the given ID", | ||
| Example: heredoc.Doc(` | ||
| $ compass asset view <id> |
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.
Can we update supported commands to use urn instead of ID?
cli/assets.go
Outdated
| } | ||
|
|
||
| func listAllTypesCommand() *cobra.Command { | ||
| var types, services, data, q, q_fields string |
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.
Lets follow Go's naming convention and rename q_fields to qFields or just fields (context conveys that it is for the query)
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.
Can we keep the type for data as map[string]string itself? We can use cmd.Flags().StringToStringVarP(...) to handle the unmarshaling.
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.
One obvious improvement would be to directly use an instance of compassv1beta1.GetAllTypesRequest and pass the pointers to it's field so that there is no additional step required for setting these fields and create a new request.
cli/assets.go
Outdated
|
|
||
| func makeGetAllTypesRequest(types, services, data, q, q_fields string) *compassv1beta1.GetAllTypesRequest { | ||
| newReq := &compassv1beta1.GetAllTypesRequest{} | ||
| if types != "" { |
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.
Is there any value in having this if check? compassv1beta1.GetAllTypesRequest.Types is a string field so setting it to "" is not a problem. Similarly for other fields being set conditionally, is it required?
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.
Was already there in the current CLI code, will change if it's required
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.
Lets refactor where it makes sense.
cli/assets.go
Outdated
| report := [][]string{} | ||
| report = append(report, []string{"NAME", "COUNT"}) |
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.
| report := [][]string{} | |
| report = append(report, []string{"NAME", "COUNT"}) | |
| report := [][]string{{"NAME", "COUNT"}} |
cli/server.go
Outdated
| Short: "Serve gRPC & HTTP service", | ||
| Long: heredoc.Doc(`Serve gRPC & HTTP on a port defined in PORT env var.`), | ||
| Aliases: []string{"server", "start"}, | ||
| func serverCmd() *cobra.Command { |
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.
Currently not able to test these since the config loading is broken.
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.
can you once again try with --config flag to use the file from current directory. Alternatively you can use the config file created after init command.
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.
will test once the current set of review comments are addressed.
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.
not sure if you realised but #211 (comment) means that loading config is also broken.
cli/server.go
Outdated
| } | ||
|
|
||
| esClient, err := initElasticsearch(logger, config.Elasticsearch) | ||
| esClient, err := initElasticsearch(logger) |
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.
Pass in the configs explicitly.
cli/server.go
Outdated
| } | ||
|
|
||
| func runServer(config Config) error { | ||
| func runServer() error { |
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.
Undo this change please.
cli/version.go
Outdated
| Short: "Print version information", | ||
| RunE: func(cmd *cobra.Command, args []string) error { | ||
| if Version == "" { | ||
| fmt.Println(term.Yellow("Version history not available")) |
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.
Any reason we are printing 'Version history'? Shouldn't it just be 'Version'?
main.go
Outdated
|
|
||
| rootCmd := cli.New(cliConfig) | ||
|
|
||
| if cmd, err := rootCmd.ExecuteC(); err != nil { |
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.
Can we run with ExecuteContextC instead? We can pass in a context created with signal.NotifyContext.
| flagErr := strings.HasPrefix(err.Error(), "unknown flag") | ||
| sflagErr := strings.HasPrefix(err.Error(), "unknown shorthand flag") | ||
|
|
||
| if cmdErr || flagErr || sflagErr { |
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.
Can you double-check the handling for this? Usage is being printed twice currently:
$ ./compass config --test
Usage: compass config <command> [flags]
Available commands:
init
list
unknown flag: --test
Usage: compass config <command> [flags]
Available commands:
init
list
cli/config.go
Outdated
| opts = append(opts, | ||
| config.WithPath("./"), | ||
| config.WithName("compass"), | ||
| config.WithName("compass.yaml"), |
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.
@sudo-suhas laoding the options WithName("compass") gives an error error in reading config: yaml when the conifg file compass.yaml is missing in the root folder.
Instead it should be giving an error, file not found. Currently it is trying to use the compass executable while trying to read the configs when the file is missing.
I also tried to add the WithType("yaml") but it still gives the same error. Can we rename the compass.yaml to something like .conifg.yaml.
I need the correct error to be handled for searching for file made from the init command, which will be called only when the config file is missing in the root.
The extention "yaml" in line 118 will be discarded once we go ahead with something which throws the right error.
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.
I am not clear on why we care about the exact error message being returned. We were anyway proceeding with defaults when the config file was not found.
cli/config.go
Outdated
| Example: heredoc.Doc(` | ||
| $ compass config init | ||
| `), | ||
| // SilencePersistentFlag: true, |
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.
clean this up?
cli/config.go
Outdated
| _ = yaml.NewEncoder(os.Stdout).Encode(*cfg) | ||
| return nil |
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.
return yaml.NewEncoder(os.Stdout).Encode(*cfg) ?
| func LoadConfig() (*Config, error) { | ||
| var cfg Config | ||
| err := cmdx.SetConfig("compass").Load(&cfg) | ||
| if err != nil { | ||
| if errors.As(err, &config.ConfigFileNotFoundError{}) { | ||
| return LoadFromCurrentDir() | ||
| } | ||
| return &cfg, err | ||
| } | ||
| return &cfg, nil | ||
| } | ||
|
|
||
| func LoadFromCurrentDir() (*Config, error) { | ||
| var cfg Config | ||
| var opts []config.LoaderOption | ||
|
|
||
| opts = append(opts, | ||
| config.WithPath("./"), | ||
| config.WithName("compass.yaml"), | ||
| config.WithEnvKeyReplacer(".", "_"), | ||
| config.WithEnvPrefix("COMPASS"), | ||
| ) | ||
|
|
||
| if err := config.NewLoader(opts...).Load(&cfg); err != nil { | ||
| if errors.As(err, &config.ConfigFileNotFoundError{}) { | ||
| return cfg, nil | ||
| return &cfg, ErrConfigNotFound | ||
| } | ||
| return cfg, err | ||
| return &cfg, err | ||
| } | ||
| return cfg, nil | ||
| return &cfg, nil | ||
| } |
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.
The current implementation looks like we give lower priority to the config file in the current directory, I feel we should do the opposite, and try to load from the current first if fails, we can fall back to the rest of the pre-defined directories.
| } | ||
|
|
||
| func serverStartCommand(cfg *Config) *cobra.Command { | ||
|
|
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.
nit: I see couple of unnecessary new lines, for example in serverMigrateCommand, func New(cliConfig *Config) *cobra.Comman, and here
|
LGTM |
|
@Chief-Rishab Can you update the docs PR according to the change made here. |
Add/Update CLI commands
configscmd to supportconfig initandconfig list)compass serveandcompass migratecommand tocompass server <subcommands>BREAKING CHANGES:
serveandmigratecommand changed toserver start,server migrate,configschanged toconfig initandconfig list