mirror of
https://github.com/thegeeklab/wp-opentofu.git
synced 2024-11-24 23:30:39 +00:00
Remove secrets handling and add copying TF_VAR_ vars to lowercase names
This commit is contained in:
parent
551bdf8b83
commit
69e77144a1
20
plugin.go
20
plugin.go
@ -5,6 +5,7 @@ import (
|
|||||||
"io/ioutil"
|
"io/ioutil"
|
||||||
"os"
|
"os"
|
||||||
"os/exec"
|
"os/exec"
|
||||||
|
"regexp"
|
||||||
"strings"
|
"strings"
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
@ -53,9 +54,7 @@ func (p Plugin) Exec() error {
|
|||||||
|
|
||||||
var commands []*exec.Cmd
|
var commands []*exec.Cmd
|
||||||
|
|
||||||
if len(p.Config.Secrets) != 0 {
|
CopyTfEnv()
|
||||||
exportSecrets(p.Config.Secrets)
|
|
||||||
}
|
|
||||||
|
|
||||||
if p.Config.Cacert != "" {
|
if p.Config.Cacert != "" {
|
||||||
commands = append(commands, installCaCert(p.Config.Cacert))
|
commands = append(commands, installCaCert(p.Config.Cacert))
|
||||||
@ -108,9 +107,14 @@ func installCaCert(cacert string) *exec.Cmd {
|
|||||||
)
|
)
|
||||||
}
|
}
|
||||||
|
|
||||||
func exportSecrets(secrets map[string]string) {
|
func CopyTfEnv() {
|
||||||
for k, v := range secrets {
|
tfVar := regexp.MustCompile(`^TF_VAR_.*$`)
|
||||||
os.Setenv(fmt.Sprintf("%s", k), fmt.Sprintf("%s", os.Getenv(v)))
|
for _, e := range os.Environ() {
|
||||||
|
pair := strings.Split(e, "=")
|
||||||
|
if tfVar.MatchString(pair[0]) {
|
||||||
|
name := strings.Split(pair[0], "TF_VAR_")
|
||||||
|
os.Setenv(fmt.Sprintf("TF_VAR_%s", strings.ToLower(name[1])), pair[1])
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -187,10 +191,6 @@ func planCommand(config Config) *exec.Cmd {
|
|||||||
args = append(args, "-var")
|
args = append(args, "-var")
|
||||||
args = append(args, fmt.Sprintf("%s=%s", k, v))
|
args = append(args, fmt.Sprintf("%s=%s", k, v))
|
||||||
}
|
}
|
||||||
for k, v := range config.Secrets {
|
|
||||||
args = append(args, "-var")
|
|
||||||
args = append(args, fmt.Sprintf("%s=%s", k, os.Getenv(v)))
|
|
||||||
}
|
|
||||||
if config.Parallelism > 0 {
|
if config.Parallelism > 0 {
|
||||||
args = append(args, fmt.Sprintf("-parallelism=%d", config.Parallelism))
|
args = append(args, fmt.Sprintf("-parallelism=%d", config.Parallelism))
|
||||||
}
|
}
|
||||||
|
@ -1,9 +1,12 @@
|
|||||||
package main
|
package main
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"os"
|
||||||
"os/exec"
|
"os/exec"
|
||||||
"reflect"
|
"reflect"
|
||||||
"testing"
|
"testing"
|
||||||
|
|
||||||
|
. "github.com/franela/goblin"
|
||||||
)
|
)
|
||||||
|
|
||||||
func Test_destroyCommand(t *testing.T) {
|
func Test_destroyCommand(t *testing.T) {
|
||||||
@ -131,3 +134,21 @@ func Test_planCommand(t *testing.T) {
|
|||||||
})
|
})
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func TestPlugin(t *testing.T) {
|
||||||
|
g := Goblin(t)
|
||||||
|
|
||||||
|
g.Describe("CopyTfEnv", func() {
|
||||||
|
g.It("Should create copies of TF_VAR_ to lowercase", func() {
|
||||||
|
// Set some initial TF_VAR_ that are uppercase
|
||||||
|
os.Setenv("TF_VAR_SOMETHING", "some value")
|
||||||
|
os.Setenv("TF_VAR_SOMETHING_ELSE", "some other value")
|
||||||
|
|
||||||
|
CopyTfEnv()
|
||||||
|
|
||||||
|
// Make sure new env vars exist with proper values
|
||||||
|
g.Assert(os.Getenv("TF_VAR_something")).Equal("some value")
|
||||||
|
g.Assert(os.Getenv("TF_VAR_something_else")).Equal("some other value")
|
||||||
|
})
|
||||||
|
})
|
||||||
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user