Skip to content

Commit

Permalink
refactor: Shorten config flag (#71)
Browse files Browse the repository at this point in the history
* refactor: Shorten config flag

* adapt tests

* add testcase
  • Loading branch information
lindnerby authored Oct 18, 2024
1 parent ce28884 commit d2a1d78
Show file tree
Hide file tree
Showing 11 changed files with 55 additions and 52 deletions.
15 changes: 9 additions & 6 deletions cmd/modulectl/create/cmd_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ func Test_Execute_ParsesAllModuleOptions(t *testing.T) {

os.Args = []string{
"create",
"--module-config-file", moduleConfigFile,
"--config-file", moduleConfigFile,
"--git-remote", gitRemote,
"--insecure", insecure,
"--output", templateOutput,
Expand All @@ -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)
Expand All @@ -84,13 +84,15 @@ func Test_Execute_ParsesAllModuleOptions(t *testing.T) {
}

func Test_Execute_ParsesModuleShortOptions(t *testing.T) {
credentials := testutils.RandomName(10)
configFile := testutils.RandomName(10)
templateOutput := testutils.RandomName(10)
registry := testutils.RandomName(10)

os.Args = []string{
"create",
"-c", credentials,
"-c", configFile,
"-o", templateOutput,
"-r", registry,
}

svc := &moduleServiceStub{}
Expand All @@ -99,8 +101,9 @@ func Test_Execute_ParsesModuleShortOptions(t *testing.T) {
err := cmd.Execute()
require.NoError(t, err)

assert.Equal(t, credentials, svc.opts.Credentials)
assert.Equal(t, configFile, svc.opts.ConfigFile)
assert.Equal(t, templateOutput, svc.opts.TemplateOutput)
assert.Equal(t, registry, svc.opts.RegistryURL)
}

func Test_Execute_ModuleParsesDefaults(t *testing.T) {
Expand All @@ -114,7 +117,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)
Expand Down
14 changes: 7 additions & 7 deletions cmd/modulectl/create/flags.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 <user:password> format."

Expand Down Expand Up @@ -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)
Expand Down
4 changes: 2 additions & 2 deletions cmd/modulectl/create/flags_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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: ""},
Expand Down
2 changes: 1 addition & 1 deletion cmd/modulectl/create/long.txt
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion cmd/modulectl/create/use.txt
Original file line number Diff line number Diff line change
@@ -1 +1 @@
create [--module-config-file MODULE_CONFIG_FILE] [--registry MODULE_REGISTRY] [flags]
create [--config-file MODULE_CONFIG_FILE] [--registry MODULE_REGISTRY] [flags]
8 changes: 4 additions & 4 deletions docs/gen-docs/modulectl_create.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand All @@ -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 <user:password> format.
--registry-credentials string Basic authentication credentials for the given repository in the <user:password> format.
```
## See also
Expand Down
2 changes: 1 addition & 1 deletion internal/service/create/create.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
Expand Down
4 changes: 2 additions & 2 deletions internal/service/create/create_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down Expand Up @@ -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
}

Expand Down
6 changes: 3 additions & 3 deletions internal/service/create/options.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ import (

type Options struct {
Out iotools.Out
ModuleConfigFile string
ConfigFile string
Credentials string
GitRemote string
Insecure bool
Expand All @@ -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 != "" {
Expand Down
48 changes: 24 additions & 24 deletions internal/service/create/options_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,57 +26,57 @@ 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",
},
{
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",
},
{
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)",
Expand Down
2 changes: 1 addition & 1 deletion tests/e2e/create/create_suite_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ func (cmd *createCmd) execute() error {
args := []string{"create"}

if cmd.moduleConfigFile != "" {
args = append(args, "--module-config-file="+cmd.moduleConfigFile)
args = append(args, "--config-file="+cmd.moduleConfigFile)
}

if cmd.registry != "" {
Expand Down

0 comments on commit d2a1d78

Please sign in to comment.