Skip to content

Commit 05be3c2

Browse files
committed
Fix alpha extra commands parent command
Signed-off-by: Adrian Orive <[email protected]>
1 parent 6ca3450 commit 05be3c2

File tree

2 files changed

+31
-10
lines changed

2 files changed

+31
-10
lines changed

pkg/cli/alpha.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -75,7 +75,7 @@ func (c *CLI) addExtraAlphaCommands() error {
7575
return fmt.Errorf("command %q already exists", fmt.Sprintf("%s %s", alphaCommand, cmd.Name()))
7676
}
7777
}
78-
c.cmd.AddCommand(cmd)
78+
alpha.AddCommand(cmd)
7979
}
8080
return nil
8181
}

pkg/cli/cli_test.go

Lines changed: 30 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -67,8 +67,8 @@ func setPluginsFlag(value string) {
6767
setFlag(pluginsFlag, value)
6868
}
6969

70-
func hasSubCommand(c *CLI, name string) bool {
71-
for _, subcommand := range c.cmd.Commands() {
70+
func hasSubCommand(cmd *cobra.Command, name string) bool {
71+
for _, subcommand := range cmd.Commands() {
7272
if subcommand.Name() == name {
7373
return true
7474
}
@@ -677,15 +677,15 @@ var _ = Describe("CLI", func() {
677677
const version = "version string"
678678
c, err = New(WithVersion(version))
679679
Expect(err).NotTo(HaveOccurred())
680-
Expect(hasSubCommand(c, "version")).To(BeTrue())
680+
Expect(hasSubCommand(c.cmd, "version")).To(BeTrue())
681681
})
682682
})
683683

684684
When("enabling completion", func() {
685685
It("should create a valid CLI", func() {
686686
c, err = New(WithCompletion())
687687
Expect(err).NotTo(HaveOccurred())
688-
Expect(hasSubCommand(c, "completion")).To(BeTrue())
688+
Expect(hasSubCommand(c.cmd, "completion")).To(BeTrue())
689689
})
690690
})
691691

@@ -712,22 +712,43 @@ var _ = Describe("CLI", func() {
712712
})
713713

714714
When("providing extra commands", func() {
715-
var extraCommand *cobra.Command
716-
717715
It("should create a valid CLI for non-conflicting ones", func() {
718-
extraCommand = &cobra.Command{Use: "extra"}
716+
extraCommand := &cobra.Command{Use: "extra"}
719717
c, err = New(WithExtraCommands(extraCommand))
720718
Expect(err).NotTo(HaveOccurred())
721-
Expect(hasSubCommand(c, extraCommand.Use)).To(BeTrue())
719+
Expect(hasSubCommand(c.cmd, extraCommand.Use)).To(BeTrue())
722720
})
723721

724722
It("should return an error for conflicting ones", func() {
725-
extraCommand = &cobra.Command{Use: "init"}
723+
extraCommand := &cobra.Command{Use: "init"}
726724
_, err = New(WithExtraCommands(extraCommand))
727725
Expect(err).To(HaveOccurred())
728726
})
729727
})
730728

729+
When("providing extra alpha commands", func() {
730+
It("should create a valid CLI for non-conflicting ones", func() {
731+
extraAlphaCommand := &cobra.Command{Use: "extra"}
732+
c, err = New(WithExtraAlphaCommands(extraAlphaCommand))
733+
Expect(err).NotTo(HaveOccurred())
734+
var alpha *cobra.Command
735+
for _, subcmd := range c.cmd.Commands() {
736+
if subcmd.Name() == alphaCommand {
737+
alpha = subcmd
738+
break
739+
}
740+
}
741+
Expect(alpha).NotTo(BeNil())
742+
Expect(hasSubCommand(alpha, extraAlphaCommand.Use)).To(BeTrue())
743+
})
744+
745+
It("should return an error for conflicting ones", func() {
746+
extraAlphaCommand := &cobra.Command{Use: "extra"}
747+
_, err = New(WithExtraAlphaCommands(extraAlphaCommand, extraAlphaCommand))
748+
Expect(err).To(HaveOccurred())
749+
})
750+
})
751+
731752
When("providing deprecated plugins", func() {
732753
It("should succeed and print the deprecation notice", func() {
733754
const (

0 commit comments

Comments
 (0)