Skip to content

Commit

Permalink
add ENUM type input
Browse files Browse the repository at this point in the history
  • Loading branch information
zreigz committed Oct 24, 2024
1 parent f970b8e commit 3a3912b
Show file tree
Hide file tree
Showing 8 changed files with 130 additions and 3 deletions.
1 change: 1 addition & 0 deletions pkg/api/models.go
Original file line number Diff line number Diff line change
Expand Up @@ -251,6 +251,7 @@ type ConfigurationItem struct {
Optional bool
Condition *Condition
Validation *Validation
Values []string
}

type Artifact struct {
Expand Down
17 changes: 17 additions & 0 deletions pkg/bundle/configuration.go
Original file line number Diff line number Diff line change
Expand Up @@ -199,6 +199,23 @@ func Configure(ctx map[string]interface{}, item *api.ConfigurationItem, context
return err
}
ctx[item.Name] = contents

case Enum:
var res string
if len(item.Values) == 0 {
return fmt.Errorf("no values defined for %s", item.Name)
}
if value := getEnvVar(repo, item.Name); value != "" {
res = value
} else {
prompt, opts := enumSurvey(def, item)
err = survey.AskOne(prompt, &res, opts...)
if err != nil {
return err
}
}

ctx[item.Name] = res
}

return
Expand Down
14 changes: 14 additions & 0 deletions pkg/bundle/configuration_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,20 @@ func TestConfigureEnvVariables(t *testing.T) {
expectedValue string
envVars map[string]string
}{
{
name: "test enum item",
item: &api.ConfigurationItem{
Name: "test_item",
Default: "",
Type: bundle.Enum,
Values: []string{"a", "b", "c"},
},
context: &manifest.Context{},
ctx: map[string]interface{}{},
repo: "test",
envVars: map[string]string{"PLURAL_TEST_TEST_ITEM": "a"},
expectedValue: "a",
},
{
name: "test integer item",
item: &api.ConfigurationItem{
Expand Down
21 changes: 18 additions & 3 deletions pkg/bundle/surveys.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@ import (
"github.com/mitchellh/go-homedir"
)

const enterTheValue = "Enter the value"

func stringValidator(item *api.ConfigurationItem) survey.AskOpt {
return survey.WithValidator(func(val interface{}) error {
res, _ := val.(string)
Expand All @@ -33,7 +35,7 @@ func stringSurvey(def string, item *api.ConfigurationItem) (survey.Prompt, []sur
}

return &survey.Input{
Message: "Enter the value",
Message: enterTheValue,
Default: def,
}, opts
}
Expand All @@ -45,7 +47,7 @@ func passwordSurvey(item *api.ConfigurationItem) (survey.Prompt, []survey.AskOpt
opts = append(opts, survey.WithValidator(survey.Required))
}

return &survey.Password{Message: "Enter the value"}, opts
return &survey.Password{Message: enterTheValue}, opts
}

func boolSurvey() (survey.Prompt, []survey.AskOpt) {
Expand All @@ -54,7 +56,7 @@ func boolSurvey() (survey.Prompt, []survey.AskOpt) {

func intSurvey(def string) (survey.Prompt, []survey.AskOpt) {
return &survey.Input{
Message: "Enter the value",
Message: enterTheValue,
Default: def,
}, []survey.AskOpt{survey.WithValidator(survey.Required)}
}
Expand Down Expand Up @@ -108,6 +110,19 @@ func fileSurvey(def string, item *api.ConfigurationItem) (prompt survey.Prompt,
return
}

func enumSurvey(def string, item *api.ConfigurationItem) (input survey.Prompt, opts []survey.AskOpt) {
opts = []survey.AskOpt{survey.WithValidator(survey.Required)}
if def == "" && len(item.Values) > 0 {
def = item.Values[0]
}
input = &survey.Select{
Message: enterTheValue,
Default: def,
Options: item.Values,
}
return
}

func CleanPath(path string) string {
if strings.HasSuffix(path, "/") {
return path
Expand Down
1 change: 1 addition & 0 deletions pkg/bundle/types.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,4 +9,5 @@ const (
File = "FILE"
Function = "FUNCTION"
Password = "PASSWORD"
Enum = "ENUM"
)
5 changes: 5 additions & 0 deletions pkg/pr/crd.go
Original file line number Diff line number Diff line change
Expand Up @@ -117,6 +117,11 @@ func configuration(pr *v1alpha1.PrAutomation, contextFile string) (map[string]in
}
ci.Validation = validation
}
if len(t.Values) > 0 {
ci.Values = algorithms.Map(t.Values, func(t *string) string {
return *t
})
}
return ci
})
utils.Highlight("Lets' fill out the configuration for this PR automation:\n")
Expand Down
39 changes: 39 additions & 0 deletions pkg/pr/crd_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
package pr_test

import (
"os"
"testing"

"github.com/pluralsh/plural-cli/pkg/pr"
"github.com/stretchr/testify/assert"
)

func TestBuildCRD(t *testing.T) {
tests := []struct {
name string
path string
envs map[string]string
expectedCtx map[string]interface{}
}{
{
name: "test PR automation",
path: "../../test/prautomation/prautomations.yaml",
envs: map[string]string{"PLURAL__NAME": "test", "PLURAL__REGION": "eu-central-1", "PLURAL__TYPE": "s3"},
expectedCtx: map[string]interface{}{
"name": "test", "region": "eu-central-1", "type": "s3",
},
},
}
for _, test := range tests {
t.Run(test.name, func(t *testing.T) {
for k, v := range test.envs {
os.Setenv(k, v)
}

prTemplate, err := pr.BuildCRD(test.path, "")
assert.NoError(t, err)
assert.Equal(t, test.expectedCtx, prTemplate.Context)

})
}
}
35 changes: 35 additions & 0 deletions test/prautomation/prautomations.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
apiVersion: deployments.plural.sh/v1alpha1
kind: PrAutomation
metadata:
name: blob-creator
spec:
name: blob-creator
documentation: |
Sets up a PR to provision a blobstore with a given type (eg s3) and region
creates:
templates:
- source: templates/blob/stack.yaml
destination: "services/blobstores/{{ context.type }}/{{ context.name }}.yaml"
external: false
- source: templates/blob/service.yaml
destination: "bootstrap/blobstores.yaml"
external: false
scmConnectionRef:
name: github
title: "Adding a {{ context.type }} bucket {{ context.name }}"
message: "Setup a stack to manage the {{ context.name }} {{ context.type }} bucket"
identifier: your-org/your-plural-up-repo
configuration:
- name: name
type: STRING
documentation: the name of this blob store (if using s3, this would become an s3 bucket name)
validation:
regex: "[a-z][a-z-0-9]+"
- name: type
type: ENUM
documentation: the type of blob storage to provision
values:
- s3
- name: region
type: STRING
documentation: the region your blobstore will live in

0 comments on commit 3a3912b

Please sign in to comment.