Skip to content

Commit

Permalink
bt: add dry-run
Browse files Browse the repository at this point in the history
  • Loading branch information
DavidGamba committed Feb 21, 2024
1 parent 66e9d81 commit dc09537
Show file tree
Hide file tree
Showing 8 changed files with 41 additions and 9 deletions.
8 changes: 7 additions & 1 deletion bt/terraform/apply.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,11 +15,13 @@ import (

func applyCMD(ctx context.Context, parent *getoptions.GetOpt) *getoptions.GetOpt {
opt := parent.NewCommand("apply", "")
opt.Bool("dry-run", false)
opt.SetCommandFn(applyRun)
return opt
}

func applyRun(ctx context.Context, opt *getoptions.GetOpt, args []string) error {
dryRun := opt.Value("dry-run").(bool)
ws := opt.Value("ws").(string)
profile := opt.Value("profile").(string)

Expand Down Expand Up @@ -67,7 +69,7 @@ func applyRun(ctx context.Context, opt *getoptions.GetOpt, args []string) error
cmd = append(cmd, args...)
dataDir := fmt.Sprintf("TF_DATA_DIR=%s", getDataDir(cfg.Config.DefaultTerraformProfile, cfg.Profile(profile)))
Logger.Printf("export %s\n", dataDir)
ri := run.CMDCtx(ctx, cmd...).Stdin().Log().Env(dataDir).Dir(dir)
ri := run.CMDCtx(ctx, cmd...).Stdin().Log().Env(dataDir).Dir(dir).DryRun(dryRun)
if ws != "" {
wsEnv := fmt.Sprintf("TF_WORKSPACE=%s", ws)
Logger.Printf("export %s\n", wsEnv)
Expand All @@ -79,6 +81,10 @@ func applyRun(ctx context.Context, opt *getoptions.GetOpt, args []string) error
return fmt.Errorf("failed to run: %w", err)
}

if dryRun {
return nil
}

fh, err := os.Create(applyFile)
if err != nil {
return fmt.Errorf("failed to create file: %w", err)
Expand Down
1 change: 1 addition & 0 deletions bt/terraform/build.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ func buildCMD(ctx context.Context, parent *getoptions.GetOpt) *getoptions.GetOpt
opt := parent.NewCommand("build", "Wraps init, plan and apply into a single operation with a cache")
opt.SetCommandFn(buildRun)
opt.StringSlice("var-file", 1, 1)
opt.Bool("dry-run", false)
opt.Bool("destroy", false)
opt.Bool("detailed-exitcode", false)
opt.Bool("ignore-cache", false, opt.Description("Ignore the cache and re-run the plan"), opt.Alias("ic"))
Expand Down
13 changes: 10 additions & 3 deletions bt/terraform/checks.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ import (

func checksCMD(ctx context.Context, parent *getoptions.GetOpt) *getoptions.GetOpt {
opt := parent.NewCommand("checks", "Run pre-apply checks against the latest plan")
opt.Bool("dry-run", false)
opt.StringSlice("var-file", 1, 1)
opt.Bool("no-checks", false, opt.Description("Do not run pre-apply checks"), opt.Alias("nc"))
opt.Bool("ignore-cache", false, opt.Description("ignore the cache and re-run the checks"), opt.Alias("ic"))
Expand All @@ -24,6 +25,7 @@ func checksCMD(ctx context.Context, parent *getoptions.GetOpt) *getoptions.GetOp
}

func checksRun(ctx context.Context, opt *getoptions.GetOpt, args []string) error {
dryRun := opt.Value("dry-run").(bool)
profile := opt.Value("profile").(string)
varFiles := opt.Value("var-file").([]string)
ws := opt.Value("ws").(string)
Expand All @@ -35,14 +37,15 @@ func checksRun(ctx context.Context, opt *getoptions.GetOpt, args []string) error
}

cfg := config.ConfigFromContext(ctx)
dir := DirFromContext(ctx)
LogConfig(cfg, profile)

ws, err := updateWSIfSelected(cfg.Config.DefaultTerraformProfile, cfg.Profile(profile), ws)
if err != nil {
return err
}

cwd, err := os.Getwd()
cwd, err := filepath.Abs(dir)
if err != nil {
return fmt.Errorf("failed to get current dir: %w", err)
}
Expand Down Expand Up @@ -114,7 +117,7 @@ func checksRun(ctx context.Context, opt *getoptions.GetOpt, args []string) error
cmd := []string{cfg.TFProfile[cfg.Profile(profile)].BinaryName, "show", "-json", planFile}
dataDir := fmt.Sprintf("TF_DATA_DIR=%s", getDataDir(cfg.Config.DefaultTerraformProfile, cfg.Profile(profile)))
Logger.Printf("export %s\n", dataDir)
ri := run.CMDCtx(ctx, cmd...).Stdin().Log().Env(dataDir)
ri := run.CMDCtx(ctx, cmd...).Stdin().Log().Env(dataDir).Dir(dir).DryRun(dryRun)
out, err := ri.STDOutOutput()
if err != nil {
return fmt.Errorf("failed to get plan json output: %w", err)
Expand All @@ -132,13 +135,17 @@ func checksRun(ctx context.Context, opt *getoptions.GetOpt, args []string) error
if err != nil {
return fmt.Errorf("failed to expand: %w", err)
}
ri := run.CMDCtx(ctx, exp...).Stdin().Log().Env(dataDir)
ri := run.CMDCtx(ctx, exp...).Stdin().Log().Env(dataDir).Dir(dir).DryRun(dryRun)
err = ri.Run()
if err != nil {
return fmt.Errorf("failed to run: %w", err)
}
}

if dryRun {
return nil
}

fh, err := os.Create(checkFile)
if err != nil {
return fmt.Errorf("failed to create file: %w", err)
Expand Down
9 changes: 8 additions & 1 deletion bt/terraform/init.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,11 +16,13 @@ import (

func initCMD(ctx context.Context, parent *getoptions.GetOpt) *getoptions.GetOpt {
opt := parent.NewCommand("init", "")
opt.Bool("dry-run", false)
opt.SetCommandFn(initRun)
return opt
}

func initRun(ctx context.Context, opt *getoptions.GetOpt, args []string) error {
dryRun := opt.Value("dry-run").(bool)
profile := opt.Value("profile").(string)

cfg := config.ConfigFromContext(ctx)
Expand All @@ -47,10 +49,15 @@ func initRun(ctx context.Context, opt *getoptions.GetOpt, args []string) error {
cmd = append(cmd, args...)
dataDir := fmt.Sprintf("TF_DATA_DIR=%s", getDataDir(cfg.Config.DefaultTerraformProfile, cfg.Profile(profile)))
Logger.Printf("export %s\n", dataDir)
err := run.CMDCtx(ctx, cmd...).Stdin().Log().Env(dataDir).Dir(dir).Run()
err := run.CMDCtx(ctx, cmd...).Stdin().Log().Env(dataDir).Dir(dir).DryRun(dryRun).Run()
if err != nil {
return fmt.Errorf("failed to run: %w", err)
}

if dryRun {
return nil
}

initFile := filepath.Join(dir, ".tf.init")
fh, err := os.Create(initFile)
if err != nil {
Expand Down
6 changes: 4 additions & 2 deletions bt/terraform/plan.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,10 +19,11 @@ import (

func planCMD(ctx context.Context, parent *getoptions.GetOpt) *getoptions.GetOpt {
opt := parent.NewCommand("plan", "")
opt.StringSlice("var-file", 1, 1)
opt.Bool("dry-run", false)
opt.Bool("destroy", false)
opt.Bool("detailed-exitcode", false)
opt.Bool("ignore-cache", false, opt.Description("ignore the cache and re-run the plan"), opt.Alias("ic"))
opt.StringSlice("var-file", 1, 1)
opt.StringSlice("target", 1, 99)
opt.StringSlice("replace", 1, 99)
opt.SetCommandFn(planRun)
Expand All @@ -31,6 +32,7 @@ func planCMD(ctx context.Context, parent *getoptions.GetOpt) *getoptions.GetOpt
}

func planRun(ctx context.Context, opt *getoptions.GetOpt, args []string) error {
dryRun := opt.Value("dry-run").(bool)
profile := opt.Value("profile").(string)
destroy := opt.Value("destroy").(bool)
detailedExitcode := opt.Value("detailed-exitcode").(bool)
Expand Down Expand Up @@ -176,7 +178,7 @@ func planRun(ctx context.Context, opt *getoptions.GetOpt, args []string) error {

dataDir := fmt.Sprintf("TF_DATA_DIR=%s", getDataDir(cfg.Config.DefaultTerraformProfile, cfg.Profile(profile)))
Logger.Printf("export %s\n", dataDir)
ri := run.CMDCtx(ctx, cmd...).Stdin().Log().Env(dataDir).Dir(dir)
ri := run.CMDCtx(ctx, cmd...).Stdin().Log().Env(dataDir).Dir(dir).DryRun(dryRun)
if ws != "" {
wsEnv := fmt.Sprintf("TF_WORKSPACE=%s", ws)
Logger.Printf("export %s\n", wsEnv)
Expand Down
1 change: 1 addition & 0 deletions bt/terraform/show.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import (

func showCMD(ctx context.Context, parent *getoptions.GetOpt) *getoptions.GetOpt {
opt := parent.NewCommand("show", "")
opt.Bool("dry-run", false)
opt.SetCommandFn(showRun)
return opt
}
Expand Down
5 changes: 4 additions & 1 deletion bt/terraform/show_plan.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,15 +13,18 @@ import (

func showPlanCMD(ctx context.Context, parent *getoptions.GetOpt) *getoptions.GetOpt {
opt := parent.NewCommand("show-plan", "Show the cached terraform plan")
opt.Bool("dry-run", false)
opt.SetCommandFn(showPlanRun)
return opt
}

func showPlanRun(ctx context.Context, opt *getoptions.GetOpt, args []string) error {
dryRun := opt.Value("dry-run").(bool)
profile := opt.Value("profile").(string)
ws := opt.Value("ws").(string)

cfg := config.ConfigFromContext(ctx)
dir := DirFromContext(ctx)
LogConfig(cfg, profile)

ws, err := updateWSIfSelected(cfg.Config.DefaultTerraformProfile, cfg.Profile(profile), ws)
Expand Down Expand Up @@ -50,7 +53,7 @@ func showPlanRun(ctx context.Context, opt *getoptions.GetOpt, args []string) err
}
dataDir := fmt.Sprintf("TF_DATA_DIR=%s", getDataDir(cfg.Config.DefaultTerraformProfile, cfg.Profile(profile)))
Logger.Printf("export %s\n", dataDir)
ri := run.CMDCtx(ctx, cmd...).Stdin().Log().Env(dataDir)
ri := run.CMDCtx(ctx, cmd...).Stdin().Log().Env(dataDir).Dir(dir).DryRun(dryRun)
if ws != "" {
wsEnv := fmt.Sprintf("TF_WORKSPACE=%s", ws)
Logger.Printf("export %s\n", wsEnv)
Expand Down
7 changes: 6 additions & 1 deletion bt/terraform/ws_cmd.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,13 @@ func wsCMDRun(cmd ...string) getoptions.CommandFn {
return func(ctx context.Context, opt *getoptions.GetOpt, args []string) error {
profile := opt.Value("profile").(string)
ws := opt.Value("ws").(string)
dryRun := false
if opt.Value("dry-run") != nil {
dryRun = opt.Value("dry-run").(bool)
}

cfg := config.ConfigFromContext(ctx)
dir := DirFromContext(ctx)
LogConfig(cfg, profile)

ws, err := updateWSIfSelected(cfg.Config.DefaultTerraformProfile, cfg.Profile(profile), ws)
Expand Down Expand Up @@ -45,7 +50,7 @@ func wsCMDRun(cmd ...string) getoptions.CommandFn {
cmd = append(cmd, args...)
dataDir := fmt.Sprintf("TF_DATA_DIR=%s", getDataDir(cfg.Config.DefaultTerraformProfile, cfg.Profile(profile)))
Logger.Printf("export %s\n", dataDir)
ri := run.CMDCtx(ctx, cmd...).Stdin().Log().Env(dataDir)
ri := run.CMDCtx(ctx, cmd...).Stdin().Log().Env(dataDir).Dir(dir).DryRun(dryRun)
if ws != "" {
wsEnv := fmt.Sprintf("TF_WORKSPACE=%s", ws)
Logger.Printf("export %s\n", wsEnv)
Expand Down

0 comments on commit dc09537

Please sign in to comment.