@@ -17,19 +17,24 @@ limitations under the License.
17
17
package cli
18
18
19
19
import (
20
+ "fmt"
20
21
"strings"
21
22
22
23
"github.com/spf13/cobra"
23
24
configgen "sigs.k8s.io/kubebuilder/v3/pkg/cli/alpha/config-gen"
24
25
)
25
26
27
+ const (
28
+ alphaCommand = "alpha"
29
+ )
30
+
26
31
var alphaCommands = []* cobra.Command {
27
32
configgen .NewCommand (),
28
33
}
29
34
30
35
func (c * CLI ) newAlphaCmd () * cobra.Command {
31
36
alpha := & cobra.Command {
32
- Use : "alpha" ,
37
+ Use : alphaCommand ,
33
38
SuggestFor : []string {"experimental" },
34
39
Short : "Alpha kubebuilder subcommands" ,
35
40
Long : strings .TrimSpace (`
@@ -46,7 +51,31 @@ Alpha kubebuilder commands are for unstable features.
46
51
}
47
52
48
53
func (c * CLI ) addAlphaCmd () {
49
- if len (alphaCommands ) > 0 {
54
+ if ( len (alphaCommands ) + len ( c . extraAlphaCommands ) ) > 0 {
50
55
c .cmd .AddCommand (c .newAlphaCmd ())
51
56
}
52
57
}
58
+
59
+ func (c * CLI ) addExtraAlphaCommands () error {
60
+ // Search for the alpha subcommand
61
+ var alpha * cobra.Command
62
+ for _ , subCmd := range c .cmd .Commands () {
63
+ if subCmd .Name () == alphaCommand {
64
+ alpha = subCmd
65
+ break
66
+ }
67
+ }
68
+ if alpha == nil {
69
+ return fmt .Errorf ("no %q command found" , alphaCommand )
70
+ }
71
+
72
+ for _ , cmd := range c .extraAlphaCommands {
73
+ for _ , subCmd := range alpha .Commands () {
74
+ if cmd .Name () == subCmd .Name () {
75
+ return fmt .Errorf ("command %q already exists" , fmt .Sprintf ("%s %s" , alphaCommand , cmd .Name ()))
76
+ }
77
+ }
78
+ c .cmd .AddCommand (cmd )
79
+ }
80
+ return nil
81
+ }
0 commit comments