From eede51fea61dcb5092bc4c37391de73bf76e45a1 Mon Sep 17 00:00:00 2001 From: Nic Maki Date: Mon, 6 Feb 2017 18:39:00 -0600 Subject: [PATCH] added ability to pass -var-file args to plan --- DOCS.md | 4 ++++ main.go | 7 +++++++ plugin.go | 19 ++++++++++++------- 3 files changed, 23 insertions(+), 7 deletions(-) diff --git a/DOCS.md b/DOCS.md index 53e7479..fb8990a 100644 --- a/DOCS.md +++ b/DOCS.md @@ -188,6 +188,10 @@ vars : a map of variables to pass to the Terraform `plan` and `apply` commands. Each value is passed as a `-var =` option. +var_files +: a list of variable files to pass to the Terraform `plan` and `apply` commands. +Each value is passed as a `-var-file ` option. + secrets : a map of variables to pass to the Terraform `plan` and `apply` commands as well as setting envvars. The `key` is the var and ENV to set. The `value` is the ENV to read the value from. diff --git a/main.go b/main.go index 6f609e0..5d00736 100644 --- a/main.go +++ b/main.go @@ -79,6 +79,12 @@ func main() { Usage: "targets to run apply or plan on", EnvVar: "PLUGIN_TARGETS", }, + + cli.StringSliceFlag{ + Name: "var_files", + Usage: "a list of var files to use. Each value is passed as -var-file=", + EnvVar: "PLUGIN_VAR_FILES", + }, } if err := app.Run(os.Args); err != nil { @@ -123,6 +129,7 @@ func run(c *cli.Context) error { RootDir: c.String("root_dir"), Parallelism: c.Int("parallelism"), Targets: c.StringSlice("targets"), + VarFiles: c.StringSlice("var_files"), }, } diff --git a/plugin.go b/plugin.go index b90d39e..3c0f62e 100644 --- a/plugin.go +++ b/plugin.go @@ -2,16 +2,17 @@ package main import ( "fmt" - "github.com/Sirupsen/logrus" - "github.com/aws/aws-sdk-go/aws/credentials" - "github.com/aws/aws-sdk-go/aws/credentials/stscreds" - "github.com/aws/aws-sdk-go/aws/session" - "github.com/aws/aws-sdk-go/service/sts" "io/ioutil" "os" "os/exec" "strings" "time" + + "github.com/Sirupsen/logrus" + "github.com/aws/aws-sdk-go/aws/credentials" + "github.com/aws/aws-sdk-go/aws/credentials/stscreds" + "github.com/aws/aws-sdk-go/aws/session" + "github.com/aws/aws-sdk-go/service/sts" ) type ( @@ -26,6 +27,7 @@ type ( RootDir string Parallelism int Targets []string + VarFiles []string } Remote struct { @@ -59,7 +61,7 @@ func (p Plugin) Exec() error { } commands = append(commands, getModules()) commands = append(commands, validateCommand()) - commands = append(commands, planCommand(p.Config.Vars, p.Config.Secrets, p.Config.Parallelism, p.Config.Targets)) + commands = append(commands, planCommand(p.Config.Vars, p.Config.Secrets, p.Config.Parallelism, p.Config.Targets, p.Config.VarFiles)) if !p.Config.Plan { commands = append(commands, applyCommand(p.Config.Parallelism, p.Config.Targets)) } @@ -146,7 +148,7 @@ func validateCommand() *exec.Cmd { ) } -func planCommand(variables map[string]string, secrets map[string]string, parallelism int, targets []string) *exec.Cmd { +func planCommand(variables map[string]string, secrets map[string]string, parallelism int, targets []string, varFiles []string) *exec.Cmd { args := []string{ "plan", "-out=plan.tfout", @@ -154,6 +156,9 @@ func planCommand(variables map[string]string, secrets map[string]string, paralle for _, v := range targets { args = append(args, "--target", fmt.Sprintf("%s", v)) } + for _, v := range varFiles { + args = append(args, "-var-file", fmt.Sprintf("%s", v)) + } for k, v := range variables { args = append(args, "-var") args = append(args, fmt.Sprintf("%s=%s", k, v))