Skip to content

Conversation

@Chief-Rishab
Copy link
Member

@Chief-Rishab Chief-Rishab commented Feb 22, 2023

Add/Update CLI commands

  • Style: update core commands according to the salt configurations
  • Reference
  • Shell auto completion,
  • Environment
  • Config init (Refactor configs cmd to support config init and config list)
  • Version
  • Refactor compass serve and compass migrate command to compass server <subcommands>
  • Asset: getalltypes, stargazers, version history, get asset by version
  • Current user: get user starred assets, star asset, unstar asset, get my discussions
  • Make file to auto-generate docs, fix version flags
  • Fix the client config over-riding from config flag ( currently the client config from root is not getting over-ridden)

BREAKING CHANGES: serve and migrate command changed to server start, server migrate,
configs changed to config init and config list

@Chief-Rishab Chief-Rishab marked this pull request as ready for review February 22, 2023 10:19
@coveralls
Copy link

coveralls commented Feb 22, 2023

Pull Request Test Coverage Report for Build 4363037925

Warning: 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

  • 0 of 0 changed or added relevant lines in 0 files are covered.
  • No unchanged relevant lines lost coverage.
  • Overall coverage decreased (-0.5%) to 85.635%

Totals Coverage Status
Change from base Build 4321301974: -0.5%
Covered Lines: 4644
Relevant Lines: 5423

💛 - Coveralls

@ravisuhag
Copy link
Member

@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.

@sudo-suhas
Copy link
Contributor

@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
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
@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>
Copy link
Contributor

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
Copy link
Contributor

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)

Copy link
Contributor

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.

Copy link
Contributor

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 != "" {
Copy link
Contributor

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?

Copy link
Member Author

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

Copy link
Contributor

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
Comment on lines 313 to 314
report := [][]string{}
report = append(report, []string{"NAME", "COUNT"})
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
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 {
Copy link
Contributor

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.

Copy link
Member Author

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.

Copy link
Contributor

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.

Copy link
Contributor

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)
Copy link
Contributor

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 {
Copy link
Contributor

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"))
Copy link
Contributor

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 {
Copy link
Contributor

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 {
Copy link
Contributor

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"),
Copy link
Member Author

@Chief-Rishab Chief-Rishab Mar 1, 2023

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.

Copy link
Contributor

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,
Copy link
Member

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
Comment on lines 73 to 74
_ = yaml.NewEncoder(os.Stdout).Encode(*cfg)
return nil
Copy link
Member

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) ?

Comment on lines 103 to 133
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
}
Copy link
Member

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 {

Copy link
Member

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

@kushsharma
Copy link
Member

LGTM

@ravisuhag ravisuhag merged commit 4524891 into main Mar 9, 2023
@ravisuhag ravisuhag deleted the update-cli branch March 9, 2023 14:37
@ravisuhag
Copy link
Member

@Chief-Rishab Can you update the docs PR according to the change made here.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

6 participants