Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Conversation

@mislav
Copy link
Contributor

@mislav mislav commented May 13, 2020

The most gratifying result of the upgrade is that we get to remove our obsolete hack to generate fish completions.

The downside is that Cobra still hasn't figured out their error handling, and it looks like it's getting worse instead of better. How this issue affects us is this:

gh issue listt  # note the typo
#=> the output of `gh help issue` is shown on stdout ❗️
#=> exit status is 0 ‼️
#=> there is no warning that you've passed an invalid argument ⁉️

Compare that with this, which had always worked:

gh issu  # note the typo
#=> "unknown command" error on stderr 👍
#=> "did you mean ...?" 💯
#=> exit status 1 🥇

Cobra treats error handling in top-level commands differently than in sub-commands, but after digging a lot through Cobra I could not find ways to change this behavior. For example, I tried defining a custom issueCmd.Args checker, but since issueCmd isn't "runnable" (it doesn't have a Run func), Args is downright ignored.

This error handling issue is not necessarily a blocker, but at this point I'm thinking that it would be easier to write our own command dispacher instead of relying on Cobra's buggy one.

@mislav mislav requested review from probablycorey and vilmibm May 13, 2020 18:51
@probablycorey
Copy link
Contributor

It's nice that we can remove the fish hack, but the subcommand error issue seems like a step backwards. Would it be better to open an issue on Cobra and wait before upgrading?

This error handling issue is not necessarily a blocker, but at this point I'm thinking that it would be easier to write our own command dispacher instead of relying on Cobra's buggy one.

I've been wondering about this too. Maybe a good hack day project?

@mislav
Copy link
Contributor Author

mislav commented May 14, 2020

Would it be better to open an issue on Cobra and wait before upgrading?

I don't think it would be wise to wait. There are probably at least two dozen issues in the cobra repository about this and people have attempted to fix it, but I think they were limited due to having to keep backwards compatibility (Cobra is a framework for a lot of apps). I'm much more in favor writing our own dispatcher, since our dispatching needs are very simple.

@mislav mislav added the blocked label May 14, 2020
@vilmibm
Copy link
Contributor

vilmibm commented May 14, 2020

I'm fine with writing our own dispatcher. Should that be a prerequisite for the cobra 1.0 upgrade, though?

@mislav
Copy link
Contributor Author

mislav commented May 15, 2020

Should that be a prerequisite for the cobra 1.0 upgrade, though?

Not necessarily! I think it's acceptable for now to have gh <command> <invalid-subcommand> show help output that doesn't necessarily mention that you've used an invalid word. Users will still get to see the list of valid subcommands.

@mislav mislav removed the blocked label May 15, 2020
@vilmibm
Copy link
Contributor

vilmibm commented May 20, 2020

I'm worried about losing the "did you mean..." functionality until we get our own dispatcher written (which is work that is unscoped and doesn't even have an issue yet). It's small but I think goes a long way when people are trying to learn our tool.

Getting rid of the fish hack is 👍 but really doesn't seem worth the trade of the error handling to me; after thinking about it more I'd rather the new dispatcher be a requirement for this.

I am absolutely in favor of writing our own dispatcher because it would help with error handling as well as things like controlling help output more finely, expanding aliases, and potentially testing. Unfortunately I don't feel like I have a good grasp on the amount of effort and time involved; do you, @mislav ?

@mislav mislav added the blocked label May 22, 2020
@mislav
Copy link
Contributor Author

mislav commented May 22, 2020

I'm worried about losing the "did you mean..." functionality

Note that we never did have "did you mean" for subcommands, only for top-level commands. To illustrate, the current behavior is:

$ gh issue listt
#=> output of `gh help issue` printed to stdout
#=> "subcommand is required" printed to stderr
#=> exit code: 1

At first I thought that writing a dispatcher that only covers our own needs would be easy, but now I'm finding that it would not be so straighforward due to addition of advanced shell completion support in Cobra 1.0 injected via hidden command. Currently it's behind private methods, so if we wanted to port that over to our own dispatcher, we would have to copy-paste some implementation, and then we're basically back to square one like we were with fish completion before.

I think we could do better by overriding HelpFunc to cover the case of mistyped command. I'm going to take a stab at this.

mislav added 2 commits May 22, 2020 18:21
…ands

When executing `gh pr re` (note the incomplete command name), Cobra
would just display the help text for `gh pr` on standard output, exit
with status 0, and not print any message that you have mistyped the
"re" subcommand. Each part of this behavior is wrong.

This workaround makes sure that the helpful error message is printed on
stderr:

    $ gh pr re
    unknown command "re" for "gh pr"

    Did you mean this?
            reopen
            ready
            review

However, the exit status is still 0, whereas it should be non-zero.
Since `HelpFunc` does not return an error argument, we cannot trigger an
error status from this workaround.
@mislav
Copy link
Contributor Author

mislav commented May 22, 2020

Okay, got something working:

$ gh pr re
unknown command "re" for "gh pr"

Did you mean this?
        reopen
        ready
        review

Success tally:

  • prints to stderr
  • actually tells you what you did wrong
  • includes suggestions for similar-sounding command names
  • still exits with status code 0 (it should be non-zero) 🙅

I'm feeling that we can ship like this. 🚀

@mislav mislav removed the blocked label May 22, 2020
@mislav mislav changed the base branch from master to trunk May 27, 2020 11:41
Copy link
Contributor

@vilmibm vilmibm left a comment

Choose a reason for hiding this comment

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

I'm more comfortable with this; thanks for bringing over the subcommand suggestions. We ought to open an issue about the exit code behavior because that could bite or confuse someone down the road but I'm ok with it for now.

@mislav mislav merged commit bfc3e4e into trunk Jun 2, 2020
@mislav mislav deleted the cobra1.0 branch June 2, 2020 08:34
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