Skip to content
This repository has been archived by the owner on Nov 15, 2022. It is now read-only.

Commit

Permalink
Merge pull request #616 from surajssd/default-value-token
Browse files Browse the repository at this point in the history
Default value having colon can be parsed now
  • Loading branch information
surajssd authored Apr 5, 2018
2 parents 0ff2f08 + 5f9bf2a commit 86b1562
Show file tree
Hide file tree
Showing 2 changed files with 56 additions and 12 deletions.
8 changes: 5 additions & 3 deletions pkg/cmd/util.go
Original file line number Diff line number Diff line change
Expand Up @@ -162,14 +162,16 @@ func replaceWithEnv(in []byte) []byte {
var found bool
// Splitting string with separator ":"
slice := strings.Split(name, ":")
// if slice has 2 elements, i.e given variable has default value
if len(slice) == 2 {
// if slice has 2 or more elements, i.e given variable has default value
// there could be more than two elements when the default value has one or
// more colons
if len(slice) >= 2 {
name = strings.TrimSpace(slice[0])
// look into environment for the value
value, found = os.LookupEnv(name)
// put default value if it's not present in environment
if !found && value == "" {
value = strings.TrimSpace(slice[1])
value = strings.TrimSpace(strings.Join(slice[1:], ":"))
}
} else {
// default value is not provided
Expand Down
60 changes: 51 additions & 9 deletions pkg/cmd/util_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ package cmd

import (
"bytes"
"fmt"
"io/ioutil"
"os"
"path/filepath"
Expand Down Expand Up @@ -136,16 +137,17 @@ func TestReplaceWithEnv(t *testing.T) {
}

func TestSubstituteVariables(t *testing.T) {
err := os.Setenv("TEST_IMAGE_TAG", "version")
if err != nil {
t.Error(err)
t.FailNow()
envs := map[string]string{
"TEST_IMAGE_TAG": "version",
"TEST_SERVICE_NAME": "name",
"WEB_APP_URL": "webapp:8080",
}

err = os.Setenv("TEST_SERVICE_NAME", "name")
if err != nil {
t.Error(err)
t.FailNow()
for k, v := range envs {
err := os.Setenv(k, v)
if err != nil {
t.Error(fmt.Errorf("error setting environment variable, %s=%s: %v", k, v, err))
t.FailNow()
}
}

tests := []struct {
Expand Down Expand Up @@ -276,6 +278,46 @@ func TestSubstituteVariables(t *testing.T) {
`),
false,
},
{
"Default value has colon",
[]byte(`
name: httpd
containers:
- image: foo/bar:version
env:
- name: APP_URL
value: [[ APP_URL:app:8080 ]]
`),
[]byte(`
name: httpd
containers:
- image: foo/bar:version
env:
- name: APP_URL
value: app:8080
`),
false,
},
{
"Default value has colon but variable is exported in environment",
[]byte(`
name: httpd
containers:
- image: foo/bar:version
env:
- name: APP_URL
value: [[ WEB_APP_URL:app:8080 ]]
`),
[]byte(`
name: httpd
containers:
- image: foo/bar:version
env:
- name: APP_URL
value: webapp:8080
`),
false,
},
}

for _, test := range tests {
Expand Down

0 comments on commit 86b1562

Please sign in to comment.