@@ -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+
1024type 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.
3751type Commandeer struct {
38- Command Commander
39- commandeers []* Commandeer
40-
41- // compiled
52+ Command Commander
4253 CobraCommand * cobra.Command
54+ commandeers []* Commandeer
4355}
4456
4557func (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.
8684type 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+ }
0 commit comments