Skip to content

Commit 3a3a8e0

Browse files
committed
Only Init the running command and its ancestors
1 parent b591dca commit 3a3a8e0

File tree

2 files changed

+41
-14
lines changed

2 files changed

+41
-14
lines changed

‎simplecobra.go‎

Lines changed: 17 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -74,22 +74,27 @@ type Commandeer struct {
7474
}
7575

7676
func (c *Commandeer) init() error {
77-
// Start from the root and initialize all commands recursively.
78-
// root is always set.
79-
cd := c.Root
80-
var initc func(*Commandeer) error
81-
initc = func(cd *Commandeer) error {
77+
78+
// Collect all ancestors including self.
79+
var ancestors []*Commandeer
80+
{
81+
cd := c
82+
for cd != nil {
83+
ancestors = append(ancestors, cd)
84+
cd = cd.Parent
85+
}
86+
}
87+
88+
// Init all of them starting from the root.
89+
for i := len(ancestors) - 1; i >= 0; i-- {
90+
cd := ancestors[i]
8291
if err := cd.Command.Init(cd); err != nil {
8392
return err
8493
}
85-
for _, cc := range cd.commandeers {
86-
if err := initc(cc); err != nil {
87-
return err
88-
}
89-
}
90-
return nil
9194
}
92-
return initc(cd)
95+
96+
return nil
97+
9398
}
9499

95100
type runErr struct {

‎simplecobra_test.go‎

Lines changed: 24 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -76,6 +76,22 @@ func TestSimplecobra(t *testing.T) {
7676

7777
}
7878

79+
func TestInitAncestorsOnly(t *testing.T) {
80+
c := qt.New(t)
81+
82+
rootCmd := testCommands()
83+
x, err := simplecobra.New(rootCmd)
84+
c.Assert(err, qt.IsNil)
85+
args := []string{"bar", "baz", "--persistentFlagName", "baz_persistent"}
86+
cd3, err := x.Execute(context.Background(), args)
87+
c.Assert(err, qt.IsNil)
88+
c.Assert(cd3.Command.Name(), qt.Equals, "baz")
89+
c.Assert(rootCmd.isInit, qt.IsTrue)
90+
c.Assert(rootCmd.commands[0].(*lvl1Command).isInit, qt.IsFalse)
91+
c.Assert(rootCmd.commands[1].(*lvl1Command).isInit, qt.IsTrue)
92+
c.Assert(cd3.Command.(*lvl2Command).isInit, qt.IsTrue)
93+
}
94+
7995
func TestErrors(t *testing.T) {
8096
c := qt.New(t)
8197

@@ -142,7 +158,8 @@ func Example() {
142158
}
143159

144160
type rootCommand struct {
145-
name string
161+
name string
162+
isInit bool
146163

147164
// Flags
148165
persistentFlagName string
@@ -164,6 +181,7 @@ func (c *rootCommand) Commands() []simplecobra.Commander {
164181
}
165182

166183
func (c *rootCommand) Init(*simplecobra.Commandeer) error {
184+
c.isInit = true
167185
c.persistentFlagNameC = c.persistentFlagName + "_rootCommand_compiled"
168186
c.localFlagNameC = c.localFlagName + "_rootCommand_compiled"
169187
return nil
@@ -189,7 +207,8 @@ func (c *rootCommand) WithCobraCommand(cmd *cobra.Command) error {
189207
}
190208

191209
type lvl1Command struct {
192-
name string
210+
name string
211+
isInit bool
193212

194213
localFlagName string
195214
localFlagNameC string
@@ -206,6 +225,7 @@ func (c *lvl1Command) Commands() []simplecobra.Commander {
206225
}
207226

208227
func (c *lvl1Command) Init(cd *simplecobra.Commandeer) error {
228+
c.isInit = true
209229
c.localFlagNameC = c.localFlagName + "_lvl1Command_compiled"
210230
c.rootCmd = cd.Root.Command.(*rootCommand)
211231
return nil
@@ -228,6 +248,7 @@ func (c *lvl1Command) WithCobraCommand(cmd *cobra.Command) error {
228248

229249
type lvl2Command struct {
230250
name string
251+
isInit bool
231252
localFlagName string
232253

233254
ctx context.Context
@@ -240,6 +261,7 @@ func (c *lvl2Command) Commands() []simplecobra.Commander {
240261
}
241262

242263
func (c *lvl2Command) Init(cd *simplecobra.Commandeer) error {
264+
c.isInit = true
243265
c.rootCmd = cd.Root.Command.(*rootCommand)
244266
c.parentCmd = cd.Parent.Command.(*lvl1Command)
245267
return nil

0 commit comments

Comments
 (0)