Skip to content

Commit

Permalink
Default value for opts (#100)
Browse files Browse the repository at this point in the history
add default value for opts
  • Loading branch information
kaplanelad authored May 16, 2022
1 parent 33860f9 commit 1a0012a
Show file tree
Hide file tree
Showing 3 changed files with 60 additions and 6 deletions.
35 changes: 31 additions & 4 deletions pkg/core/populate.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,11 @@ import (
"strings"
)

const (
populateFromEnvironment = "env:"
populateWithDefault = ","
)

type Opts map[string]string
type Populate struct {
opts map[string]string
Expand All @@ -21,9 +26,13 @@ func (p *Populate) FindAndReplace(path string) string {
populated := path
for k, v := range p.opts {
val := v
if strings.HasPrefix(v, "env:") {
evar := strings.TrimPrefix(v, "env:")
if strings.HasPrefix(v, populateFromEnvironment) {
evar := strings.TrimPrefix(v, populateFromEnvironment)
evar, defaultValue := p.parseDefaultValue(evar)
val = os.Getenv(evar)
if val == "" {
val = defaultValue
}
}
populated = strings.ReplaceAll(populated, fmt.Sprintf("{{%s}}", k), val)
}
Expand All @@ -35,11 +44,29 @@ func (p *Populate) KeyPath(kp KeyPath) KeyPath {
populated := path
for k, v := range p.opts {
val := v
if strings.HasPrefix(v, "env:") {
evar := strings.TrimPrefix(v, "env:")
if strings.HasPrefix(v, populateFromEnvironment) {
evar := strings.TrimPrefix(v, populateFromEnvironment)
evar, defaultValue := p.parseDefaultValue(evar)
val = os.Getenv(evar)
if val == "" {
val = defaultValue
}
}
populated = strings.ReplaceAll(populated, fmt.Sprintf("{{%s}}", k), val)
}
return kp.SwitchPath(path)
}

// parseDefaultValue returns that field name and the default value if `populateWithDefault` was found
// Example 1: FOO,BAR -> the function return FOO, BAR
// Example 2: FOO -> the function return FOO, "" (empty value)
func (p *Populate) parseDefaultValue(evar string) (key, defaultValue string) {

if strings.Contains(evar, populateWithDefault) {
data := strings.SplitN(evar, populateWithDefault, 2) //nolint
if len(data) == 2 { //nolint
return data[0], strings.TrimSpace(data[1])
}
}
return evar, ""
}
29 changes: 28 additions & 1 deletion pkg/core/populate_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,13 +8,14 @@ import (
)

func TestPopulatePath(t *testing.T) {
p := NewPopulate(map[string]string{"foo": "bar", "teller-env": "env:TELLER_TEST_FOO"})
p := NewPopulate(map[string]string{"foo": "bar", "teller-env": "env:TELLER_TEST_FOO", "teller-env-default": "env:TELLER_TEST_DEFAULT,default-value"})

os.Setenv("TELLER_TEST_FOO", "test-foo")
assert.Equal(t, p.FindAndReplace("foo/{{foo}}/qux/{{foo}}"), "foo/bar/qux/bar")
assert.Equal(t, p.FindAndReplace("foo/qux"), "foo/qux")
assert.Equal(t, p.FindAndReplace("foo/{{none}}"), "foo/{{none}}")
assert.Equal(t, p.FindAndReplace("foo/{{teller-env}}"), "foo/test-foo")
assert.Equal(t, p.FindAndReplace("foo/{{teller-env-default}}"), "foo/default-value")
assert.Equal(t, p.FindAndReplace(""), "")

kp := KeyPath{
Expand All @@ -41,4 +42,30 @@ func TestPopulatePath(t *testing.T) {
Decrypt: true,
Optional: true,
})
kp = KeyPath{
Path: "{{teller-env-default}}/hey",
Env: "env",
Decrypt: true,
Optional: true,
}
assert.Equal(t, p.KeyPath(kp), KeyPath{
Path: "default-value/hey",
Env: "env",
Decrypt: true,
Optional: true,
})
}

func TestPopulateDefaultValue(t *testing.T) {

p := NewPopulate(map[string]string{})

key, value := p.parseDefaultValue("TELLER_TEST_FOO")
assert.Equal(t, key, "TELLER_TEST_FOO")
assert.Equal(t, value, "")

key, value = p.parseDefaultValue("TELLER_TEST_FOO, default,,value")
assert.Equal(t, key, "TELLER_TEST_FOO")
assert.Equal(t, value, "default,,value")

}
2 changes: 1 addition & 1 deletion pkg/wizard_template.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ confirm: Are you sure you want to run on {{"{{stage}}"}}?
# paths.
#
opts:
region: env:AWS_REGION # you can get env variables with the 'env:' prefix
region: env:AWS_REGION # you can get env variables with the 'env:' prefix, for default values if env not found use comma. Example: env:AWS_REGION,{DEFAULT_VALUE}
stage: development
Expand Down

0 comments on commit 1a0012a

Please sign in to comment.