From 699ebdcfc79cb17dfaae747bf5c09bd7e03c04c4 Mon Sep 17 00:00:00 2001 From: David Schmitt Date: Wed, 28 Feb 2024 18:13:24 +0100 Subject: [PATCH] Actually execute terraform plan --- cmd/terraform_plan.go | 36 +++++++++++++++++++++++++++++------- 1 file changed, 29 insertions(+), 7 deletions(-) diff --git a/cmd/terraform_plan.go b/cmd/terraform_plan.go index d8e8f9df..900ad84e 100644 --- a/cmd/terraform_plan.go +++ b/cmd/terraform_plan.go @@ -4,7 +4,9 @@ import ( "context" "fmt" "os" + "os/exec" "os/signal" + "strings" "syscall" "time" @@ -55,7 +57,7 @@ var terraformPlanCmd = &cobra.Command{ }, } -func TerraformPlan(ctx context.Context, files []string, ready chan bool) int { +func TerraformPlan(ctx context.Context, args []string, ready chan bool) int { timeout, err := time.ParseDuration(viper.GetString("timeout")) if err != nil { log.Errorf("invalid --timeout value '%v', error: %v", viper.GetString("timeout"), err) @@ -151,7 +153,6 @@ func TerraformPlan(ctx context.Context, files []string, ready chan bool) int { return 1 } // reset the environment to the requested value - os.Setenv("AWS_PROFILE", aws_profile) awsAuthConfig.Strategy = "sso-profile" awsAuthConfig.Profile = aws_profile case "aws_profile": @@ -220,21 +221,42 @@ func TerraformPlan(ctx context.Context, files []string, ready chan bool) int { _ = stdlibEngine.Stop() }() - prompt := `# Doing something - -NATS connection: %v + args = append([]string{"plan"}, args...) + // -out needs to go last to override whatever the user specified on the command line + args = append(args, "-out", "overmind.plan") + prompt := ` * AWS Source: running * stdlib Source: running -This will be doing something: %vAWS_PROFILE=%v terraform plan -out overmind_plan.out%v +# Planning Changes + +Running ` + "`" + `terraform %v` + "`" + ` ` - out, err := r.Render(fmt.Sprintf(prompt, awsEngine.IsNATSConnected(), "`", aws_profile, "`")) + + out, err := r.Render(fmt.Sprintf(prompt, strings.Join(args, " "))) if err != nil { panic(err) } fmt.Print(out) + // TODO: remove this debugging aid + err = os.Chdir("/workspace/deploy") + if err != nil { + panic(err) + } + + tfPlanCmd := exec.CommandContext(ctx, "terraform", args...) + tfPlanCmd.Stderr = os.Stderr + tfPlanCmd.Stdout = os.Stdout + tfPlanCmd.Stdin = os.Stdin + + err = tfPlanCmd.Run() + if err != nil { + log.WithError(err).Error("failed to run terraform plan") + return 1 + } + return 0 }