Skip to content

Commit

Permalink
feat: added missing auto-init logic in show-plan
Browse files Browse the repository at this point in the history
  • Loading branch information
demeyerthom committed Mar 19, 2024
1 parent 0ed0e39 commit 8a66d10
Show file tree
Hide file tree
Showing 4 changed files with 14 additions and 0 deletions.
1 change: 1 addition & 0 deletions docs/src/reference/cli/mach-composer_show-plan.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
3 changes: 3 additions & 0 deletions internal/cmd/show-plan.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import (
)

var showPlanFlags struct {
forceInit bool
noColor bool
ignoreChangeDetection bool
}
Expand All @@ -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")
Expand All @@ -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,
})
Expand Down
9 changes: 9 additions & 0 deletions internal/runner/graph.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
1 change: 1 addition & 0 deletions internal/runner/runner.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ type ProxyOptions struct {
}

type ShowPlanOptions struct {
ForceInit bool
IgnoreChangeDetection bool
NoColor bool
}
Expand Down

0 comments on commit 8a66d10

Please sign in to comment.