diff --git a/pkg/generator/credentials.go b/pkg/generator/credentials.go index 232e49016..b2b1d0fbe 100644 --- a/pkg/generator/credentials.go +++ b/pkg/generator/credentials.go @@ -53,7 +53,7 @@ func genCredentialSet(namespace string, name string, creds map[string]bundle.Cre sort.Strings(credentialNames) for _, name := range credentialNames { - c, err := fn(name, surveyCredentials, creds[name].Required) + c, err := fn(name, surveyCredentials, withRequired(creds[name].Required)) if err != nil { return cs, err } diff --git a/pkg/generator/generator.go b/pkg/generator/generator.go index 405fa6a6f..2b4a76ed5 100644 --- a/pkg/generator/generator.go +++ b/pkg/generator/generator.go @@ -36,29 +36,45 @@ const ( questionSkip = "skip" ) -type generator func(name string, surveyType SurveyType, required bool) (secrets.SourceMap, error) +type surveyOptions struct { + required bool +} + +type surveyOption func(*surveyOptions) -func genEmptySet(name string, surveyType SurveyType, required bool) (secrets.SourceMap, error) { +func withRequired(required bool) surveyOption { + return func(s *surveyOptions) { + s.required = required + } +} + +type generator func(name string, surveyType SurveyType, opts ...surveyOption) (secrets.SourceMap, error) + +func genEmptySet(name string, surveyType SurveyType, opts ...surveyOption) (secrets.SourceMap, error) { return secrets.SourceMap{ Name: name, Source: secrets.Source{Hint: "TODO"}, }, nil } -func genSurvey(name string, surveyType SurveyType, required bool) (secrets.SourceMap, error) { +func genSurvey(name string, surveyType SurveyType, opts ...surveyOption) (secrets.SourceMap, error) { if surveyType != surveyCredentials && surveyType != surveyParameters { return secrets.SourceMap{}, fmt.Errorf("unsupported survey type: %s", surveyType) } + surveyOptions := &surveyOptions{} + for _, opt := range opts { + opt(surveyOptions) + } - surveyOptions := []string{questionSecret, questionValue, questionEnvVar, questionPath, questionCommand} - if !required { - surveyOptions = append(surveyOptions, questionSkip) + selectOptions := []string{questionSecret, questionValue, questionEnvVar, questionPath, questionCommand} + if !surveyOptions.required { + selectOptions = append(selectOptions, questionSkip) } // extra space-suffix to align question and answer. Unfortunately misaligns help text sourceTypePrompt := &survey.Select{ Message: fmt.Sprintf("How would you like to set %s %q\n ", surveyType, name), - Options: surveyOptions, + Options: selectOptions, Default: "environment variable", } diff --git a/pkg/generator/generator_test.go b/pkg/generator/generator_test.go index 7ada6cb31..464fd2446 100644 --- a/pkg/generator/generator_test.go +++ b/pkg/generator/generator_test.go @@ -15,13 +15,13 @@ func Test_genEmptySet(t *testing.T) { Source: secrets.Source{Hint: "TODO"}, } - got, err := genEmptySet("emptyset", surveyParameters, false) + got, err := genEmptySet("emptyset", surveyParameters) require.NoError(t, err) require.Equal(t, expected, got) } func Test_genSurvey_unsupported(t *testing.T) { - got, err := genSurvey("myturtleset", SurveyType("turtles"), false) + got, err := genSurvey("myturtleset", SurveyType("turtles")) require.EqualError(t, err, "unsupported survey type: turtles") require.Equal(t, secrets.SourceMap{}, got) } diff --git a/pkg/generator/parameters.go b/pkg/generator/parameters.go index b878f3ddc..4bf05c2ca 100644 --- a/pkg/generator/parameters.go +++ b/pkg/generator/parameters.go @@ -54,7 +54,7 @@ func (opts *GenerateParametersOptions) genParameterSet(fn generator) (storage.Pa if opts.Bundle.IsInternalParameter(name) { continue } - c, err := fn(name, surveyParameters, opts.Bundle.Parameters[name].Required) + c, err := fn(name, surveyParameters, withRequired(opts.Bundle.Parameters[name].Required)) if err != nil { return pset, err }