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

Fix crash with targets show #303

Closed
wants to merge 1 commit into from

Conversation

doanac
Copy link
Member

@doanac doanac commented Oct 6, 2023

fioctl targets show "" causes the tool to crash. This helps it fail more cleanly.

`fioctl targets show ""` causes the tool to crash. This helps it fail
more cleanly.

Signed-off-by: Andy Doan <andy@foundries.io>
@doanac doanac requested a review from vkhoroz October 6, 2023 21:05
@doanac
Copy link
Member Author

doanac commented Oct 6, 2023

discovered by a customer

Copy link
Member

@vkhoroz vkhoroz left a comment

Choose a reason for hiding this comment

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

I am still puzzled about why cobra did not catch this.
Does it mean that a dozen of other commands can fail on this too?

@doanac
Copy link
Member Author

doanac commented Oct 7, 2023 via email

@vkhoroz
Copy link
Member

vkhoroz commented Oct 9, 2023

@doanac I'm kind of fine with this as a quick fix for the exact problem.

Still, I'm a bit of worried about the generic use case: that we might end up fixing a few dozen of places like this here and there.
So, I was curious if we can apply some generic logic somewhere.

TL;DR
The first idea that came to my mind is: that we literally never expect a user to pass us arguments like "" or '', or ever " ".
So, based on that assumption, I'd like us to have some generic check which iterates over all arguments and prevents that junk from entering.
Unfortunately, Cobra has no such attribute as DenyEmptyArgs or similar.
Fortunately, it has PersistentPreRun & PersistentPreRunE, which we can use to our benefit.

The next idea is to put that check somewhere in the root command: https://github.com/foundriesio/fioctl/blob/main/cmd/root.go#L42 like below:

var rootCmd = &cobra.Command{
    ...
    PersistentPreRunE: func (cmd *cobra.Command, args []string) error {
        for pos, val := range args {
            if len(strings.Trim(val)) == 0 {
                // You can also use PersistentPreRun and subcommand.DieNotNil(...) here
                return fmt.Errorf("Empty values or values containing only white space are not allowed for positional argument at position %d", pos)
            }
        }
        return nil
    },

This way, every single command is covered by this check uniformly, and we don't need to worry about empty values anymore.
What do you think?

@doanac
Copy link
Member Author

doanac commented Oct 9, 2023

@vkhoroz works for me. I'm not going to have a lot of time to hack this week. you want to close this out and push your own fix?

@vkhoroz
Copy link
Member

vkhoroz commented Oct 10, 2023

@doanac ok, let me try to craft smth by EOW. Then I will close this PR.

@vkhoroz
Copy link
Member

vkhoroz commented Oct 11, 2023

Closing this in favor of a more generic fix to all commands: #312

@vkhoroz vkhoroz closed this Oct 11, 2023
@doanac doanac deleted the targets-show-crash branch October 17, 2023 20:42
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.

3 participants