Skip to content

Commit 07f92dd

Browse files
committed
Revise the Commander (again)
1 parent ff3f5a8 commit 07f92dd

File tree

3 files changed

+27
-25
lines changed

3 files changed

+27
-25
lines changed

README.md

+8-8
Original file line numberDiff line numberDiff line change
@@ -13,17 +13,17 @@ type Commander interface {
1313
// The name of the command.
1414
Name() string
1515

16-
// The command execution.
17-
Run(ctx context.Context, cd *Commandeer, args []string) error
16+
// Init is called when the cobra command is created.
17+
// This is where the flags, short and long description etc. can be added.
18+
Init(*Commandeer) error
1819

19-
// Init called on all ancestors and the executing command itself, before execution, starting from the root.
20+
// PreRun called on all ancestors and the executing command itself, before execution, starting from the root.
2021
// This is the place to evaluate flags and set up the this Commandeer.
21-
// The runner Commandeer holds the currently running command, which will be Init last.
22-
Init(this, runner *Commandeer) error
22+
// The runner Commandeer holds the currently running command, which will be PreRun last.
23+
PreRun(this, runner *Commandeer) error
2324

24-
// WithCobraCommand is called when the cobra command is created.
25-
// This is where the flags, short and long description etc. are added.
26-
WithCobraCommand(*cobra.Command) error
25+
// The command execution.
26+
Run(ctx context.Context, cd *Commandeer, args []string) error
2727

2828
// Commands returns the sub commands, if any.
2929
Commands() []Commander

simplecobra.go

+10-10
Original file line numberDiff line numberDiff line change
@@ -14,17 +14,17 @@ type Commander interface {
1414
// The name of the command.
1515
Name() string
1616

17-
// The command execution.
18-
Run(ctx context.Context, cd *Commandeer, args []string) error
17+
// Init is called when the cobra command is created.
18+
// This is where the flags, short and long description etc. can be added.
19+
Init(*Commandeer) error
1920

20-
// Init called on all ancestors and the executing command itself, before execution, starting from the root.
21+
// PreRun called on all ancestors and the executing command itself, before execution, starting from the root.
2122
// This is the place to evaluate flags and set up the this Commandeer.
22-
// The runner Commandeer holds the currently running command, which will be Init last.
23-
Init(this, runner *Commandeer) error
23+
// The runner Commandeer holds the currently running command, which will be PreRun last.
24+
PreRun(this, runner *Commandeer) error
2425

25-
// WithCobraCommand is called when the cobra command is created.
26-
// This is where the flags, short and long description etc. are added.
27-
WithCobraCommand(*cobra.Command) error
26+
// The command execution.
27+
Run(ctx context.Context, cd *Commandeer, args []string) error
2828

2929
// Commands returns the sub commands, if any.
3030
Commands() []Commander
@@ -89,7 +89,7 @@ func (c *Commandeer) init() error {
8989
// Init all of them starting from the root.
9090
for i := len(ancestors) - 1; i >= 0; i-- {
9191
cd := ancestors[i]
92-
if err := cd.Command.Init(cd, c); err != nil {
92+
if err := cd.Command.PreRun(cd, c); err != nil {
9393
return err
9494
}
9595
}
@@ -124,7 +124,7 @@ func (c *Commandeer) compile() error {
124124
}
125125

126126
// This is where the flags, short and long description etc. are added
127-
if err := c.Command.WithCobraCommand(c.CobraCommand); err != nil {
127+
if err := c.Command.Init(c); err != nil {
128128
return err
129129
}
130130

simplecobra_test.go

+9-7
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,6 @@ import (
1010

1111
"github.com/bep/simplecobra"
1212
qt "github.com/frankban/quicktest"
13-
"github.com/spf13/cobra"
1413
)
1514

1615
func testCommands() *rootCommand {
@@ -267,7 +266,7 @@ func (c *rootCommand) Commands() []simplecobra.Commander {
267266
return c.commands
268267
}
269268

270-
func (c *rootCommand) Init(this, runner *simplecobra.Commandeer) error {
269+
func (c *rootCommand) PreRun(this, runner *simplecobra.Commandeer) error {
271270
c.isInit = true
272271
c.persistentFlagNameC = c.persistentFlagName + "_rootCommand_compiled"
273272
c.localFlagNameC = c.localFlagName + "_rootCommand_compiled"
@@ -288,10 +287,11 @@ func (c *rootCommand) Run(ctx context.Context, cd *simplecobra.Commandeer, args
288287
return nil
289288
}
290289

291-
func (c *rootCommand) WithCobraCommand(cmd *cobra.Command) error {
290+
func (c *rootCommand) Init(cd *simplecobra.Commandeer) error {
292291
if c.failWithCobraCommand {
293292
return errors.New("failWithCobraCommand")
294293
}
294+
cmd := cd.CobraCommand
295295
localFlags := cmd.Flags()
296296
persistentFlags := cmd.PersistentFlags()
297297

@@ -323,7 +323,7 @@ func (c *lvl1Command) Commands() []simplecobra.Commander {
323323
return c.commands
324324
}
325325

326-
func (c *lvl1Command) Init(this, runner *simplecobra.Commandeer) error {
326+
func (c *lvl1Command) PreRun(this, runner *simplecobra.Commandeer) error {
327327
if c.failInit {
328328
return fmt.Errorf("failInit")
329329
}
@@ -342,10 +342,11 @@ func (c *lvl1Command) Run(ctx context.Context, cd *simplecobra.Commandeer, args
342342
return nil
343343
}
344344

345-
func (c *lvl1Command) WithCobraCommand(cmd *cobra.Command) error {
345+
func (c *lvl1Command) Init(cd *simplecobra.Commandeer) error {
346346
if c.failWithCobraCommand {
347347
return errors.New("failWithCobraCommand")
348348
}
349+
cmd := cd.CobraCommand
349350
cmd.DisableSuggestions = c.disableSuggestions
350351
localFlags := cmd.Flags()
351352
localFlags.StringVar(&c.localFlagName, "localFlagName", "", "set localFlagName for lvl1Command")
@@ -366,7 +367,7 @@ func (c *lvl2Command) Commands() []simplecobra.Commander {
366367
return nil
367368
}
368369

369-
func (c *lvl2Command) Init(this, runner *simplecobra.Commandeer) error {
370+
func (c *lvl2Command) PreRun(this, runner *simplecobra.Commandeer) error {
370371
c.isInit = true
371372
c.rootCmd = this.Root.Command.(*rootCommand)
372373
c.parentCmd = this.Parent.Command.(*lvl1Command)
@@ -382,7 +383,8 @@ func (c *lvl2Command) Run(ctx context.Context, cd *simplecobra.Commandeer, args
382383
return nil
383384
}
384385

385-
func (c *lvl2Command) WithCobraCommand(cmd *cobra.Command) error {
386+
func (c *lvl2Command) Init(cd *simplecobra.Commandeer) error {
387+
cmd := cd.CobraCommand
386388
localFlags := cmd.Flags()
387389
localFlags.StringVar(&c.localFlagName, "localFlagName", "", "set localFlagName for lvl2Command")
388390
return nil

0 commit comments

Comments
 (0)