From 144cc29e103f6a7ae06933bd22e855ffd528b337 Mon Sep 17 00:00:00 2001 From: Benjamin Lindner Date: Wed, 16 Oct 2024 17:00:35 +0200 Subject: [PATCH] refactor: Shorten config flag --- cmd/modulectl/create/cmd_test.go | 4 +-- cmd/modulectl/create/flags.go | 14 ++++---- cmd/modulectl/create/flags_test.go | 4 +-- cmd/modulectl/create/long.txt | 2 +- cmd/modulectl/create/use.txt | 2 +- docs/gen-docs/modulectl_create.md | 8 ++--- internal/service/create/create.go | 2 +- internal/service/create/create_test.go | 4 +-- internal/service/create/options.go | 6 ++-- internal/service/create/options_test.go | 48 ++++++++++++------------- 10 files changed, 47 insertions(+), 47 deletions(-) diff --git a/cmd/modulectl/create/cmd_test.go b/cmd/modulectl/create/cmd_test.go index 5e8c6a82..8e91ab64 100644 --- a/cmd/modulectl/create/cmd_test.go +++ b/cmd/modulectl/create/cmd_test.go @@ -74,7 +74,7 @@ func Test_Execute_ParsesAllModuleOptions(t *testing.T) { insecureFlagSet, err := strconv.ParseBool(insecure) require.NoError(t, err) - assert.Equal(t, moduleConfigFile, svc.opts.ModuleConfigFile) + assert.Equal(t, moduleConfigFile, svc.opts.ConfigFile) assert.Equal(t, credentials, svc.opts.Credentials) assert.Equal(t, gitRemote, svc.opts.GitRemote) assert.Equal(t, insecureFlagSet, svc.opts.Insecure) @@ -114,7 +114,7 @@ func Test_Execute_ModuleParsesDefaults(t *testing.T) { err := cmd.Execute() require.NoError(t, err) - assert.Equal(t, createcmd.ModuleConfigFileFlagDefault, svc.opts.ModuleConfigFile) + assert.Equal(t, createcmd.ConfigFileFlagDefault, svc.opts.ConfigFile) assert.Equal(t, createcmd.CredentialsFlagDefault, svc.opts.Credentials) assert.Equal(t, createcmd.GitRemoteFlagDefault, svc.opts.GitRemote) assert.Equal(t, createcmd.InsecureFlagDefault, svc.opts.Insecure) diff --git a/cmd/modulectl/create/flags.go b/cmd/modulectl/create/flags.go index 54fbcde1..64162d85 100644 --- a/cmd/modulectl/create/flags.go +++ b/cmd/modulectl/create/flags.go @@ -7,12 +7,12 @@ import ( ) const ( - ModuleConfigFileFlagName = "module-config-file" - ModuleConfigFileFlagDefault = "module-config.yaml" - moduleConfigFileFlagUsage = "Specifies the path to the module configuration file." + ConfigFileFlagName = "config-file" + configFileFlagShort = "c" + ConfigFileFlagDefault = "module-config.yaml" + configFileFlagUsage = "Specifies the path to the module configuration file." CredentialsFlagName = "registry-credentials" //nolint:gosec // Not hardcoded credentials, rather just flag name - credentialsFlagShort = "c" CredentialsFlagDefault = "" credentialsFlagUsage = "Basic authentication credentials for the given repository in the format." @@ -42,9 +42,9 @@ const ( ) func parseFlags(flags *pflag.FlagSet, opts *create.Options) { - flags.StringVar(&opts.ModuleConfigFile, ModuleConfigFileFlagName, ModuleConfigFileFlagDefault, - moduleConfigFileFlagUsage) - flags.StringVarP(&opts.Credentials, CredentialsFlagName, credentialsFlagShort, CredentialsFlagDefault, + flags.StringVarP(&opts.ConfigFile, ConfigFileFlagName, configFileFlagShort, ConfigFileFlagDefault, + configFileFlagUsage) + flags.StringVar(&opts.Credentials, CredentialsFlagName, CredentialsFlagDefault, credentialsFlagUsage) flags.StringVar(&opts.GitRemote, GitRemoteFlagName, GitRemoteFlagDefault, gitRemoteFlagUsage) flags.BoolVar(&opts.Insecure, InsecureFlagName, InsecureFlagDefault, insecureFlagUsage) diff --git a/cmd/modulectl/create/flags_test.go b/cmd/modulectl/create/flags_test.go index f3297edb..b0c50d65 100644 --- a/cmd/modulectl/create/flags_test.go +++ b/cmd/modulectl/create/flags_test.go @@ -14,8 +14,8 @@ func Test_ScaffoldFlagsDefaults(t *testing.T) { expected string }{ { - name: createcmd.ModuleConfigFileFlagName, - value: createcmd.ModuleConfigFileFlagDefault, + name: createcmd.ConfigFileFlagName, + value: createcmd.ConfigFileFlagDefault, expected: "module-config.yaml", }, {name: createcmd.CredentialsFlagName, value: createcmd.CredentialsFlagDefault, expected: ""}, diff --git a/cmd/modulectl/create/long.txt b/cmd/modulectl/create/long.txt index 4a4e04f7..5e09d764 100644 --- a/cmd/modulectl/create/long.txt +++ b/cmd/modulectl/create/long.txt @@ -7,7 +7,7 @@ For more information about Kyma modules see the [documentation](https://kyma-pro ### Configuration -Provide the `--module-config-file` flag with a config file path. +Provide the `--config-file` flag with a config file path. The module config file is a YAML file used to configure the following attributes for the module: ```yaml diff --git a/cmd/modulectl/create/use.txt b/cmd/modulectl/create/use.txt index 7bf02e60..d9478045 100644 --- a/cmd/modulectl/create/use.txt +++ b/cmd/modulectl/create/use.txt @@ -1 +1 @@ -create [--module-config-file MODULE_CONFIG_FILE] [--registry MODULE_REGISTRY] [flags] \ No newline at end of file +create [--config-file MODULE_CONFIG_FILE] [--registry MODULE_REGISTRY] [flags] \ No newline at end of file diff --git a/docs/gen-docs/modulectl_create.md b/docs/gen-docs/modulectl_create.md index a1a5a16b..c3c9f311 100644 --- a/docs/gen-docs/modulectl_create.md +++ b/docs/gen-docs/modulectl_create.md @@ -15,7 +15,7 @@ For more information about Kyma modules see the [documentation](https://kyma-pro ### Configuration -Provide the `--module-config-file` flag with a config file path. +Provide the `--config-file` flag with a config file path. The module config file is a YAML file used to configure the following attributes for the module: ```yaml @@ -47,7 +47,7 @@ If you configured the "--registry" flag, the created module is validated and pus ```bash -modulectl create [--module-config-file MODULE_CONFIG_FILE] [--registry MODULE_REGISTRY] [flags] +modulectl create [--config-file MODULE_CONFIG_FILE] [--registry MODULE_REGISTRY] [flags] ``` ## Examples @@ -60,14 +60,14 @@ Build a simple module and push it to a remote registry ## Flags ```bash +-c, --config-file string Specifies the path to the module configuration file. --git-remote string Specifies the URL of the module's GitHub repository. -h, --help Provides help for the create command. --insecure Uses an insecure connection to access the registry. - --module-config-file string Specifies the path to the module configuration file. -o, --output string Path to write the ModuleTemplate file to, if the module is uploaded to a registry (default "template.yaml"). -r, --registry string Context URL of the repository. The repository URL will be automatically added to the repository contexts in the module descriptor. --registry-cred-selector string Label selector to identify an externally created Secret of type "kubernetes.io/dockerconfigjson". It allows the image to be accessed in private image registries. It can be used when you push your module to a registry with authenticated access. For example, "label1=value1,label2=value2". --c, --registry-credentials string Basic authentication credentials for the given repository in the format. + --registry-credentials string Basic authentication credentials for the given repository in the format. ``` ## See also diff --git a/internal/service/create/create.go b/internal/service/create/create.go index bbefb392..8527911d 100644 --- a/internal/service/create/create.go +++ b/internal/service/create/create.go @@ -122,7 +122,7 @@ func (s *Service) Run(opts Options) error { } }() - moduleConfig, err := s.moduleConfigService.ParseAndValidateModuleConfig(opts.ModuleConfigFile) + moduleConfig, err := s.moduleConfigService.ParseAndValidateModuleConfig(opts.ConfigFile) if err != nil { return fmt.Errorf("failed to parse module config: %w", err) } diff --git a/internal/service/create/create_test.go b/internal/service/create/create_test.go index 40474d2a..c49078a6 100644 --- a/internal/service/create/create_test.go +++ b/internal/service/create/create_test.go @@ -36,7 +36,7 @@ func Test_CreateModule_ReturnsError_WhenModuleConfigFileIsEmpty(t *testing.T) { err = svc.Run(opts) require.ErrorIs(t, err, commonerrors.ErrInvalidOption) - require.Contains(t, err.Error(), "opts.ModuleConfigFile") + require.Contains(t, err.Error(), "opts.ConfigFile") } func Test_CreateModule_ReturnsError_WhenOutIsNil(t *testing.T) { @@ -120,7 +120,7 @@ func (b *createOptionsBuilder) withOut(out iotools.Out) *createOptionsBuilder { } func (b *createOptionsBuilder) withModuleConfigFile(moduleConfigFile string) *createOptionsBuilder { - b.options.ModuleConfigFile = moduleConfigFile + b.options.ConfigFile = moduleConfigFile return b } diff --git a/internal/service/create/options.go b/internal/service/create/options.go index fbe183c9..71e952b8 100644 --- a/internal/service/create/options.go +++ b/internal/service/create/options.go @@ -11,7 +11,7 @@ import ( type Options struct { Out iotools.Out - ModuleConfigFile string + ConfigFile string Credentials string GitRemote string Insecure bool @@ -25,8 +25,8 @@ func (opts Options) Validate() error { return fmt.Errorf("%w: opts.Out must not be nil", commonerrors.ErrInvalidOption) } - if opts.ModuleConfigFile == "" { - return fmt.Errorf("%w: opts.ModuleConfigFile must not be empty", commonerrors.ErrInvalidOption) + if opts.ConfigFile == "" { + return fmt.Errorf("%w: opts.ConfigFile must not be empty", commonerrors.ErrInvalidOption) } if opts.Credentials != "" { diff --git a/internal/service/create/options_test.go b/internal/service/create/options_test.go index 70a74898..af6987b1 100644 --- a/internal/service/create/options_test.go +++ b/internal/service/create/options_test.go @@ -26,20 +26,20 @@ func Test_Validate_Options(t *testing.T) { errMsg: "opts.Out must not be nil", }, { - name: "ModuleConfigFile is empty", + name: "ConfigFile is empty", options: create.Options{ - Out: iotools.NewDefaultOut(io.Discard), - ModuleConfigFile: "", + Out: iotools.NewDefaultOut(io.Discard), + ConfigFile: "", }, wantErr: true, - errMsg: "opts.ModuleConfigFile must not be empty", + errMsg: "opts.ConfigFile must not be empty", }, { name: "Credentials invalid format", options: create.Options{ - Out: iotools.NewDefaultOut(io.Discard), - ModuleConfigFile: "config.yaml", - Credentials: "missingsemicolon", + Out: iotools.NewDefaultOut(io.Discard), + ConfigFile: "config.yaml", + Credentials: "missingsemicolon", }, wantErr: true, errMsg: "opts.Credentials is in invalid format", @@ -47,11 +47,11 @@ func Test_Validate_Options(t *testing.T) { { name: "TemplateOutput is empty", options: create.Options{ - Out: iotools.NewDefaultOut(io.Discard), - ModuleConfigFile: "config.yaml", - Credentials: "username:password", - GitRemote: "github.com/kyma-project/modulectl", - TemplateOutput: "", + Out: iotools.NewDefaultOut(io.Discard), + ConfigFile: "config.yaml", + Credentials: "username:password", + GitRemote: "github.com/kyma-project/modulectl", + TemplateOutput: "", }, wantErr: true, errMsg: "opts.TemplateOutput must not be empty", @@ -59,24 +59,24 @@ func Test_Validate_Options(t *testing.T) { { name: "All fields valid", options: create.Options{ - Out: iotools.NewDefaultOut(io.Discard), - ModuleConfigFile: "config.yaml", - Credentials: "username:password", - GitRemote: "github.com/kyma-project/modulectl", - TemplateOutput: "output", - RegistryURL: "http://registry.example.com", + Out: iotools.NewDefaultOut(io.Discard), + ConfigFile: "config.yaml", + Credentials: "username:password", + GitRemote: "github.com/kyma-project/modulectl", + TemplateOutput: "output", + RegistryURL: "http://registry.example.com", }, wantErr: false, }, { name: "RegistryURL does not start with http", options: create.Options{ - Out: iotools.NewDefaultOut(io.Discard), - ModuleConfigFile: "config.yaml", - Credentials: "username:password", - GitRemote: "origin", - TemplateOutput: "output", - RegistryURL: "ftp://registry.example.com", + Out: iotools.NewDefaultOut(io.Discard), + ConfigFile: "config.yaml", + Credentials: "username:password", + GitRemote: "origin", + TemplateOutput: "output", + RegistryURL: "ftp://registry.example.com", }, wantErr: true, errMsg: "opts.RegistryURL does not start with http(s)",