Skip to content

Commit db8a76c

Browse files
committed
refactor registry commanad
1 parent 8296278 commit db8a76c

File tree

6 files changed

+85
-45
lines changed

6 files changed

+85
-45
lines changed

internal/cmd/alpha/alpha.go

Lines changed: 19 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -5,13 +5,14 @@ import (
55
"github.com/kyma-project/cli.v3/internal/cmd/alpha/access"
66
"github.com/kyma-project/cli.v3/internal/cmd/alpha/add"
77
"github.com/kyma-project/cli.v3/internal/cmd/alpha/hana"
8-
"github.com/kyma-project/cli.v3/internal/cmd/alpha/imageimport"
98
"github.com/kyma-project/cli.v3/internal/cmd/alpha/modules"
109
"github.com/kyma-project/cli.v3/internal/cmd/alpha/oidc"
1110
"github.com/kyma-project/cli.v3/internal/cmd/alpha/provision"
1211
"github.com/kyma-project/cli.v3/internal/cmd/alpha/referenceinstance"
13-
"github.com/kyma-project/cli.v3/internal/cmd/alpha/registry"
12+
"github.com/kyma-project/cli.v3/internal/cmd/alpha/registry/config"
13+
"github.com/kyma-project/cli.v3/internal/cmd/alpha/registry/imageimport"
1414
"github.com/kyma-project/cli.v3/internal/cmd/alpha/remove"
15+
"github.com/kyma-project/cli.v3/internal/cmd/alpha/templates"
1516
"github.com/kyma-project/cli.v3/internal/cmdcommon"
1617
"github.com/spf13/cobra"
1718
)
@@ -23,23 +24,28 @@ func NewAlphaCMD() (*cobra.Command, clierror.Error) {
2324
Long: `A set of alpha prototypes that may still change. Use in automations on your own risk.`,
2425
DisableFlagsInUseLine: true,
2526
}
26-
config, err := cmdcommon.NewKymaConfig(cmd)
27+
kymaConfig, err := cmdcommon.NewKymaConfig(cmd)
2728
if err != nil {
2829
return nil, err
2930
}
3031

31-
cmd.AddCommand(hana.NewHanaCMD(config))
32+
cmd.AddCommand(hana.NewHanaCMD(kymaConfig))
3233
cmd.AddCommand(provision.NewProvisionCMD())
33-
cmd.AddCommand(referenceinstance.NewReferenceInstanceCMD(config))
34-
cmd.AddCommand(imageimport.NewImportCMD(config))
35-
cmd.AddCommand(access.NewAccessCMD(config))
36-
cmd.AddCommand(oidc.NewOIDCCMD(config))
37-
cmd.AddCommand(modules.NewModulesCMD(config))
38-
cmd.AddCommand(add.NewAddCMD(config))
39-
cmd.AddCommand(remove.NewRemoveCMD(config))
40-
cmd.AddCommand(registry.NewRegistryCMD(config))
34+
cmd.AddCommand(referenceinstance.NewReferenceInstanceCMD(kymaConfig))
35+
cmd.AddCommand(access.NewAccessCMD(kymaConfig))
36+
cmd.AddCommand(oidc.NewOIDCCMD(kymaConfig))
37+
cmd.AddCommand(modules.NewModulesCMD(kymaConfig))
38+
cmd.AddCommand(add.NewAddCMD(kymaConfig))
39+
cmd.AddCommand(remove.NewRemoveCMD(kymaConfig))
4140

42-
cmds := cmdcommon.BuildExtensions(config)
41+
cmds := cmdcommon.BuildExtensions(kymaConfig, &cmdcommon.TemplateCommandsList{
42+
// list of template commands deffinitions
43+
Explain: templates.BuildExplainCommand,
44+
}, cmdcommon.CoreCommandsMap{
45+
// map of available core commands
46+
"registry_config": config.NewConfigCMD,
47+
"registry_image-import": imageimport.NewImportCMD,
48+
})
4349
cmd.AddCommand(cmds...)
4450

4551
return cmd, nil

internal/cmd/alpha/registry/registry.go

Lines changed: 0 additions & 20 deletions
This file was deleted.

internal/cmdcommon/extension.go

Lines changed: 32 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -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+
5569
func 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
}

internal/cmdcommon/extension_test.go

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -110,6 +110,10 @@ explain:
110110
description: test-description
111111
descriptionLong: test-description-long
112112
output: test-explain-output
113+
`,
114+
ExtensionCoreCommandsKey: `
115+
- actionID: test-action-id-1
116+
- actionID: test-action-id-2
113117
`,
114118
},
115119
}
@@ -137,5 +141,13 @@ func fixTestExtension(name string) Extension {
137141
Output: "test-explain-output",
138142
},
139143
},
144+
CoreCommands: []CoreCommandInfo{
145+
{
146+
ActionID: "test-action-id-1",
147+
},
148+
{
149+
ActionID: "test-action-id-2",
150+
},
151+
},
140152
}
141153
}

internal/cmdcommon/extension_types.go

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,28 @@
11
package cmdcommon
22

3+
import (
4+
"github.com/kyma-project/cli.v3/internal/cmd/alpha/templates"
5+
"github.com/spf13/cobra"
6+
)
7+
38
const (
49
ExtensionLabelKey = "kyma-cli/extension"
510
ExtensionResourceLabelValue = "resource"
611

712
ExtensionResourceInfoKey = "resource"
813
ExtensionRootCommandKey = "rootCommand"
914
ExtensionGenericCommandsKey = "templateCommands"
15+
ExtensionCoreCommandsKey = "coreCommands"
1016
)
1117

18+
// map of allowed core commands in format ID: FUNC
19+
type CoreCommandsMap map[string]func(*KymaConfig) *cobra.Command
20+
21+
// allowed template commands
22+
type TemplateCommandsList struct {
23+
Explain func(*templates.ExplainOptions) *cobra.Command
24+
}
25+
1226
type ExtensionList []Extension
1327

1428
type Extension struct {
@@ -19,6 +33,9 @@ type Extension struct {
1933
// configuration of generic commands (like 'create', 'delete', 'get', ...) which implementation is provided by the cli
2034
// most of these commands bases on the `Resource` field
2135
TemplateCommands *TemplateCommands
36+
// configuration of buildin commands (like 'registry config') which implementation is provided by cli
37+
// use this command to enable feature for a module
38+
CoreCommands []CoreCommandInfo
2239
}
2340

2441
type Scope string
@@ -60,3 +77,8 @@ type TemplateCommands struct {
6077
// kyma <root_command> explain
6178
ExplainCommand *ExplainCommand `yaml:"explain"`
6279
}
80+
81+
type CoreCommandInfo struct {
82+
// id of the functionality that cli will run when user use this command
83+
ActionID string `yaml:"actionID"`
84+
}

0 commit comments

Comments
 (0)