Skip to content

Commit a346470

Browse files
committed
cmd/enter: Set failing command as active in main parser
This commit fixes handling of the following two cases: 1. pebble enter subcommand --help 2. pebble enter help subcommand Help messages are handled in main.go when the executed command returns flags.Error{Type:flags.ErrHelp}. When we pass the subcommand's return value back to the main parser, the error handling there will use the currently active command to print the help message. The active command is enter but we want to print the subcommands help message. Whenever the subcommand returns an error value, set it as active command in the main parser, so that the error handling there works with the failing subcommand.
1 parent fec1003 commit a346470

File tree

2 files changed

+76
-2
lines changed

2 files changed

+76
-2
lines changed

cmd/pebble/cmd_enter.go

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,7 @@ type cmdEnter struct {
4747
Positional struct {
4848
Cmd []string `positional-arg-name:"<subcommand>"`
4949
} `positional-args:"yes"`
50+
parser *flags.Parser
5051
}
5152

5253
func init() {
@@ -111,6 +112,7 @@ func (cmd *cmdEnter) Execute(args []string) error {
111112
}
112113

113114
if _, err := parser.ParseArgs(cmd.Positional.Cmd); err != nil {
115+
cmd.parser.Command.Active = parser.Command.Active
114116
return err
115117
}
116118

@@ -133,7 +135,11 @@ func (cmd *cmdEnter) Execute(args []string) error {
133135
}
134136

135137
if enterFlags&enterNoServiceManager != 0 {
136-
return commander.Execute(extraArgs)
138+
if err := commander.Execute(extraArgs); err != nil {
139+
cmd.parser.Command.Active = parser.Command.Active
140+
return err
141+
}
142+
return nil
137143
}
138144

139145
if enterFlags&enterSilenceLogging != 0 && !cmd.Verbose {
@@ -160,6 +166,10 @@ func (cmd *cmdEnter) Execute(args []string) error {
160166

161167
err := commander.Execute(extraArgs)
162168

169+
if err != nil {
170+
cmd.parser.Command.Active = parser.Command.Active
171+
}
172+
163173
if err != nil || enterFlags&enterKeepServiceManager == 0 {
164174
runStop()
165175
}
@@ -170,3 +180,7 @@ func (cmd *cmdEnter) Execute(args []string) error {
170180

171181
return err
172182
}
183+
184+
func (cmd *cmdEnter) setParser(parser *flags.Parser) {
185+
cmd.parser = parser
186+
}

cmd/pebble/cmd_enter_test.go

Lines changed: 61 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -99,7 +99,7 @@ func (s *PebbleSuite) TestEnterUnknownCommand(c *C) {
9999
defer restore()
100100

101101
exitCode := pebble.PebbleMain()
102-
c.Check(s.Stderr(), Equals, "error: unknown command \"foo\", see 'pebble help enter'.\n")
102+
c.Check(s.Stderr(), Equals, "error: unknown command \"foo\", see 'pebble help'.\n")
103103
c.Check(s.Stdout(), Equals, "")
104104
c.Check(exitCode, Equals, 1)
105105
}
@@ -171,3 +171,63 @@ func (s *PebbleSuite) TestEnterExecReadServiceOutputFile(c *C) {
171171
c.Check(s.Stdout(), Equals, "foo\ncat: msg2: No such file or directory\n")
172172
c.Check(exitCode, Equals, 1)
173173
}
174+
175+
func (s *PebbleSuite) TestEnterExecCommandHelpOption(c *C) {
176+
cmd := []string{"pebble", "enter", "exec", "--help"}
177+
restore := fakeArgs(cmd...)
178+
defer restore()
179+
180+
exitCode := pebble.PebbleMain()
181+
// stderr is written to stdout buffer because of "combine stderr" mode,
182+
// see cmd/pebble/cmd_exec.go:163
183+
c.Check(s.Stderr(), Equals, "")
184+
stdout := s.Stdout()
185+
c.Check(stdout, Matches, "^(?s)Usage:\n pebble exec \\[exec-OPTIONS\\] <command>\n.*")
186+
c.Check(stdout, Matches, "(?s).*\\bThe exec command runs a remote command and waits for it to finish\\..*")
187+
c.Check(exitCode, Equals, 0)
188+
}
189+
190+
func (s *PebbleSuite) TestEnterHelpCommandHelpOption(c *C) {
191+
cmd := []string{"pebble", "enter", "help", "--help"}
192+
restore := fakeArgs(cmd...)
193+
defer restore()
194+
195+
exitCode := pebble.PebbleMain()
196+
// stderr is written to stdout buffer because of "combine stderr" mode,
197+
// see cmd/pebble/cmd_exec.go:163
198+
c.Check(s.Stderr(), Equals, "")
199+
stdout := s.Stdout()
200+
c.Check(stdout, Matches, "^(?s)Usage:\n pebble help \\[help-OPTIONS\\] \\[<command>\\.\\.\\.\\]\n.*")
201+
c.Check(stdout, Matches, "(?s).*\\bThe help command displays information about commands\\..*")
202+
c.Check(exitCode, Equals, 0)
203+
}
204+
205+
func (s *PebbleSuite) TestEnterHelpCommandExecArg(c *C) {
206+
cmd := []string{"pebble", "enter", "help", "exec"}
207+
restore := fakeArgs(cmd...)
208+
defer restore()
209+
210+
exitCode := pebble.PebbleMain()
211+
// stderr is written to stdout buffer because of "combine stderr" mode,
212+
// see cmd/pebble/cmd_exec.go:163
213+
c.Check(s.Stderr(), Equals, "")
214+
stdout := s.Stdout()
215+
c.Check(stdout, Matches, "^(?s)Usage:\n pebble exec \\[exec-OPTIONS\\] <command>\n.*")
216+
c.Check(stdout, Matches, "(?s).*\\bThe exec command runs a remote command and waits for it to finish\\..*")
217+
c.Check(exitCode, Equals, 0)
218+
}
219+
220+
func (s *PebbleSuite) TestEnterHelpCommandHelpArg(c *C) {
221+
cmd := []string{"pebble", "enter", "help", "help"}
222+
restore := fakeArgs(cmd...)
223+
defer restore()
224+
225+
exitCode := pebble.PebbleMain()
226+
// stderr is written to stdout buffer because of "combine stderr" mode,
227+
// see cmd/pebble/cmd_exec.go:163
228+
c.Check(s.Stderr(), Equals, "")
229+
stdout := s.Stdout()
230+
c.Check(stdout, Matches, "^(?s)Usage:\n pebble help \\[help-OPTIONS\\] \\[<command>\\.\\.\\.\\]\n.*")
231+
c.Check(stdout, Matches, "(?s).*\\bThe help command displays information about commands\\..*")
232+
c.Check(exitCode, Equals, 0)
233+
}

0 commit comments

Comments
 (0)