Skip to content

Commit 2c8df91

Browse files
committed
Shrink the command interface
1 parent 9a726c8 commit 2c8df91

File tree

3 files changed

+24
-32
lines changed

3 files changed

+24
-32
lines changed

‎cobrakai.go‎

Lines changed: 11 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@ import (
44
"context"
55

66
"github.com/spf13/cobra"
7-
"github.com/spf13/pflag"
87
)
98

109
// Executer is the execution entry point.
@@ -17,8 +16,7 @@ type Executer interface {
1716
type Commander interface {
1817
Name() string
1918
Run(ctx context.Context, args []string) error
20-
AddFlagsLocal(*pflag.FlagSet)
21-
AddFlagsPersistent(*pflag.FlagSet)
19+
WithCobraCommand(*cobra.Command) error
2220
}
2321

2422
type root struct {
@@ -56,18 +54,14 @@ type Commandeer struct {
5654

5755
func (c *Commandeer) compile() error {
5856
c.CobraCommand = &cobra.Command{
59-
Use: c.Command.Name(),
60-
Short: "TODO",
61-
Long: "TODO",
57+
Use: c.Command.Name(),
6258
RunE: func(cmd *cobra.Command, args []string) error {
6359
return c.Command.Run(cmd.Context(), args)
6460
},
6561
}
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.
68-
// This is how the docs say to do it and also where Hugo puts local flags.
69-
c.Command.AddFlagsLocal(c.CobraCommand.Flags())
70-
c.Command.AddFlagsPersistent(c.CobraCommand.PersistentFlags())
62+
63+
// This is where the flags, short and long description etc. are added
64+
c.Command.WithCobraCommand(c.CobraCommand)
7165

7266
// Add commands recursively.
7367
for _, cc := range c.commandeers {
@@ -123,16 +117,14 @@ type simpleCommand struct {
123117
run func(ctx context.Context, args []string) error
124118
}
125119

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)
120+
func (c *simpleCommand) Name() string {
121+
return c.name
132122
}
133123

134-
func (s *simpleCommand) AddFlagsLocal(*pflag.FlagSet) {
124+
func (c *simpleCommand) Run(ctx context.Context, args []string) error {
125+
return c.run(ctx, args)
135126
}
136127

137-
func (s *simpleCommand) AddFlagsPersistent(*pflag.FlagSet) {
128+
func (c *simpleCommand) WithCobraCommand(cmd *cobra.Command) error {
129+
return nil
138130
}

‎cobrakai_test.go‎

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ import (
88

99
"github.com/bep/cobrakai"
1010
qt "github.com/frankban/quicktest"
11-
"github.com/spf13/pflag"
11+
"github.com/spf13/cobra"
1212
)
1313

1414
func TestCobraKai(t *testing.T) {
@@ -95,12 +95,14 @@ func (c *testComand1) Name() string {
9595
return c.name
9696
}
9797

98-
func (c *testComand1) AddFlagsLocal(flags *pflag.FlagSet) {
99-
flags.StringVar(&c.localFlagName, "localFlagName", "", "set localFlagName")
100-
}
98+
func (c *testComand1) WithCobraCommand(cmd *cobra.Command) error {
99+
localFlags := cmd.Flags()
100+
persistentFlags := cmd.PersistentFlags()
101+
102+
localFlags.StringVar(&c.localFlagName, "localFlagName", "", "set localFlagName")
103+
persistentFlags.StringVar(&c.persistentFlagName, "persistentFlagName", "", "set persistentFlagName")
101104

102-
func (c *testComand1) AddFlagsPersistent(flags *pflag.FlagSet) {
103-
flags.StringVar(&c.persistentFlagName, "persistentFlagName", "", "set persistentFlagName")
105+
return nil
104106
}
105107

106108
type testComand2 struct {
@@ -120,10 +122,8 @@ func (c *testComand2) Name() string {
120122
return c.name
121123
}
122124

123-
func (c *testComand2) AddFlagsLocal(flags *pflag.FlagSet) {
124-
flags.StringVar(&c.localFlagName, "localFlagName", "", "set localFlagName for testCommand2")
125-
}
126-
127-
func (c *testComand2) AddFlagsPersistent(flags *pflag.FlagSet) {
128-
125+
func (c *testComand2) WithCobraCommand(cmd *cobra.Command) error {
126+
localFlags := cmd.Flags()
127+
localFlags.StringVar(&c.localFlagName, "localFlagName", "", "set localFlagName for testCommand2")
128+
return nil
129129
}

‎go.mod‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@ go 1.20
55
require (
66
github.com/frankban/quicktest v1.14.2
77
github.com/spf13/cobra v1.7.0
8-
github.com/spf13/pflag v1.0.5
98
)
109

1110
require (
@@ -14,5 +13,6 @@ require (
1413
github.com/kr/pretty v0.3.0 // indirect
1514
github.com/kr/text v0.2.0 // indirect
1615
github.com/rogpeppe/go-internal v1.6.1 // indirect
16+
github.com/spf13/pflag v1.0.5 // indirect
1717
golang.org/x/xerrors v0.0.0-20191204190536-9bdfabe68543 // indirect
1818
)

0 commit comments

Comments
 (0)