Skip to content
This repository was archived by the owner on May 31, 2024. It is now read-only.

fix: fixed arg validation for format#198

Merged
anisatahlil merged 3 commits into
mainfrom
atahlil/formatValidationBug
Dec 1, 2021
Merged

fix: fixed arg validation for format#198
anisatahlil merged 3 commits into
mainfrom
atahlil/formatValidationBug

Conversation

@anisatahlil
Copy link
Copy Markdown
Contributor

Issue #, if available:

Description of Changes
Argument checks for agc configure format was not handled gracefully. This caused stack error to output into the console.

$ agc configure format
panic: runtime error: index out of range [0] with length 0

Description of how you validated changes

$ agc configure format
Error: format value must be provided

Checklist

  • If this change would make any existing documentation invalid, I have included those updates within this PR
  • I have added unit tests that prove my fix is effective or that my feature works
  • I have linted my code before raising the PR
  • Title of this Pull Request follows Conventional Commits standard: https://www.conventionalcommits.org/en/v1.0.0/

By submitting this pull request, I confirm that my contribution is made under the terms of the Apache-2.0 license

@anisatahlil anisatahlil temporarily deployed to slack November 30, 2021 18:12 Inactive
@@ -56,14 +56,14 @@ func BuildConfigureFormatCommand() *cobra.Command {
Short: "Sets default format option for output display of AGC commands. Valid format options are 'text' and 'table'",
Args: cobra.ArbitraryArgs,
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Should this be Args: cobra.ExactArgs(1), to ensure the single argument passed. or is there a case where we would want to have more than one arg passed to this command?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

The feedback from pervious PRs suggested to move away from Args: cobra.ExactArgs(1) to give customers a more readable error when argument is not provided.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Linking to the discussion on the other thread https://github.com/aws/amazon-genomics-cli/pull/157/files/d6efecb9dcbba2d8736b81db8c9292f507e336e4#r745907823

i can take an argument on better error message, for Error: accepts 1 arg(s), received 0 vs a single format value must be provided does the latter really looks better? If we're going to the extent of avoiding cobra provided logic and building our own here could we give a better message then?

with us having two allowed values for the command i think we could do something like

    Args: cobra.OnlyValidArgs,
    ValidArgs: []string{"text", "table"},

or something like that could be an option too, potentially allowing us to use the values in autocomplete too?

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

"Error: accepts 1 arg(s), received 0" only tells the customer the what while "Error: a single format value must be provided" tells the customer the why as well. This means that for a customer to resolve the issue, they don't need to type "-h" to know what the values are.

Ideally, we could populated the valid args like you mention or generate the message with the valid options like we do with some of our other commands.


func (o *formatContextOpts) Validate() error {
if o.formatContextVars.format == "" {
func (o *formatContextOpts) Validate(args []string) error {
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Should we be passing an array in here if we're only reading args[0] out of it later on?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

That function is handling validation which also checks if the array is empty to provide users with a readable error message

func (o *formatContextOpts) Validate() error {
if o.formatContextVars.format == "" {
func (o *formatContextOpts) Validate(args []string) error {
if len(args) == 0 {
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

This should be != 1 with a message similar to "a single format value must be provided"

@anisatahlil anisatahlil merged commit 0b68019 into main Dec 1, 2021
@anisatahlil anisatahlil deleted the atahlil/formatValidationBug branch December 1, 2021 17:34
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants