From d11ee9dc0a2a73cb3d1c162e7c49c00cdade64fd Mon Sep 17 00:00:00 2001 From: Jacob McCann Date: Tue, 31 Jul 2018 13:53:20 -0500 Subject: [PATCH] refactor generating args for vars and var-files --- plugin.go | 32 ++++++++++++++++++++------------ 1 file changed, 20 insertions(+), 12 deletions(-) diff --git a/plugin.go b/plugin.go index 07c4b5d..7887143 100644 --- a/plugin.go +++ b/plugin.go @@ -256,12 +256,8 @@ func tfDestroy(config Config) *exec.Cmd { for _, v := range config.Targets { args = append(args, fmt.Sprintf("-target=%s", v)) } - for _, v := range config.VarFiles { - args = append(args, fmt.Sprintf("-var-file=%s", v)) - } - for k, v := range config.Vars { - args = append(args, "-var", fmt.Sprintf("%s=%s", k, v)) - } + args = append(args, varFiles(config.VarFiles)...) + args = append(args, vars(config.Vars)...) if config.Parallelism > 0 { args = append(args, fmt.Sprintf("-parallelism=%d", config.Parallelism)) } @@ -292,12 +288,8 @@ func tfPlan(config Config, destroy bool) *exec.Cmd { for _, v := range config.Targets { args = append(args, "--target", fmt.Sprintf("%s", v)) } - for _, v := range config.VarFiles { - args = append(args, fmt.Sprintf("-var-file=%s", v)) - } - for k, v := range config.Vars { - args = append(args, "-var", fmt.Sprintf("%s=%s", k, v)) - } + args = append(args, varFiles(config.VarFiles)...) + args = append(args, vars(config.Vars)...) if config.Parallelism > 0 { args = append(args, fmt.Sprintf("-parallelism=%d", config.Parallelism)) } @@ -329,6 +321,22 @@ func tfValidate(config Config) *exec.Cmd { ) } +func vars(vs map[string]string) []string { + var args []string + for k, v := range vs { + args = append(args, "-var", fmt.Sprintf("%s=%s", k, v)) + } + return args +} + +func varFiles(vfs []string) []string { + var args []string + for _, v := range vfs { + args = append(args, fmt.Sprintf("-var-file=%s", v)) + } + return args +} + // helper function to write a netrc file. // The following code comes from the official Git plugin for Drone: // https://github.com/drone-plugins/drone-git/blob/8386effd2fe8c8695cf979427f8e1762bd805192/utils.go#L43-L68