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

cmd/snap: don't run install on 'snap --help install' #6963

Merged
merged 1 commit into from Jun 7, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
14 changes: 12 additions & 2 deletions cmd/snap/cmd_help.go
Expand Up @@ -48,8 +48,18 @@ func addHelp(parser *flags.Parser) error {
// on which help is being requested (like "snap foo
// --help", active is foo), or nil in the toplevel.
if parser.Command.Active == nil {
// toplevel --help will get handled via ErrCommandRequired
return nil
// this means *either* a bare 'snap --help',
// *or* 'snap --help command'
//
// If we return nil in the first case go-flags
// will throw up an ErrCommandRequired on its
// own, but in the second case it'll go on to
// run the command, which is very unexpected.
//
// So we force the ErrCommandRequired here.

// toplevel --help gets handled via ErrCommandRequired
return &flags.Error{Type: flags.ErrCommandRequired}
}
// not toplevel, so ask for regular help
return &flags.Error{Type: flags.ErrHelp}
Expand Down
10 changes: 1 addition & 9 deletions cmd/snap/cmd_help_test.go
Expand Up @@ -41,6 +41,7 @@ func (s *SnapSuite) TestHelpPrintsHelp(c *check.C) {
{"snap", "help"},
{"snap", "--help"},
{"snap", "-h"},
{"snap", "--help", "install"},
} {
s.ResetStdStreams()

Expand Down Expand Up @@ -195,12 +196,3 @@ func (s *SnapSuite) TestBadSub(c *check.C) {
err := snap.RunMain()
c.Assert(err, check.ErrorMatches, `unknown command "brotato", see 'snap help debug'.`)
}

func (s *SnapSuite) TestWorseSub(c *check.C) {
origArgs := os.Args
defer func() { os.Args = origArgs }()
os.Args = []string{"snap", "-h", "debug", "brotato"}

err := snap.RunMain()
c.Assert(err, check.ErrorMatches, `unknown command "brotato", see 'snap help debug'.`)
}