From 0e5819bddc50560cd48a3f2bc8bc9fa8e0c55f40 Mon Sep 17 00:00:00 2001
From: David Gannon <19214156+dgannon991@users.noreply.github.com>
Date: Sun, 12 May 2024 15:54:01 +0100
Subject: [PATCH] Add the description to the survey prompt for credentials and
 parameters

Signed-off-by: David Gannon <19214156+dgannon991@users.noreply.github.com>
---
 pkg/generator/credentials.go    |  2 +-
 pkg/generator/generator.go      | 14 ++++++++++----
 pkg/generator/generator_test.go |  4 ++--
 pkg/generator/parameters.go     |  2 +-
 4 files changed, 14 insertions(+), 8 deletions(-)

diff --git a/pkg/generator/credentials.go b/pkg/generator/credentials.go
index e2d22c129..d97d7307b 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)
+		c, err := fn(name, creds[name].Description, surveyCredentials)
 		if err != nil {
 			return cs, err
 		}
diff --git a/pkg/generator/generator.go b/pkg/generator/generator.go
index c3d69e34a..1299d3174 100644
--- a/pkg/generator/generator.go
+++ b/pkg/generator/generator.go
@@ -35,23 +35,29 @@ const (
 	questionCommand = "shell command"
 )
 
-type generator func(name string, surveyType SurveyType) (secrets.SourceMap, error)
+type generator func(name string, description string, surveyType SurveyType) (secrets.SourceMap, error)
 
-func genEmptySet(name string, surveyType SurveyType) (secrets.SourceMap, error) {
+func genEmptySet(name string, description string, surveyType SurveyType) (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, description string, surveyType SurveyType) (secrets.SourceMap, error) {
 	if surveyType != surveyCredentials && surveyType != surveyParameters {
 		return secrets.SourceMap{}, fmt.Errorf("unsupported survey type: %s", surveyType)
 	}
 
+	// if there is a description, append the newline to the end of it
+	// this prevents empty descriptions adding new lines
+	if description != "" {
+		description += "\n"
+	}
+
 	// 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),
+		Message: fmt.Sprintf("How would you like to set %s %q\n%s ", surveyType, name, description),
 		Options: []string{questionSecret, questionValue, questionEnvVar, questionPath, questionCommand},
 		Default: "environment variable",
 	}
diff --git a/pkg/generator/generator_test.go b/pkg/generator/generator_test.go
index 464fd2446..2f106689d 100644
--- a/pkg/generator/generator_test.go
+++ b/pkg/generator/generator_test.go
@@ -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)
 	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"))
 	require.EqualError(t, err, "unsupported survey type: turtles")
 	require.Equal(t, secrets.SourceMap{}, got)
 }
diff --git a/pkg/generator/parameters.go b/pkg/generator/parameters.go
index 3007fabc8..138d76602 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)
+		c, err := fn(name, opts.Bundle.Parameters[name].Description, surveyParameters)
 		if err != nil {
 			return pset, err
 		}