0
0
mirror of https://github.com/thegeeklab/wp-opentofu.git synced 2024-11-22 00:30:40 +00:00

Configure terraform commands, don't allow any command.

This commit is contained in:
John Engelman 2015-11-09 19:07:05 -06:00
parent 7ab6c29d3e
commit 991e651835

77
main.go
View File

@ -9,33 +9,44 @@ import (
"github.com/drone/drone-plugin-go/plugin" "github.com/drone/drone-plugin-go/plugin"
) )
type Terraform struct { type terraform struct {
Commands []string `json:"commands"` Remote remote `json:"remote"`
DryRun bool `json:"dryRun"`
Vars map[string]string `json:"vars"`
}
type remote struct {
Backend string `json:"backend"`
Config map[string]string `json:"config"`
} }
func main() { func main() {
workspace := plugin.Workspace{} workspace := plugin.Workspace{}
vargs := Terraform{} vargs := terraform{}
plugin.Param("workspace", &workspace) plugin.Param("workspace", &workspace)
plugin.Param("vargs", &vargs) plugin.Param("vargs", &vargs)
plugin.MustParse() plugin.MustParse()
//skip if no commands are specified var commands []*exec.Cmd
if len(vargs.Commands) == 0 { remote := vargs.Remote
return if remote.Backend != "" {
commands = append(commands, remoteConfigCommand(remote))
}
commands = append(commands, planCommand(vargs.Vars))
if vargs.DryRun {
commands = append(commands, applyCommand())
} }
for _, c := range vargs.Commands { for _, c := range commands {
cmd := command(c) c.Env = os.Environ()
cmd.Env = os.Environ() c.Dir = workspace.Path
cmd.Dir = workspace.Path c.Stdout = os.Stdout
cmd.Stdout = os.Stdout c.Stderr = os.Stderr
cmd.Stderr = os.Stderr trace(c)
trace(cmd)
err := cmd.Run() err := c.Run()
if err != nil { if err != nil {
os.Exit(1) os.Exit(1)
} }
@ -43,9 +54,41 @@ func main() {
} }
func command(cmd string) *exec.Cmd { func remoteConfigCommand(config remote) *exec.Cmd {
args := strings.Split(cmd, " ") args := []string{
return exec.Command(args[0], args[1:]...) "remote",
"config",
fmt.Sprintf("-backend=%s", config.Backend),
}
for k, v := range config.Config {
args = append(args, fmt.Sprintf("-backend-config=\"%s=%s\"", k, v))
}
return exec.Command(
"terraform",
args...,
)
}
func planCommand(variables map[string]string) *exec.Cmd {
args := []string{
"plan",
"-out=plan.tfout",
}
for k, v := range variables {
args = append(args, fmt.Sprintf("-var \"%s=%s\"", k, v))
}
return exec.Command(
"terraform",
args...,
)
}
func applyCommand() *exec.Cmd {
return exec.Command(
"terraform",
"apply",
"plan.tfout",
)
} }
func trace(cmd *exec.Cmd) { func trace(cmd *exec.Cmd) {