Skip to content

Commit 68154de

Browse files
committed
Add a SimpleCommand func and example
1 parent 8cb9a9f commit 68154de

File tree

2 files changed

+73
-25
lines changed

2 files changed

+73
-25
lines changed

‎cobrakai.go‎

Lines changed: 45 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,20 @@ import (
77
"github.com/spf13/pflag"
88
)
99

10+
// Executer is the execution entry point.
11+
// The args are usually filled with os.Args[1:].
12+
type Executer interface {
13+
Execute(ctx context.Context, args []string) (*Commandeer, error)
14+
}
15+
16+
// Commander is the interface that must be implemented by all commands.
17+
type Commander interface {
18+
Name() string
19+
Run(ctx context.Context, args []string) error
20+
AddFlagsLocal(*pflag.FlagSet)
21+
AddFlagsPersistent(*pflag.FlagSet)
22+
}
23+
1024
type root struct {
1125
c *Commandeer
1226
}
@@ -35,11 +49,9 @@ func (r *root) Execute(ctx context.Context, args []string) (*Commandeer, error)
3549

3650
// Commandeer holds the state of a command and its subcommands.
3751
type Commandeer struct {
38-
Command Commander
39-
commandeers []*Commandeer
40-
41-
// compiled
52+
Command Commander
4253
CobraCommand *cobra.Command
54+
commandeers []*Commandeer
4355
}
4456

4557
func (c *Commandeer) compile() error {
@@ -51,8 +63,8 @@ func (c *Commandeer) compile() error {
5163
return c.Command.Run(cmd.Context(), args)
5264
},
5365
}
54-
// THere's a LocalFlags set in Cobra which one would beliee would be the right place to put these flags,
55-
// but theat doesn't work and there's several related open issues.
66+
// There's a LocalFlags set in Cobra which one would believe would be the right place to put these flags,
67+
// but that doesn't work and there's several related open issues.
5668
// This is how the docs say to do it and also where Hugo puts local flags.
5769
c.Command.AddFlagsLocal(c.CobraCommand.Flags())
5870
c.Command.AddFlagsPersistent(c.CobraCommand.PersistentFlags())
@@ -68,20 +80,6 @@ func (c *Commandeer) compile() error {
6880
return nil
6981
}
7082

71-
// Executer is the execution entry point.
72-
// The args are usually filled with os.Args[1:].
73-
type Executer interface {
74-
Execute(ctx context.Context, args []string) (*Commandeer, error)
75-
}
76-
77-
// Commander is the interface that must be implemented by all commands.
78-
type Commander interface {
79-
Name() string
80-
Run(ctx context.Context, args []string) error
81-
AddFlagsLocal(*pflag.FlagSet)
82-
AddFlagsPersistent(*pflag.FlagSet)
83-
}
84-
8583
// WithCommandeer allows chaining of commandeers.
8684
type WithCommandeer func(*Commandeer)
8785

@@ -111,3 +109,30 @@ func C(command Commander, wcs ...WithCommandeer) func(*Commandeer) {
111109
}
112110
}
113111
}
112+
113+
// SimpleCommand creates a simple command that does not take any flags.
114+
func SimpleCommand(name string, run func(ctx context.Context, args []string) error) Commander {
115+
return &simpleCommand{
116+
name: name,
117+
run: run,
118+
}
119+
}
120+
121+
type simpleCommand struct {
122+
name string
123+
run func(ctx context.Context, args []string) error
124+
}
125+
126+
func (s *simpleCommand) Name() string {
127+
return s.name
128+
}
129+
130+
func (s *simpleCommand) Run(ctx context.Context, args []string) error {
131+
return s.run(ctx, args)
132+
}
133+
134+
func (s *simpleCommand) AddFlagsLocal(*pflag.FlagSet) {
135+
}
136+
137+
func (s *simpleCommand) AddFlagsPersistent(*pflag.FlagSet) {
138+
}

‎cobrakai_test.go‎

Lines changed: 28 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,10 @@ package cobrakai_test
33
import (
44
"context"
55
"fmt"
6+
"log"
67
"testing"
78

8-
ck "github.com/bep/cobrakai"
9+
"github.com/bep/cobrakai"
910
qt "github.com/frankban/quicktest"
1011
"github.com/spf13/pflag"
1112
)
@@ -19,12 +20,12 @@ func TestCobraKai(t *testing.T) {
1920
)
2021

2122
c := qt.New(t)
22-
r, err := ck.R(
23+
r, err := cobrakai.R(
2324
&testComand1{name: "hugo"},
24-
ck.C(fooCommand,
25-
ck.C(fooBazCommand),
25+
cobrakai.C(fooCommand,
26+
cobrakai.C(fooBazCommand),
2627
),
27-
ck.C(barCommand),
28+
cobrakai.C(barCommand),
2829
)
2930
c.Assert(err, qt.IsNil)
3031

@@ -52,6 +53,28 @@ func TestCobraKai(t *testing.T) {
5253

5354
}
5455

56+
func ExampleSimpleCommand() {
57+
r, err := cobrakai.R(
58+
// If you need flags, implement cobrakai.Commander.
59+
cobrakai.SimpleCommand("root", func(ctx context.Context, args []string) error { fmt.Print("run root "); return nil }),
60+
cobrakai.C(cobrakai.SimpleCommand("sub1", func(ctx context.Context, args []string) error { fmt.Print("run sub1"); return nil })),
61+
cobrakai.C(cobrakai.SimpleCommand("sub2", func(ctx context.Context, args []string) error { fmt.Print("run sub2"); return nil })),
62+
)
63+
64+
if err != nil {
65+
log.Fatal(err)
66+
}
67+
68+
if _, err := r.Execute(context.Background(), []string{""}); err != nil {
69+
log.Fatal(err)
70+
}
71+
if _, err := r.Execute(context.Background(), []string{"sub1"}); err != nil {
72+
log.Fatal(err)
73+
}
74+
// Output: run root run sub1
75+
76+
}
77+
5578
type testComand1 struct {
5679
persistentFlagName string
5780
localFlagName string

0 commit comments

Comments
 (0)