Browse Source

Delete temp override file when done

pull/32/head
Greyson Dehn 8 years ago
parent
commit
60fab7d3d7
  1. 4
      main.go
  2. 34
      plugin.go

4
main.go

@ -2,7 +2,6 @@ package main
import ( import (
"encoding/json" "encoding/json"
"fmt"
"os" "os"
"github.com/Sirupsen/logrus" "github.com/Sirupsen/logrus"
@ -69,18 +68,15 @@ func main() {
Usage: "The number of concurrent operations as Terraform walks its graph", Usage: "The number of concurrent operations as Terraform walks its graph",
EnvVar: "PLUGIN_PARALLELISM", EnvVar: "PLUGIN_PARALLELISM",
}, },
cli.StringFlag{ cli.StringFlag{
Name: "env-file", Name: "env-file",
Usage: "source env file", Usage: "source env file",
}, },
cli.StringSliceFlag{ cli.StringSliceFlag{
Name: "targets", Name: "targets",
Usage: "targets to run apply or plan on", Usage: "targets to run apply or plan on",
EnvVar: "PLUGIN_TARGETS", EnvVar: "PLUGIN_TARGETS",
}, },
cli.StringFlag{ cli.StringFlag{
Name: "submodules", Name: "submodules",
Usage: "submodules to override", Usage: "submodules to override",

34
plugin.go

@ -53,8 +53,13 @@ func (p Plugin) Exec() error {
exportSecrets(p.Config.Secrets) exportSecrets(p.Config.Secrets)
} }
var overridesFileName string
if len(p.Config.Submodules) != 0 { 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 != "" { 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{} allOverrides := []string{}
for moduleName, override := range submodules { for moduleName, override := range submodules {
overrideContents := []string{} overrideContents := []string{}
for k, v := range override { for k, v := range override {
overrideString := fmt.Sprintf(` %s = "%s"`, k, v) overrideContents = append(overrideContents, fmt.Sprintf(` %s = "%s"`, k, v))
overrideContents = append(overrideContents, overrideString)
} }
moduleContents := fmt.Sprintf(` moduleContents := fmt.Sprintf("module \"%s\" {\n%s\n}", moduleName, strings.Join(overrideContents, "\n"))
module "%s" {
%s
}`, moduleName, strings.Join(overrideContents, "\n"))
allOverrides = append(allOverrides, moduleContents) allOverrides = append(allOverrides, moduleContents)
} }
fileContents := []byte(strings.Join(allOverrides, "\n")) fileContents := []byte(strings.Join(allOverrides, "\n"))
randBytes := make([]byte, 16) randBytes := make([]byte, 16)
rand.Read(randBytes) rand.Read(randBytes)
fileName := hex.EncodeToString(randBytes) + "_override.tf" 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) panic(err)
} }
return fileName
}
func removeOverridesFile(fileName string) *exec.Cmd {
return exec.Command(
"rm",
"-f",
fileName,
)
} }
func exportSecrets(secrets map[string]string) { func exportSecrets(secrets map[string]string) {

Loading…
Cancel
Save