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 {
1716type 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
2422type root struct {
@@ -56,18 +54,14 @@ type Commandeer struct {
5654
5755func (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}
0 commit comments