@@ -13,17 +13,17 @@ import (
1313 "k8s.io/client-go/kubernetes"
1414)
1515
16- func BuildExtensions (config * KymaConfig ) []* cobra.Command {
16+ func BuildExtensions (config * KymaConfig , availableTemplateCommands * TemplateCommandsList , availableCoreCommands CoreCommandsMap ) []* cobra.Command {
1717 cmds := make ([]* cobra.Command , len (config .Extensions ))
1818
1919 for i , extension := range config .Extensions {
20- cmds [i ] = buildCommandFromExtension (& extension )
20+ cmds [i ] = buildCommandFromExtension (config , & extension , availableTemplateCommands , availableCoreCommands )
2121 }
2222
2323 return cmds
2424}
2525
26- func buildCommandFromExtension (extension * Extension ) * cobra.Command {
26+ func buildCommandFromExtension (config * KymaConfig , extension * Extension , availableTemplateCommands * TemplateCommandsList , availableCoreCommands CoreCommandsMap ) * cobra.Command {
2727 cmd := & cobra.Command {
2828 Use : extension .RootCommand .Name ,
2929 Short : extension .RootCommand .Description ,
@@ -36,22 +36,36 @@ func buildCommandFromExtension(extension *Extension) *cobra.Command {
3636 }
3737
3838 if extension .TemplateCommands != nil {
39- addGenericCommands (cmd , extension .TemplateCommands )
39+ addGenericCommands (cmd , extension .TemplateCommands , availableTemplateCommands )
4040 }
4141
42+ addCoreCommands (cmd , config , extension .CoreCommands , availableCoreCommands )
43+
4244 return cmd
4345}
4446
45- func addGenericCommands (cmd * cobra.Command , genericCommands * TemplateCommands ) {
47+ func addGenericCommands (cmd * cobra.Command , genericCommands * TemplateCommands , availableTemplateCommands * TemplateCommandsList ) {
4648 if genericCommands .ExplainCommand != nil {
47- cmd .AddCommand (templates . BuildExplainCommand (& templates.ExplainOptions {
49+ cmd .AddCommand (availableTemplateCommands . Explain (& templates.ExplainOptions {
4850 Short : genericCommands .ExplainCommand .Description ,
4951 Long : genericCommands .ExplainCommand .DescriptionLong ,
5052 Output : genericCommands .ExplainCommand .Output ,
5153 }))
5254 }
5355}
5456
57+ func addCoreCommands (cmd * cobra.Command , config * KymaConfig , extensionCoreCommands []CoreCommandInfo , availableCoreCommands CoreCommandsMap ) {
58+ for _ , expectedCoreCommand := range extensionCoreCommands {
59+ command , ok := availableCoreCommands [expectedCoreCommand .ActionID ]
60+ if ! ok {
61+ // commands doesn't exist in this version of cli and we will not process it
62+ continue
63+ }
64+
65+ cmd .AddCommand (command (config ))
66+ }
67+ }
68+
5569func ListExtensions (ctx context.Context , client kubernetes.Interface ) (ExtensionList , error ) {
5670 labelSelector := fmt .Sprintf ("%s==%s" , ExtensionLabelKey , ExtensionResourceLabelValue )
5771 cms , err := client .CoreV1 ().ConfigMaps ("" ).List (ctx , metav1.ListOptions {
@@ -87,12 +101,17 @@ func parseResourceExtension(cmData map[string]string) (*Extension, error) {
87101 return nil , err
88102 }
89103
90- resourceInfo , err := parseOptionalField [ResourceInfo ](cmData , ExtensionResourceInfoKey )
104+ resourceInfo , err := parseOptionalField [* ResourceInfo ](cmData , ExtensionResourceInfoKey )
91105 if err != nil {
92106 return nil , err
93107 }
94108
95- genericCommands , err := parseOptionalField [TemplateCommands ](cmData , ExtensionGenericCommandsKey )
109+ genericCommands , err := parseOptionalField [* TemplateCommands ](cmData , ExtensionGenericCommandsKey )
110+ if err != nil {
111+ return nil , err
112+ }
113+
114+ coreCommands , err := parseOptionalField [[]CoreCommandInfo ](cmData , ExtensionCoreCommandsKey )
96115 if err != nil {
97116 return nil , err
98117 }
@@ -101,6 +120,7 @@ func parseResourceExtension(cmData map[string]string) (*Extension, error) {
101120 RootCommand : * rootCommand ,
102121 Resource : resourceInfo ,
103122 TemplateCommands : genericCommands ,
123+ CoreCommands : coreCommands ,
104124 }, nil
105125}
106126
@@ -115,14 +135,14 @@ func parseRequiredField[T any](cmData map[string]string, cmKey string) (*T, erro
115135 return & data , err
116136}
117137
118- func parseOptionalField [T any ](cmData map [string ]string , cmKey string ) (* T , error ) {
138+ func parseOptionalField [T any ](cmData map [string ]string , cmKey string ) (T , error ) {
139+ var data T
119140 dataBytes , ok := cmData [cmKey ]
120141 if ! ok {
121142 // skip because field is not required
122- return nil , nil
143+ return data , nil
123144 }
124145
125- var data T
126146 err := yaml .Unmarshal ([]byte (dataBytes ), & data )
127- return & data , err
147+ return data , err
128148}
0 commit comments