0
0
mirror of https://github.com/thegeeklab/wp-opentofu.git synced 2024-11-10 04:10:41 +00:00
wp-opentofu/main.go

116 lines
2.9 KiB
Go
Raw Normal View History

2015-11-09 19:23:42 +00:00
package main
import (
2016-10-08 15:34:43 +00:00
"encoding/json"
2015-11-09 19:23:42 +00:00
"os"
2016-10-08 15:34:43 +00:00
"github.com/joho/godotenv"
"github.com/Sirupsen/logrus"
"github.com/urfave/cli"
)
2016-10-08 15:34:43 +00:00
var version string // build number set at compile-time
2015-11-09 19:23:42 +00:00
func main() {
2016-10-08 15:34:43 +00:00
app := cli.NewApp()
app.Name = "terraform plugin"
app.Usage = "terraform plugin"
app.Action = run
app.Version = version
app.Flags = []cli.Flag{
//
// plugin args
//
cli.BoolFlag{
Name: "terraform.plan",
Usage: "calculates a plan but does NOT apply it",
EnvVar: "PLUGIN_PLAN",
},
cli.StringFlag{
Name: "terraform.remote",
Usage: "contains the configuration for the Terraform remote state tracking",
EnvVar: "PLUGIN_REMOTE",
},
cli.StringFlag{
Name: "terraform.vars",
Usage: "a map of variables to pass to the Terraform `plan` and `apply` commands. Each value is passed as a `<key>=<value>` option",
EnvVar: "PLUGIN_VARS",
},
cli.StringFlag{
Name: "terraform.secrets",
Usage: "a map of secrets to pass to the Terraform `plan` and `apply` commands. Each value is passed as a `<key>=<ENV>` option",
EnvVar: "PLUGIN_SECRETS",
},
cli.StringFlag{
Name: "terraform.ca_cert",
Usage: "ca cert to add to your environment to allow terraform to use internal/private resources",
EnvVar: "PLUGIN_CA_CERT",
},
cli.BoolFlag{
Name: "terraform.sensitive",
Usage: "whether or not to suppress terraform commands to stdout",
EnvVar: "PLUGIN_SENSITIVE",
},
cli.StringFlag{
Name: "terraform.role_arn_to_assume",
Usage: "A role to assume before running the terraform commands",
EnvVar: "PLUGIN_ROLE_ARN_TO_ASSUME",
},
cli.StringFlag{
Name: "terraform.root_dir",
Usage: "The root directory where the terraform files live. When unset, the top level directory will be assumed",
EnvVar: "PLUGIN_ROOT_DIR",
},
cli.IntFlag{
Name: "terraform.parallelism",
Usage: "The number of concurrent operations as Terraform walks its graph",
EnvVar: "PLUGIN_PARALLELISM",
},
cli.StringFlag{
Name: "env-file",
Usage: "source env file",
},
}
2016-10-08 15:34:43 +00:00
if err := app.Run(os.Args); err != nil {
logrus.Fatal(err)
}
2016-02-09 19:27:12 +00:00
}
2016-10-08 15:34:43 +00:00
func run(c *cli.Context) error {
if c.String("env-file") != "" {
_ = godotenv.Load(c.String("env-file"))
}
2016-10-08 15:34:43 +00:00
remote := Remote{}
json.Unmarshal([]byte(c.String("terraform.remote")), &remote)
2016-10-08 15:34:43 +00:00
var vars map[string]string
if err := json.Unmarshal([]byte(c.String("terraform.vars")), &vars); err != nil {
panic(err)
}
2016-10-08 15:34:43 +00:00
var secrets map[string]string
if err := json.Unmarshal([]byte(c.String("terraform.secrets")), &secrets); err != nil {
panic(err)
}
2015-11-09 19:23:42 +00:00
2016-10-08 15:34:43 +00:00
plugin := Plugin{
Config: Config{
Remote: remote,
Plan: c.Bool("terraform.plan"),
Vars: vars,
Secrets: secrets,
Cacert: c.String("terraform.ca_cert"),
Sensitive: c.Bool("terraform.sensitive"),
RoleARN: c.String("terraform.role_arn_to_assume"),
RootDir: c.String("terraform.root_dir"),
Parallelism: c.Int("terraform.parallelism"),
},
}
2016-10-08 15:34:43 +00:00
return plugin.Exec()
2015-11-09 19:23:42 +00:00
}