Skip to content

Commit

Permalink
Merge pull request #82 from stormcat24/feature/default_param
Browse files Browse the repository at this point in the history
support default parameter value
  • Loading branch information
stormcat24 committed Jun 19, 2016
2 parents efdd761 + 7eb3c56 commit 6119445
Show file tree
Hide file tree
Showing 6 changed files with 63 additions and 7 deletions.
2 changes: 1 addition & 1 deletion Makefile
Expand Up @@ -38,7 +38,7 @@ $(TEST_TARGETS): test-%:
GO15VENDOREXPERIMENT=1 go test -v -covermode=atomic -coverprofile=coverage.out $(GOTEST_FLAGS) $(BASE_PACKAGE)/$(*)
GO15VENDOREXPERIMENT=1 go test -v -run=nonthing -benchmem -bench=".*" $(GOTEST_FLAGS) $(BASE_PACKAGE)/$(*)

ci-test: $(CI_TEST_TARGETS)
ci-test: $(TEST_TARGETS)

$(CI_TEST_TARGETS): ci-test-%:
@echo "**********************************************************"
Expand Down
9 changes: 9 additions & 0 deletions README.md
Expand Up @@ -234,6 +234,15 @@ You can set value for these parameters by using `-p` option.
ecs-formation task -p NGINX_VERSION=1.0 -p NGINX_PORT=80 plan your-web-task
```
Also, support default parameter value.
```Ruby
nginx:
image: stormcat24/nginx:${NGINX_VERSION|latest}
ports:
- 80:${NGINX_PORT|80}
```
#### env_file
You can use `env_file` like docker-compose. https://docs.docker.com/compose/compose-file/#env-file
Expand Down
2 changes: 1 addition & 1 deletion operation/version.go
@@ -1,3 +1,3 @@
package operation

const Version string = "0.2.0-RC6"
const Version string = "0.2.0-RC7"
2 changes: 1 addition & 1 deletion task/task_test.go
Expand Up @@ -385,7 +385,7 @@ nginx:
essential: true
`, f.Name())

taskdef, err := CreateTaskDefinition("test-web", yaml, filepath.Dir(f.Name()))
taskdef, err := CreateTaskDefinition("test-web", yaml, filepath.Dir(f.Name()), nil)
if err != nil {
t.Fatal(err)
}
Expand Down
22 changes: 18 additions & 4 deletions util/util.go
Expand Up @@ -4,15 +4,17 @@ import (
"bufio"
"bytes"
"fmt"
"github.com/aws/aws-sdk-go/aws/awsutil"
"io"
"regexp"
"strings"

"github.com/aws/aws-sdk-go/aws/awsutil"
)

var (
keyValuePattern = regexp.MustCompile(`^\s*(.+)\s*=\s*(.+)\s*$`)
variablePattern = regexp.MustCompile(`\$\{([\w_-]+)\}`)
keyValuePattern = regexp.MustCompile(`^\s*(.+)\s*=\s*(.+)\s*$`)
variablePattern = regexp.MustCompile(`\$\{([\w_-]+)\}`)
defaultVariablePattern = regexp.MustCompile(`\$\{([\w_-]+)\s*\|\s*([\w_-]+)\}`)
)

func StringValueWithIndent(value interface{}, indent int) string {
Expand Down Expand Up @@ -68,8 +70,20 @@ func ParseKeyValues(slice []string) map[string]string {
func MergeYamlWithParameters(content []byte, params map[string]string) string {

s := string(content)
matched := variablePattern.FindAllStringSubmatch(s, -1)

defMatched := defaultVariablePattern.FindAllStringSubmatch(s, -1)
for _, tokens := range defMatched {
key := tokens[1]
defVal := tokens[2]

if value, ok := params[key]; ok {
s = strings.Replace(s, tokens[0], value, -1)
} else {
s = strings.Replace(s, tokens[0], defVal, -1)
}
}

matched := variablePattern.FindAllStringSubmatch(s, -1)
for _, tokens := range matched {
key := tokens[1]

Expand Down
33 changes: 33 additions & 0 deletions util/util_test.go
Expand Up @@ -85,3 +85,36 @@ func TestMergeYamlWithParameters(t *testing.T) {
}

}

func TestMergeYamlWithDefaultParameters(t *testing.T) {

yaml := `
nginx:
image: stormcat24/nginx:${NGINX_VERSION|feature}
ports:
- 80:${NGINX_PORT|80}
environment:
PARAM: "${PARAM}"
`

expect := `
nginx:
image: stormcat24/nginx:feature
ports:
- 80:8080
environment:
PARAM: "hogehoge"
`

params := map[string]string{
"NGINX_PORT": "8080",
"PARAM": "hogehoge",
}

actual := MergeYamlWithParameters([]byte(yaml), params)

if expect != actual {
t.Errorf("actula merged string is %v", actual)
}

}

0 comments on commit 6119445

Please sign in to comment.