Skip to content

Commit

Permalink
Merge pull request #3100 from kichristensen/allowSkipDuringGenerate
Browse files Browse the repository at this point in the history
Support skipping parameters and credentials during generate
  • Loading branch information
kichristensen committed May 13, 2024
2 parents 4a0633c + 8ebc915 commit 7600978
Show file tree
Hide file tree
Showing 5 changed files with 42 additions and 6 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, withRequired(creds[name].Required))
if err != nil {
return cs, err
}
Expand Down
36 changes: 32 additions & 4 deletions pkg/generator/generator.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,26 +33,48 @@ const (
questionEnvVar = "environment variable"
questionPath = "file path"
questionCommand = "shell command"
questionSkip = "skip"
)

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

type surveyOption func(*surveyOptions)

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) (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) (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)
}

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: []string{questionSecret, questionValue, questionEnvVar, questionPath, questionCommand},
Options: selectOptions,
Default: "environment variable",
}

Expand All @@ -78,6 +100,12 @@ func genSurvey(name string, surveyType SurveyType) (secrets.SourceMap, error) {
promptMsg = fmt.Sprintf(sourceValuePromptTemplate, "path", surveyType, name)
case questionCommand:
promptMsg = fmt.Sprintf(sourceValuePromptTemplate, "command", surveyType, name)
case questionSkip:
promptMsg = fmt.Sprintf(sourceValuePromptTemplate, "skip", surveyType, name)
}

if source == questionSkip {
return secrets.SourceMap{}, nil
}

sourceValuePrompt := &survey.Input{
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, withRequired(opts.Bundle.Parameters[name].Required))
if err != nil {
return pset, err
}
Expand Down
4 changes: 4 additions & 0 deletions pkg/porter/credentials.go
Original file line number Diff line number Diff line change
Expand Up @@ -155,6 +155,10 @@ func (p *Porter) GenerateCredentials(ctx context.Context, opts CredentialOptions
return span.Error(fmt.Errorf("unable to generate credentials: %w", err))
}

if len(cs.Credentials) == 0 {
return nil
}

cs.Status.Created = time.Now()
cs.Status.Modified = cs.Status.Created

Expand Down
4 changes: 4 additions & 0 deletions pkg/porter/parameters.go
Original file line number Diff line number Diff line change
Expand Up @@ -164,6 +164,10 @@ func (p *Porter) GenerateParameters(ctx context.Context, opts ParameterOptions)
return fmt.Errorf("unable to generate parameter set: %w", err)
}

if len(pset.Parameters) == 0 {
return nil
}

pset.Status.Created = time.Now()
pset.Status.Modified = pset.Status.Created

Expand Down

0 comments on commit 7600978

Please sign in to comment.