Skip to content

Commit

Permalink
Added the description to the credentials generate output (getporter#3117
Browse files Browse the repository at this point in the history
)

* Added the description to the credentials generate output

Signed-off-by: David Gannon <19214156+dgannon991@users.noreply.github.com>

* Added tests for withRequired and withDescription

Signed-off-by: David Gannon <19214156+dgannon991@users.noreply.github.com>

* Tiny formatting tweak

Signed-off-by: David Gannon <19214156+dgannon991@users.noreply.github.com>

* Changed where we call formatDescription

Signed-off-by: David Gannon <19214156+dgannon991@users.noreply.github.com>

---------

Signed-off-by: David Gannon <19214156+dgannon991@users.noreply.github.com>
Co-authored-by: Kim Christensen <2461567+kichristensen@users.noreply.github.com>
Co-authored-by: schristoff <28318173+schristoff@users.noreply.github.com>
  • Loading branch information
3 people committed May 20, 2024
1 parent a4e1efe commit b544da6
Show file tree
Hide file tree
Showing 4 changed files with 62 additions and 14 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, withRequired(creds[name].Required))
c, err := fn(name, surveyCredentials, withRequired(creds[name].Required), withDescription(creds[name].Description))
if err != nil {
return cs, err
}
Expand Down
47 changes: 35 additions & 12 deletions pkg/generator/generator.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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)
Expand All @@ -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 "

Expand Down
25 changes: 25 additions & 0 deletions pkg/generator/generator_test.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package generator

import (
"fmt"
"os"
"testing"

Expand Down Expand Up @@ -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)))
}
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, 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
}
Expand Down

0 comments on commit b544da6

Please sign in to comment.