func initialize(c *cli.Context, terraformCommand string) TerraformOperation { if len(c.Args()) < 1 { fmt.Printf("Incorrect usage\n") fmt.Printf("%s <environment>\n", terraformCommand) os.Exit(1) } fmt.Println(c.Args()) environment := c.Args()[0] security.Apply(c.String("security"), c) fmt.Println() fmt.Println("Execute Terraform command") fmt.Println("Command: ", command.Bold(terraformCommand)) fmt.Println("Environment:", command.Bold(environment)) fmt.Println() configLocation := c.String("config-location") config := terraform_config.LoadConfig(configLocation, environment) getState(c.Bool("no-sync"), config, environment) return TerraformOperation{ command: terraformCommand, environment: environment, config: config, args: os.Args[2:], } }
func CmdRun(c *cli.Context) { if len(c.Args()) != 2 { fmt.Printf("Incorrect usage\n") fmt.Printf("run <terraform command> <environment>\n") os.Exit(1) } IsTerraformProject() terraformCommand := c.Args()[0] environment := c.Args()[1] security.Apply(c.String("security"), c) IsSupportedTerraformCommand(terraformCommand) fmt.Println() fmt.Println("Run Terraform command") fmt.Println("Command: ", bold(terraformCommand)) fmt.Println("Environment:", bold(environment)) fmt.Println() configLocation := c.String("config-location") config := terraform_config.LoadConfig(c.String("config-location"), environment) if c.Bool("no-sync") { red("SKIPPING S3 download of current state") if command.InputAffirmative() { red("Skipped syncing with S3") } else { DownloadState(config, environment) } } else { DownloadState(config, environment) } tfVars := terraform_config.TerraformVars(configLocation, environment) tfState := terraform_config.TerraformState(environment) terraformActions := terraformCommands[terraformCommand] // It would be great to use golang terraform so we don't have to install it separately // I think we would need to use "github.com/mitchellh/cli" instead of current cli cmdName := "terraform" cmdArgs := []string{terraformCommand, "-var-file", tfVars, fmt.Sprintf("-state=%s", tfState), "-var", fmt.Sprintf("environment=%s", environment)} if terraformActions.extraArgs != "" { cmdArgs = append(cmdArgs, terraformActions.extraArgs) } fmt.Println("---------------------------------------------") Bold.Println(cmdName, strings.Join(cmdArgs, " ")) fmt.Println("---------------------------------------------") fmt.Println() command.Default().Execute(cmdName, cmdArgs) fmt.Println() fmt.Println("---------------------------------------------") if terraformActions.sync { fmt.Printf("S3 SYNC new changes\n") UploadState(config, environment) } }