From c2f4781dc0c72c57102c753e09cb7232e324f5d0 Mon Sep 17 00:00:00 2001 From: Matt Gowie Date: Wed, 7 Sep 2022 17:38:15 -0600 Subject: [PATCH] feat: adds `--skip-init` flag fix: adds SkipInitFlag to commonFlags so as to not pass down to command docs: adds --skip-init to `atmos terraform --help` docs --- internal/exec/help.go | 2 ++ internal/exec/terraform.go | 7 +++++++ internal/exec/utils.go | 8 ++++++++ pkg/config/schema.go | 2 ++ pkg/globals/globals.go | 1 + 5 files changed, 20 insertions(+) diff --git a/internal/exec/help.go b/internal/exec/help.go index 0c8d218e6..c2a902c38 100644 --- a/internal/exec/help.go +++ b/internal/exec/help.go @@ -18,6 +18,8 @@ func processHelp(componentType string, command string) error { fmt.Println() u.PrintInfo("Additions and differences from native terraform:") fmt.Println(" - before executing other 'terraform' commands, 'atmos' calls 'terraform init'") + fmt.Println(" - you can skip over atmos calling 'terraform init' if you know your project is already in a good working state by using " + + "the '--skip-init' flag like so 'atmos terraform -s --skip-init") fmt.Println(" - 'atmos terraform deploy' command executes 'terraform plan' and then 'terraform apply'") fmt.Println(" - 'atmos terraform deploy' command supports '--deploy-run-init=true/false' flag to enable/disable running 'terraform init' " + "before executing the command") diff --git a/internal/exec/terraform.go b/internal/exec/terraform.go index 43eeb0a26..ef0175146 100644 --- a/internal/exec/terraform.go +++ b/internal/exec/terraform.go @@ -173,6 +173,13 @@ func ExecuteTerraform(cmd *cobra.Command, args []string) error { (info.SubCommand == "deploy" && !c.Config.Components.Terraform.DeployRunInit) { runTerraformInit = false } + + if info.SkipInit { + fmt.Println() + u.PrintInfo("Skipping over `terraform init` due to `--skip-init` being passed.") + runTerraformInit = false + } + if runTerraformInit { initCommandWithArguments := []string{"init"} if info.SubCommand == "workspace" || c.Config.Components.Terraform.InitRunReconfigure { diff --git a/internal/exec/utils.go b/internal/exec/utils.go index f268666e3..9f4d35f68 100644 --- a/internal/exec/utils.go +++ b/internal/exec/utils.go @@ -13,10 +13,13 @@ import ( ) var ( + // `commonFlags` are a list of flags that atmos understands but the underlying tools do not (e.g. terraform, helmfile, etc.) + // These flags get removed from the arg list after atmos uses them so the underlying tool does not get passed a flag it doesn't accept. commonFlags = []string{ "--stack", "-s", g.DryRunFlag, + g.SkipInitFlag, g.KubeConfigConfigFlag, g.TerraformDirFlag, g.HelmfileDirFlag, @@ -175,6 +178,7 @@ func processArgsConfigAndStacks(componentType string, cmd *cobra.Command, args [ configAndStacksInfo.AutoGenerateBackendFile = argsAndFlagsInfo.AutoGenerateBackendFile configAndStacksInfo.UseTerraformPlan = argsAndFlagsInfo.UseTerraformPlan configAndStacksInfo.DryRun = argsAndFlagsInfo.DryRun + configAndStacksInfo.SkipInit = argsAndFlagsInfo.SkipInit configAndStacksInfo.NeedHelp = argsAndFlagsInfo.NeedHelp // Check if `-h` or `--help` flags are specified @@ -566,6 +570,10 @@ func processArgsAndFlags(componentType string, inputArgsAndFlags []string) (c.Ar info.DryRun = true } + if arg == g.SkipInitFlag { + info.SkipInit = true + } + if arg == g.HelpFlag1 || arg == g.HelpFlag2 { info.NeedHelp = true } diff --git a/pkg/config/schema.go b/pkg/config/schema.go index 705964e53..07b448341 100644 --- a/pkg/config/schema.go +++ b/pkg/config/schema.go @@ -88,6 +88,7 @@ type ArgsAndFlagsInfo struct { AutoGenerateBackendFile string UseTerraformPlan bool DryRun bool + SkipInit bool NeedHelp bool } @@ -125,6 +126,7 @@ type ConfigAndStacksInfo struct { AutoGenerateBackendFile string UseTerraformPlan bool DryRun bool + SkipInit bool ComponentInheritanceChain []string NeedHelp bool ComponentIsAbstract bool diff --git a/pkg/globals/globals.go b/pkg/globals/globals.go index 482b0739a..c060d43b7 100644 --- a/pkg/globals/globals.go +++ b/pkg/globals/globals.go @@ -24,6 +24,7 @@ const ( FromPlanFlag = "--from-plan" DryRunFlag = "--dry-run" + SkipInitFlag = "--skip-init" HelpFlag1 = "-h" HelpFlag2 = "--help"