From 8a66d10c3c5473bf8346bd58b12e010399ce6370 Mon Sep 17 00:00:00 2001 From: Thomas De Meyer Date: Tue, 19 Mar 2024 13:28:08 +0100 Subject: [PATCH] feat: added missing auto-init logic in show-plan --- docs/src/reference/cli/mach-composer_show-plan.md | 1 + internal/cmd/show-plan.go | 3 +++ internal/runner/graph.go | 9 +++++++++ internal/runner/runner.go | 1 + 4 files changed, 14 insertions(+) diff --git a/docs/src/reference/cli/mach-composer_show-plan.md b/docs/src/reference/cli/mach-composer_show-plan.md index 4f6ff3d8..425553aa 100644 --- a/docs/src/reference/cli/mach-composer_show-plan.md +++ b/docs/src/reference/cli/mach-composer_show-plan.md @@ -10,6 +10,7 @@ mach-composer show-plan [flags] ``` -f, --file string YAML file to parse. (default "main.yml") + --force-init Force terraform initialization. By default mach-composer will reuse existing terraform resources -h, --help help for show-plan --ignore-change-detection Ignore change detection to run even if the components are considered up to date --ignore-version Skip MACH composer version check diff --git a/internal/cmd/show-plan.go b/internal/cmd/show-plan.go index 589e78ea..2bd15dc9 100644 --- a/internal/cmd/show-plan.go +++ b/internal/cmd/show-plan.go @@ -10,6 +10,7 @@ import ( ) var showPlanFlags struct { + forceInit bool noColor bool ignoreChangeDetection bool } @@ -28,6 +29,7 @@ var showPlanCmd = &cobra.Command{ func init() { registerCommonFlags(showPlanCmd) + showPlanCmd.Flags().BoolVarP(&showPlanFlags.forceInit, "force-init", "", false, "Force terraform initialization. By default mach-composer will reuse existing terraform resources") showPlanCmd.Flags().BoolVarP(&showPlanFlags.noColor, "no-color", "", false, "Disable color output") showPlanCmd.Flags().BoolVarP(&showPlanFlags.ignoreChangeDetection, "ignore-change-detection", "", false, "Ignore change detection to run even if the components are considered up to date") @@ -50,6 +52,7 @@ func showPlanFunc(cmd *cobra.Command, _ []string) error { ) return r.TerraformShow(ctx, dg, &runner.ShowPlanOptions{ + ForceInit: showPlanFlags.forceInit, NoColor: showPlanFlags.noColor, IgnoreChangeDetection: showPlanFlags.ignoreChangeDetection, }) diff --git a/internal/runner/graph.go b/internal/runner/graph.go index aaef19c3..a18e6dbc 100644 --- a/internal/runner/graph.go +++ b/internal/runner/graph.go @@ -171,6 +171,15 @@ func (gr *GraphRunner) TerraformProxy(ctx context.Context, dg *graph.Graph, opts func (gr *GraphRunner) TerraformShow(ctx context.Context, dg *graph.Graph, opts *ShowPlanOptions) error { if err := gr.run(ctx, dg, func(ctx context.Context, n graph.Node) (string, error) { + if !terraformIsInitialized(n.Path()) || opts.ForceInit { + log.Info().Msgf("Running terraform init for %s", n.Path()) + if out, err := terraform.Init(ctx, n.Path()); err != nil { + return out, err + } + } else { + log.Info().Msgf("Skipping terraform init for %s", n.Path()) + } + return terraform.Show(ctx, n.Path(), opts.NoColor) }, opts.IgnoreChangeDetection); err != nil { return err diff --git a/internal/runner/runner.go b/internal/runner/runner.go index de443de7..0ed317ce 100644 --- a/internal/runner/runner.go +++ b/internal/runner/runner.go @@ -24,6 +24,7 @@ type ProxyOptions struct { } type ShowPlanOptions struct { + ForceInit bool IgnoreChangeDetection bool NoColor bool }