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 (
"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",

34
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) {

Loading…
Cancel
Save