Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support skipping parameters and credentials during generate #3100

Merged
merged 5 commits into from
May 13, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading