diff --git a/pkg/generator/credentials.go b/pkg/generator/credentials.go index b2b1d0fbe..31d2caa14 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, withRequired(creds[name].Required)) + c, err := fn(name, surveyCredentials, withRequired(creds[name].Required), withDescription(creds[name].Description)) if err != nil { return cs, err } diff --git a/pkg/generator/generator.go b/pkg/generator/generator.go index 2b4a76ed5..259d1f36b 100644 --- a/pkg/generator/generator.go +++ b/pkg/generator/generator.go @@ -28,20 +28,29 @@ const ( surveyCredentials SurveyType = "credential" surveyParameters SurveyType = "parameter" - questionSecret = "secret" - questionValue = "specific value" - questionEnvVar = "environment variable" - questionPath = "file path" - questionCommand = "shell command" - questionSkip = "skip" + questionSecret = "secret" + questionValue = "specific value" + questionEnvVar = "environment variable" + questionPath = "file path" + questionCommand = "shell command" + questionSkip = "skip" + surveryFormatString = "%s %s %q\n%s" + surveyPrefix = "How would you like to set" ) type surveyOptions struct { - required bool + required bool + description string } type surveyOption func(*surveyOptions) +func withDescription(description string) surveyOption { + return func(s *surveyOptions) { + s.description = formatDescriptionForSurvey(description) + } +} + func withRequired(required bool) surveyOption { return func(s *surveyOptions) { s.required = required @@ -57,10 +66,14 @@ func genEmptySet(name string, surveyType SurveyType, opts ...surveyOption) (secr }, nil } -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) +func formatDescriptionForSurvey(description string) string { + if description != "" { + description = description + "\n" } + return description +} + +func buildSurveySelect(name string, surveyType SurveyType, opts ...surveyOption) *survey.Select { surveyOptions := &surveyOptions{} for _, opt := range opts { opt(surveyOptions) @@ -72,12 +85,22 @@ func genSurvey(name string, surveyType SurveyType, opts ...surveyOption) (secret } // 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), + return &survey.Select{ + Message: fmt.Sprintf(surveryFormatString, surveyPrefix, surveyType, name, surveyOptions.description), Options: selectOptions, Default: "environment variable", } +} + +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) + } + + // extra space-suffix to align question and answer. Unfortunately misaligns help text + sourceTypePrompt := buildSurveySelect(name, surveyType, opts...) + // extra space-suffix to align question and answer. Unfortunately misaligns help text sourceValuePromptTemplate := "Enter the %s that will be used to set %s %q\n " diff --git a/pkg/generator/generator_test.go b/pkg/generator/generator_test.go index 464fd2446..dbffdf3fb 100644 --- a/pkg/generator/generator_test.go +++ b/pkg/generator/generator_test.go @@ -1,6 +1,7 @@ package generator import ( + "fmt" "os" "testing" @@ -44,3 +45,27 @@ func TestCheckUserHomeDir(t *testing.T) { }) } } + +func TestBuildSurveySelectRequiredTrue(t *testing.T) { + survey := buildSurveySelect("name", surveyCredentials, withRequired(true)) + assert.NotContains(t, survey.Options, questionSkip) +} + +func TestBuildSurveySelectRequiredFalse(t *testing.T) { + survey := buildSurveySelect("name", surveyCredentials, withRequired(false)) + assert.Contains(t, survey.Options, questionSkip) +} + +func TestBuildSurveySelectEmptyDescription(t *testing.T) { + name := "name_value" + description := "" + survey := buildSurveySelect(name, surveyCredentials, withDescription(description)) + assert.Equal(t, survey.Message, fmt.Sprintf(surveryFormatString, surveyPrefix, surveyCredentials, name, formatDescriptionForSurvey(description))) +} + +func TestBuildSurveySelectValidDescription(t *testing.T) { + name := "name_value" + description := "here are details on how to fill out the survey" + survey := buildSurveySelect(name, surveyCredentials, withDescription(description)) + assert.Equal(t, survey.Message, fmt.Sprintf(surveryFormatString, surveyPrefix, surveyCredentials, name, formatDescriptionForSurvey(description))) +} diff --git a/pkg/generator/parameters.go b/pkg/generator/parameters.go index 4bf05c2ca..685b3b743 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, withRequired(opts.Bundle.Parameters[name].Required)) + c, err := fn(name, surveyParameters, withRequired(opts.Bundle.Parameters[name].Required), withDescription(opts.Bundle.Parameters[name].Description)) if err != nil { return pset, err }