From 60fab7d3d7e654ffbbd439f7ac1219534f17be56 Mon Sep 17 00:00:00 2001 From: Greyson Dehn Date: Thu, 22 Dec 2016 11:29:41 -0600 Subject: [PATCH] Delete temp override file when done --- main.go | 4 ---- plugin.go | 34 ++++++++++++++++++++++++---------- 2 files changed, 24 insertions(+), 14 deletions(-) diff --git a/main.go b/main.go index 5298f5e..f8be5fe 100644 --- a/main.go +++ b/main.go @@ -2,7 +2,6 @@ package main import ( "encoding/json" - "fmt" "os" "github.com/Sirupsen/logrus" @@ -69,18 +68,15 @@ func main() { Usage: "The number of concurrent operations as Terraform walks its graph", EnvVar: "PLUGIN_PARALLELISM", }, - cli.StringFlag{ Name: "env-file", Usage: "source env file", }, - cli.StringSliceFlag{ Name: "targets", Usage: "targets to run apply or plan on", EnvVar: "PLUGIN_TARGETS", }, - cli.StringFlag{ Name: "submodules", Usage: "submodules to override", diff --git a/plugin.go b/plugin.go index ae77b7b..7ac7f76 100644 --- a/plugin.go +++ b/plugin.go @@ -53,8 +53,13 @@ func (p Plugin) Exec() error { exportSecrets(p.Config.Secrets) } + var overridesFileName string if len(p.Config.Submodules) != 0 { - submoduleOverride(p.Config.Submodules) + overridesFileName = submoduleOverride(p.Config.Submodules) + + if overridesFileName != "" { + defer os.Remove(overridesFileName) + } } if p.Config.Cacert != "" { @@ -106,28 +111,37 @@ func installCaCert(cacert string) *exec.Cmd { ) } -func submoduleOverride(submodules map[string]map[string]string) { +func submoduleOverride(submodules map[string]map[string]string) string { allOverrides := []string{} + for moduleName, override := range submodules { overrideContents := []string{} for k, v := range override { - overrideString := fmt.Sprintf(` %s = "%s"`, k, v) - overrideContents = append(overrideContents, overrideString) + overrideContents = append(overrideContents, fmt.Sprintf(` %s = "%s"`, k, v)) } - moduleContents := fmt.Sprintf(` -module "%s" { -%s -}`, moduleName, strings.Join(overrideContents, "\n")) + moduleContents := fmt.Sprintf("module \"%s\" {\n%s\n}", moduleName, strings.Join(overrideContents, "\n")) allOverrides = append(allOverrides, moduleContents) } + fileContents := []byte(strings.Join(allOverrides, "\n")) + randBytes := make([]byte, 16) rand.Read(randBytes) fileName := hex.EncodeToString(randBytes) + "_override.tf" - err := ioutil.WriteFile(fileName, fileContents, 0644) - if err != nil { + + if err := ioutil.WriteFile(fileName, fileContents, 0644); err != nil { panic(err) } + + return fileName +} + +func removeOverridesFile(fileName string) *exec.Cmd { + return exec.Command( + "rm", + "-f", + fileName, + ) } func exportSecrets(secrets map[string]string) {