Skip to content
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

Defect: CLI - Cobra commands print out error twice and disoplays help menu #1677

Closed
2 tasks
techcobweb opened this issue Dec 1, 2023 · 3 comments
Closed
2 tasks
Assignees

Comments

@techcobweb
Copy link
Contributor

techcobweb commented Dec 1, 2023

Story

As a user, when I run a cli command with an expected error returned, I should only receive the error once without the cobra help menu being displayed

Background

When localgalasactl resources apply -f invalidYamlFile.yaml --log - is run, (where invalidYamlFile.yaml should return errors), the error expected is printed twice, and the cobra help menu is printed as well.

It is suspected that the RunE function when creating cobra commands causes the cobra help menu to be displayed
It is suspected that the finalWordHandler prints the error again, hence the second error statement.

savvas@BugsLife cli % localgalasactl resources apply -f properties.yaml
Error: GAL1114E: The endpoint has responded with the following errors: 
GAL5400E: Error occured when trying to execute request '{"apiversion":"galasa-dev/v1alpha1","data":{"value":"strawberry"},"kind":"GalasaProperty","metadata":{"name":"doughnutfilling","namespace":"framework"}}'. Please check your request parameters or report the problem to your Galasa Ecosystem owner.
GAL5024E: Error occured because the Galasa Property is invalid. The 'namespace' field 'null' is not structured properly.
GAL5024E: Error occured because the Galasa Property is invalid. The 'value' field 'null' is not structured properly.
Usage:
  galasactl resources apply [flags]

Aliases:
  apply, resources apply

Flags:
  -h, --help   Displays the options for the resources apply command.

Global Flags:
  -b, --bootstrap string    Bootstrap URL. Should start with 'http://' or 'file://'. If it starts with neither, it is assumed to be a fully-qualified path. If missing, it defaults to use the 'bootstrap.properties' file in your GALASA_HOME. Example: http://example.com/bootstrap, file:///user/myuserid/.galasa/bootstrap.properties , file://C:/Users/myuserid/.galasa/bootstrap.properties
  -f, --file string         Fully qualified path to the file to action against the ecosystem.Example: /user/myuserid/Documents/resources.yaml , C:/Users/myuserid/Documents/resources.yaml
      --galasahome string   Path to a folder where Galasa will read and write files and configuration settings. The default is '${HOME}/.galasa'. This overrides the GALASA_HOME environment variable which may be set instead.
  -l, --log string          File to which log information will be sent. Any folder referred to must exist. An existing file will be overwritten. Specify "-" to log to stderr. Defaults to not logging.

GAL1114E: The endpoint has responded with the following errors: 
GAL5400E: Error occured when trying to execute request '{"apiversion":"galasa-dev/v1alpha1","data":{"value":"strawberry"},"kind":"GalasaProperty","metadata":{"name":"doughnutfilling","namespace":"framework"}}'. Please check your request parameters or report the problem to your Galasa Ecosystem owner.
GAL5024E: Error occured because the Galasa Property is invalid. The 'namespace' field 'null' is not structured properly.
GAL5024E: Error occured because the Galasa Property is invalid. The 'value' field 'null' is not structured properly.

Tasks

  • Find out if RunE is really the cause of the cobra help command being printed

  • Change finalWordHandler so all the error handling should be within commands - less stuff on the cobra side

@techcobweb techcobweb converted this from a draft issue Dec 1, 2023
@KirbyKatcher KirbyKatcher changed the title CLI: Cobra commands print out error twice and disoplays help menu Defect: CLI - Cobra commands print out error twice and disoplays help menu Dec 1, 2023
@techcobweb
Copy link
Contributor Author

This issue went to the cobra folks: spf13/cobra#340

It suggests we put

SilenceUsage: true,
SilenceError: true,

into the root command, and that suppresses usage when you return an error.

I will try it now and see if that sorts things out.

@Akyiaa Akyiaa self-assigned this Dec 1, 2023
@techcobweb
Copy link
Contributor Author

Hmm. Setting SilenceUsage is OK, in that it supresses usage.

The trouble is with SilenceErrors. It doesn't even report errors it finds then. And errors don't exit with 1. Or return an error from the framework.

Seems they wanted to do this but were too scared of breaking users who used those APIs (we know that problem).

When cobra displays errors, it says "Error : GAL002E ..." for example. So maybe we live with that , and don't display the galasa errors again in the final word.

@techcobweb techcobweb moved this from 🆕 New to 🏗 2 In progress in galasa-dev team Dec 5, 2023
@techcobweb
Copy link
Contributor Author

So in the end I set UilentUsage to be true, and let Cobra report the errors to stderr.
It's far from ideal, but good enough for now I hope.

@techcobweb techcobweb moved this from 🏗 2 In progress to Done - in release in galasa-dev team Dec 11, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
Archived in project
Development

No branches or pull requests

2 participants