From 5b284af1156b0c4cd745685b02d88fc65fe51f46 Mon Sep 17 00:00:00 2001 From: stormcat24 Date: Sun, 19 Jun 2016 18:18:23 +0900 Subject: [PATCH 1/2] support default parameter value --- README.md | 9 +++++++++ operation/version.go | 2 +- util/util.go | 22 ++++++++++++++++++---- util/util_test.go | 33 +++++++++++++++++++++++++++++++++ 4 files changed, 61 insertions(+), 5 deletions(-) diff --git a/README.md b/README.md index 066aee0..ea934e5 100644 --- a/README.md +++ b/README.md @@ -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 diff --git a/operation/version.go b/operation/version.go index 5669170..185f5e1 100644 --- a/operation/version.go +++ b/operation/version.go @@ -1,3 +1,3 @@ package operation -const Version string = "0.2.0-RC6" +const Version string = "0.2.0-RC7" diff --git a/util/util.go b/util/util.go index 587ca04..1a58a70 100644 --- a/util/util.go +++ b/util/util.go @@ -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 { @@ -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] diff --git a/util/util_test.go b/util/util_test.go index 5005051..8255584 100644 --- a/util/util_test.go +++ b/util/util_test.go @@ -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) + } + +} From 7eb3c56fd49c5b2eb8127e7e2b9399a06f3ad816 Mon Sep 17 00:00:00 2001 From: stormcat24 Date: Sun, 19 Jun 2016 18:24:48 +0900 Subject: [PATCH 2/2] fix_test --- Makefile | 2 +- task/task_test.go | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/Makefile b/Makefile index 7fcdc7a..555321d 100644 --- a/Makefile +++ b/Makefile @@ -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 "**********************************************************" diff --git a/task/task_test.go b/task/task_test.go index a0f7741..e7e68d0 100644 --- a/task/task_test.go +++ b/task/task_test.go @@ -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) }