Skip to content

Commit 1a0012a

Browse files
authored
Default value for opts (#100)
add default value for opts
1 parent 33860f9 commit 1a0012a

File tree

3 files changed

+60
-6
lines changed

3 files changed

+60
-6
lines changed

pkg/core/populate.go

Lines changed: 31 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,11 @@ import (
66
"strings"
77
)
88

9+
const (
10+
populateFromEnvironment = "env:"
11+
populateWithDefault = ","
12+
)
13+
914
type Opts map[string]string
1015
type Populate struct {
1116
opts map[string]string
@@ -21,9 +26,13 @@ func (p *Populate) FindAndReplace(path string) string {
2126
populated := path
2227
for k, v := range p.opts {
2328
val := v
24-
if strings.HasPrefix(v, "env:") {
25-
evar := strings.TrimPrefix(v, "env:")
29+
if strings.HasPrefix(v, populateFromEnvironment) {
30+
evar := strings.TrimPrefix(v, populateFromEnvironment)
31+
evar, defaultValue := p.parseDefaultValue(evar)
2632
val = os.Getenv(evar)
33+
if val == "" {
34+
val = defaultValue
35+
}
2736
}
2837
populated = strings.ReplaceAll(populated, fmt.Sprintf("{{%s}}", k), val)
2938
}
@@ -35,11 +44,29 @@ func (p *Populate) KeyPath(kp KeyPath) KeyPath {
3544
populated := path
3645
for k, v := range p.opts {
3746
val := v
38-
if strings.HasPrefix(v, "env:") {
39-
evar := strings.TrimPrefix(v, "env:")
47+
if strings.HasPrefix(v, populateFromEnvironment) {
48+
evar := strings.TrimPrefix(v, populateFromEnvironment)
49+
evar, defaultValue := p.parseDefaultValue(evar)
4050
val = os.Getenv(evar)
51+
if val == "" {
52+
val = defaultValue
53+
}
4154
}
4255
populated = strings.ReplaceAll(populated, fmt.Sprintf("{{%s}}", k), val)
4356
}
4457
return kp.SwitchPath(path)
4558
}
59+
60+
// parseDefaultValue returns that field name and the default value if `populateWithDefault` was found
61+
// Example 1: FOO,BAR -> the function return FOO, BAR
62+
// Example 2: FOO -> the function return FOO, "" (empty value)
63+
func (p *Populate) parseDefaultValue(evar string) (key, defaultValue string) {
64+
65+
if strings.Contains(evar, populateWithDefault) {
66+
data := strings.SplitN(evar, populateWithDefault, 2) //nolint
67+
if len(data) == 2 { //nolint
68+
return data[0], strings.TrimSpace(data[1])
69+
}
70+
}
71+
return evar, ""
72+
}

pkg/core/populate_test.go

Lines changed: 28 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,13 +8,14 @@ import (
88
)
99

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

1313
os.Setenv("TELLER_TEST_FOO", "test-foo")
1414
assert.Equal(t, p.FindAndReplace("foo/{{foo}}/qux/{{foo}}"), "foo/bar/qux/bar")
1515
assert.Equal(t, p.FindAndReplace("foo/qux"), "foo/qux")
1616
assert.Equal(t, p.FindAndReplace("foo/{{none}}"), "foo/{{none}}")
1717
assert.Equal(t, p.FindAndReplace("foo/{{teller-env}}"), "foo/test-foo")
18+
assert.Equal(t, p.FindAndReplace("foo/{{teller-env-default}}"), "foo/default-value")
1819
assert.Equal(t, p.FindAndReplace(""), "")
1920

2021
kp := KeyPath{
@@ -41,4 +42,30 @@ func TestPopulatePath(t *testing.T) {
4142
Decrypt: true,
4243
Optional: true,
4344
})
45+
kp = KeyPath{
46+
Path: "{{teller-env-default}}/hey",
47+
Env: "env",
48+
Decrypt: true,
49+
Optional: true,
50+
}
51+
assert.Equal(t, p.KeyPath(kp), KeyPath{
52+
Path: "default-value/hey",
53+
Env: "env",
54+
Decrypt: true,
55+
Optional: true,
56+
})
57+
}
58+
59+
func TestPopulateDefaultValue(t *testing.T) {
60+
61+
p := NewPopulate(map[string]string{})
62+
63+
key, value := p.parseDefaultValue("TELLER_TEST_FOO")
64+
assert.Equal(t, key, "TELLER_TEST_FOO")
65+
assert.Equal(t, value, "")
66+
67+
key, value = p.parseDefaultValue("TELLER_TEST_FOO, default,,value")
68+
assert.Equal(t, key, "TELLER_TEST_FOO")
69+
assert.Equal(t, value, "default,,value")
70+
4471
}

pkg/wizard_template.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ confirm: Are you sure you want to run on {{"{{stage}}"}}?
1717
# paths.
1818
#
1919
opts:
20-
region: env:AWS_REGION # you can get env variables with the 'env:' prefix
20+
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}
2121
stage: development
2222
2323

0 commit comments

Comments
 (0)