Skip to content

Commit cd7c315

Browse files
committed
Add the commandeer in Run
1 parent 3a3a8e0 commit cd7c315

File tree

3 files changed

+10
-9
lines changed

3 files changed

+10
-9
lines changed

‎README.md‎

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,14 +7,15 @@ So, [Cobra](https://github.com/spf13/cobra) is a Go CLI library with a feature s
77
I welcome suggestions to improve/simplify this further, but the core idea is that the command graph gets built in one go with a tree of struct pointers implementing a simple `Commander` interface:
88

99
```go
10+
// Commander is the interface that must be implemented by all commands.
1011
type Commander interface {
1112
// The name of the command.
1213
Name() string
1314

1415
// The command execution.
15-
Run(ctx context.Context, args []string) error
16+
Run(ctx context.Context, cd *Commandeer, args []string) error
1617

17-
// Init called on all commands in this tree, before execution, starting from the root.
18+
// Init called on all ancestors and the executing command itself, before execution, starting from the root.
1819
// This is the place to evaluate flags and set up the command.
1920
Init(*Commandeer) error
2021

‎simplecobra.go‎

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -15,9 +15,9 @@ type Commander interface {
1515
Name() string
1616

1717
// The command execution.
18-
Run(ctx context.Context, args []string) error
18+
Run(ctx context.Context, cd *Commandeer, args []string) error
1919

20-
// Init called on all commands in this tree, before execution, starting from the root.
20+
// Init called on all ancestors and the executing command itself, before execution, starting from the root.
2121
// This is the place to evaluate flags and set up the command.
2222
Init(*Commandeer) error
2323

@@ -109,7 +109,7 @@ func (c *Commandeer) compile() error {
109109
c.CobraCommand = &cobra.Command{
110110
Use: c.Command.Name(),
111111
RunE: func(cmd *cobra.Command, args []string) error {
112-
if err := c.Command.Run(cmd.Context(), args); err != nil {
112+
if err := c.Command.Run(cmd.Context(), c, args); err != nil {
113113
return &runErr{err: err}
114114
}
115115
return nil

‎simplecobra_test.go‎

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ func testCommands() *rootCommand {
2525

2626
}
2727

28-
func TestSimplecobra(t *testing.T) {
28+
func TestSimpleCobra(t *testing.T) {
2929
c := qt.New(t)
3030

3131
rootCmd := testCommands()
@@ -191,7 +191,7 @@ func (c *rootCommand) Name() string {
191191
return c.name
192192
}
193193

194-
func (c *rootCommand) Run(ctx context.Context, args []string) error {
194+
func (c *rootCommand) Run(ctx context.Context, cd *simplecobra.Commandeer, args []string) error {
195195
c.ctx = ctx
196196
return nil
197197
}
@@ -235,7 +235,7 @@ func (c *lvl1Command) Name() string {
235235
return c.name
236236
}
237237

238-
func (c *lvl1Command) Run(ctx context.Context, args []string) error {
238+
func (c *lvl1Command) Run(ctx context.Context, cd *simplecobra.Commandeer, args []string) error {
239239
c.ctx = ctx
240240
return nil
241241
}
@@ -271,7 +271,7 @@ func (c *lvl2Command) Name() string {
271271
return c.name
272272
}
273273

274-
func (c *lvl2Command) Run(ctx context.Context, args []string) error {
274+
func (c *lvl2Command) Run(ctx context.Context, cd *simplecobra.Commandeer, args []string) error {
275275
c.ctx = ctx
276276
return nil
277277
}

0 commit comments

Comments
 (0)