Skip to content

Commit

Permalink
Prepend flags, append positional args (#11)
Browse files Browse the repository at this point in the history
* Add tests

* add debug info
  • Loading branch information
osterman authored Jun 21, 2019
1 parent 0087f40 commit 7aa34fd
Show file tree
Hide file tree
Showing 7 changed files with 96 additions and 5 deletions.
6 changes: 6 additions & 0 deletions .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,11 @@ addons:
- make
- curl

before_install:
- sudo add-apt-repository ppa:duggan/bats --yes
- sudo apt-get update -qq
- sudo apt-get install -qq bats

install:
- make init
- make go/deps-build
Expand All @@ -19,6 +24,7 @@ script:
- make go/test
- make go/lint
- make go/build-all
- make test TF_ENV=../release/tfenv_linux_amd64
- ls -l release/

deploy:
Expand Down
7 changes: 5 additions & 2 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,9 @@ PATH:=$(PATH):$(GOPATH)/bin

-include $(shell curl -sSL -o .build-harness "https://git.io/build-harness"; echo .build-harness)

build: go/build
@exit 0
.PHONY : test
test:
$(MAKE) -C $(@)

release/tfenv: main.go
$(MAKE) go/build
10 changes: 7 additions & 3 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -81,10 +81,11 @@ func main() {

// Combine parameters into something like `-backend-config=role_arn=xxx`
arg = "-backend-config=" + arg
if len(pair[1]) > 0 && pair[1] != "true" {
if len(pair[1]) > 0 {
arg += "=" + pair[1]
}
tfCliArgsInit = append(tfCliArgsInit, arg)
// Prepend flags
tfCliArgsInit = append([]string{arg}, tfCliArgsInit...)
} else if reTfCliOption.MatchString(pair[0]) {
// `TF_CLI_ARGS_plan`: Map `TF_CLI_PLAN_SOMETHING=value` to `-something=value`
match := reTfCliOption.FindStringSubmatch(pair[0])
Expand All @@ -98,6 +99,7 @@ func main() {
if len(pair[1]) > 0 && pair[1] != "true" {
arg += "=" + pair[1]
}
// Prepend flags
switch cmd {
case "init":
tfCliArgsInit = append([]string{arg}, tfCliArgsInit...)
Expand Down Expand Up @@ -137,7 +139,9 @@ func main() {
if len(pair[1]) > 0 && pair[1] != "true" {
arg += "=" + pair[1]
}
tfCliArgs = append(tfCliArgs, arg)
// Prepend flags
tfCliArgs = append([]string{arg}, tfCliArgs...)

} else if !reBlacklist.MatchString(pair[0]) && reWhitelist.MatchString(pair[0]) {
// Process the blacklist for exclusions, then the whitelist for inclusions
// Strip off TF_VAR_ prefix so we can simplify normalization
Expand Down
2 changes: 2 additions & 0 deletions test/Makefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
all:
bats --tap .
20 changes: 20 additions & 0 deletions test/tf_cli_args_init.bats
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
function setup() {
export TF_CLI_INIT_FROM_MODULE="git::https://https://github.com/cloudposse/terraform-null-label?ref=master"
export TF_CLI_INIT_BACKEND=false
export TF_CLI_INIT="module/"
export TF_ENV=${TF_ENV:-../release/tfenv}
}

function teardown() {
unset TF_CLI_INIT_FROM_MODULE
unset TF_CLI_INIT_BACKEND
unset TF_CLI_INIT
unset TF_ENV
}

@test "TF_CLI_ARGS_init works" {
which ${TF_ENV}
${TF_ENV} printenv TF_CLI_ARGS_init >&2
[ "$(${TF_ENV} printenv TF_CLI_ARGS_init)" != "" ]
[ "$(${TF_ENV} printenv TF_CLI_ARGS_init)" == "-backend=${TF_CLI_INIT_BACKEND} -from-module=${TF_CLI_INIT_FROM_MODULE} ${TF_CLI_INIT}" ]
}
22 changes: 22 additions & 0 deletions test/tf_cli_args_plan.bats
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
function setup() {
export TF_CLI_PLAN_AUTO_APPROVE="true"
export TF_CLI_PLAN_NO_COLOR="true"
export TF_CLI_PLAN_OUT="plan.txt"
export TF_CLI_PLAN="module/"
export TF_ENV=${TF_ENV:-../release/tfenv}
}

function teardown() {
unset TF_CLI_PLAN_AUTO_APPROVE
unset TF_CLI_PLAN_NO_COLOR
unset TF_CLI_PLAN_OUT
unset TF_CLI_PLAN
unset TF_ENV
}

@test "TF_CLI_ARGS_plan works" {
which ${TF_ENV}
${TF_ENV} printenv TF_CLI_ARGS_plan >&2
[ "$(${TF_ENV} printenv TF_CLI_ARGS_plan)" != "" ]
[ "$(${TF_ENV} printenv TF_CLI_ARGS_plan)" == "-no-color -out=${TF_CLI_PLAN_OUT} -auto-approve $TF_CLI_PLAN" ]
}
34 changes: 34 additions & 0 deletions test/tf_var.bats
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
function setup() {
export FOOBAR=123
export Blah=true
export _something=good
export TF_ENV=${TF_ENV:-../release/tfenv}
}

function teardown() {
unset FOOBAR
unset Blah
unset _something
unset TF_ENV
}

@test "TF_VAR_foobar works" {
which ${TF_ENV}
${TF_ENV} printenv TF_VAR_foobar >&2
[ "$(${TF_ENV} printenv TF_VAR_foobar)" != "" ]
[ "$(${TF_ENV} printenv TF_VAR_foobar)" == "${FOOBAR}" ]
}

@test "TF_VAR_blah works" {
which ${TF_ENV}
${TF_ENV} printenv TF_VAR_blah >&2
[ "$(${TF_ENV} printenv TF_VAR_blah)" != "" ]
[ "$(${TF_ENV} printenv TF_VAR_blah)" == "${Blah}" ]
}

@test "TF_VAR_something works" {
which ${TF_ENV}
${TF_ENV} printenv TF_VAR_something >&2
[ "$(${TF_ENV} printenv TF_VAR_something)" != "" ]
[ "$(${TF_ENV} printenv TF_VAR_something)" == "${_something}" ]
}

0 comments on commit 7aa34fd

Please sign in to comment.