Skip to content

Commit

Permalink
Only allow skipping parameter or credential if not required
Browse files Browse the repository at this point in the history
Signed-off-by: Kim Christensen <kimworking@gmail.com>
  • Loading branch information
kichristensen committed May 8, 2024
1 parent e1479cb commit 08ea420
Show file tree
Hide file tree
Showing 4 changed files with 13 additions and 8 deletions.
2 changes: 1 addition & 1 deletion pkg/generator/credentials.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
c, err := fn(name, surveyCredentials, creds[name].Required)
if err != nil {
return cs, err
}
Expand Down
13 changes: 9 additions & 4 deletions pkg/generator/generator.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,24 +36,29 @@ const (
questionSkip = "skip"
)

type generator func(name string, surveyType SurveyType) (secrets.SourceMap, error)
type generator func(name string, surveyType SurveyType, required bool) (secrets.SourceMap, error)

func genEmptySet(name string, surveyType SurveyType) (secrets.SourceMap, error) {
func genEmptySet(name string, surveyType SurveyType, required bool) (secrets.SourceMap, error) {
return secrets.SourceMap{
Name: name,
Source: secrets.Source{Hint: "TODO"},
}, nil
}

func genSurvey(name string, surveyType SurveyType) (secrets.SourceMap, error) {
func genSurvey(name string, surveyType SurveyType, required bool) (secrets.SourceMap, error) {
if surveyType != surveyCredentials && surveyType != surveyParameters {
return secrets.SourceMap{}, fmt.Errorf("unsupported survey type: %s", surveyType)
}

surveyOptions := []string{questionSecret, questionValue, questionEnvVar, questionPath, questionCommand}
if !required {
surveyOptions = append(surveyOptions, 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: []string{questionSecret, questionValue, questionEnvVar, questionPath, questionCommand, questionSkip},
Options: surveyOptions,
Default: "environment variable",
}

Expand Down
4 changes: 2 additions & 2 deletions pkg/generator/generator_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,13 +15,13 @@ func Test_genEmptySet(t *testing.T) {
Source: secrets.Source{Hint: "TODO"},
}

got, err := genEmptySet("emptyset", surveyParameters)
got, err := genEmptySet("emptyset", surveyParameters, false)
require.NoError(t, err)
require.Equal(t, expected, got)
}

func Test_genSurvey_unsupported(t *testing.T) {
got, err := genSurvey("myturtleset", SurveyType("turtles"))
got, err := genSurvey("myturtleset", SurveyType("turtles"), false)
require.EqualError(t, err, "unsupported survey type: turtles")
require.Equal(t, secrets.SourceMap{}, got)
}
Expand Down
2 changes: 1 addition & 1 deletion pkg/generator/parameters.go
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ func (opts *GenerateParametersOptions) genParameterSet(fn generator) (storage.Pa
if opts.Bundle.IsInternalParameter(name) {
continue
}
c, err := fn(name, surveyParameters)
c, err := fn(name, surveyParameters, opts.Bundle.Parameters[name].Required)
if err != nil {
return pset, err
}
Expand Down

0 comments on commit 08ea420

Please sign in to comment.