Skip to content

Commit

Permalink
Merge branch 'main' into fix/cloudpossegh-1019
Browse files Browse the repository at this point in the history
  • Loading branch information
osterman authored Feb 5, 2025
2 parents 04be802 + 6272c53 commit 01a0e24
Show file tree
Hide file tree
Showing 9 changed files with 134 additions and 116 deletions.
10 changes: 10 additions & 0 deletions cmd/cmd_utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -605,3 +605,13 @@ func getConfigAndStacksInfo(commandName string, cmd *cobra.Command, args []strin
}
return info
}

// Contains checks if a slice of strings contains an exact match for the target string.
func Contains(slice []string, target string) bool {
for _, item := range slice {
if item == target {
return true
}
}
return false
}
48 changes: 20 additions & 28 deletions cmd/terraform.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,13 @@ package cmd
import (
"fmt"

l "github.com/charmbracelet/log"
"github.com/spf13/cobra"

e "github.com/cloudposse/atmos/internal/exec"
cfg "github.com/cloudposse/atmos/pkg/config"
h "github.com/cloudposse/atmos/pkg/hooks"
u "github.com/cloudposse/atmos/pkg/utils"

l "github.com/charmbracelet/log"
)

// terraformCmd represents the base command for all terraform sub-commands
Expand All @@ -22,26 +21,12 @@ var terraformCmd = &cobra.Command{
FParseErrWhitelist: struct{ UnknownFlags bool }{UnknownFlags: true},
}

// Contains checks if a slice of strings contains an exact match for the target string.
func Contains(slice []string, target string) bool {
for _, item := range slice {
if item == target {
return true
}
}
return false
}

func terraformRun(cmd *cobra.Command, actualCmd *cobra.Command, args []string) {
info := getConfigAndStacksInfo("terraform", cmd, args)
if info.NeedHelp {
actualCmd.Usage()
return
}
err := e.ExecuteTerraform(info)
if err != nil {
u.LogErrorAndExit(err)
}
func init() {
// https://github.com/spf13/cobra/issues/739
terraformCmd.DisableFlagParsing = true
terraformCmd.PersistentFlags().StringP("stack", "s", "", "atmos terraform <terraform_command> <component> -s <stack>")
attachTerraformCommands(terraformCmd)
RootCmd.AddCommand(terraformCmd)
}

func runHooks(event h.HookEvent, cmd *cobra.Command, args []string) error {
Expand All @@ -66,10 +51,17 @@ func runHooks(event h.HookEvent, cmd *cobra.Command, args []string) error {
return nil
}

func init() {
// https://github.com/spf13/cobra/issues/739
terraformCmd.DisableFlagParsing = true
terraformCmd.PersistentFlags().StringP("stack", "s", "", "atmos terraform <terraform_command> <component> -s <stack>")
attachTerraformCommands(terraformCmd)
RootCmd.AddCommand(terraformCmd)
func terraformRun(cmd *cobra.Command, actualCmd *cobra.Command, args []string) {
info := getConfigAndStacksInfo("terraform", cmd, args)
if info.NeedHelp {
err := actualCmd.Usage()
if err != nil {
u.LogErrorAndExit(err)
}
return
}
err := e.ExecuteTerraform(info)
if err != nil {
u.LogErrorAndExit(err)
}
}
3 changes: 2 additions & 1 deletion cmd/terraform_commands.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,9 @@ package cmd
import (
"os"

h "github.com/cloudposse/atmos/pkg/hooks"
"github.com/spf13/cobra"

h "github.com/cloudposse/atmos/pkg/hooks"
)

// getTerraformCommands returns an array of statically defined Terraform commands with flags
Expand Down
2 changes: 1 addition & 1 deletion examples/quick-start-advanced/Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ ARG GEODESIC_OS=debian
# https://atmos.tools/
# https://github.com/cloudposse/atmos
# https://github.com/cloudposse/atmos/releases
ARG ATMOS_VERSION=1.158.0
ARG ATMOS_VERSION=1.160.0

# Terraform: https://github.com/hashicorp/terraform/releases
ARG TF_VERSION=1.5.7
Expand Down
18 changes: 9 additions & 9 deletions go.mod

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

34 changes: 18 additions & 16 deletions go.sum

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

64 changes: 4 additions & 60 deletions internal/exec/terraform.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,62 +32,6 @@ func ExecuteTerraformCmd(cmd *cobra.Command, args []string, additionalArgsAndFla
return ExecuteTerraform(info)
}

func shouldProcessStacks(info *schema.ConfigAndStacksInfo) (bool, bool) {
shouldProcessStacks := true
shouldCheckStack := true

if info.SubCommand == "clean" {
if info.ComponentFromArg == "" {
shouldProcessStacks = false
}
shouldCheckStack = info.Stack != ""

}

return shouldProcessStacks, shouldCheckStack
}

func generateBackendConfig(atmosConfig *schema.AtmosConfiguration, info *schema.ConfigAndStacksInfo, workingDir string) error {
// Auto-generate backend file
if atmosConfig.Components.Terraform.AutoGenerateBackendFile {
backendFileName := filepath.Join(workingDir, "backend.tf.json")

u.LogDebug("\nWriting the backend config to file:")
u.LogDebug(backendFileName)

if !info.DryRun {
componentBackendConfig, err := generateComponentBackendConfig(info.ComponentBackendType, info.ComponentBackendSection, info.TerraformWorkspace)
if err != nil {
return err
}

err = u.WriteToFileAsJSON(backendFileName, componentBackendConfig, 0o644)
if err != nil {
return err
}
}
}

return nil
}

func generateProviderOverrides(atmosConfig *schema.AtmosConfiguration, info *schema.ConfigAndStacksInfo, workingDir string) error {
// Generate `providers_override.tf.json` file if the `providers` section is configured
if len(info.ComponentProvidersSection) > 0 {
providerOverrideFileName := filepath.Join(workingDir, "providers_override.tf.json")

u.LogDebug("\nWriting the provider overrides to file:")
u.LogDebug(providerOverrideFileName)

if !info.DryRun {
providerOverrides := generateComponentProviderOverrides(info.ComponentProvidersSection)
err := u.WriteToFileAsJSON(providerOverrideFileName, providerOverrides, 0o644)
return err
}
}
return nil
}

// ExecuteTerraform executes terraform commands
func ExecuteTerraform(info schema.ConfigAndStacksInfo) error {
atmosConfig, err := cfg.InitCliConfig(info, true)
Expand All @@ -109,18 +53,18 @@ func ExecuteTerraform(info schema.ConfigAndStacksInfo) error {
info.RedirectStdErr)
}

// Skip stack processing when cleaning with --everything or --force flags to allow cleaning without requiring stack
// configuration
// Skip stack processing when cleaning with --force flag to allow cleaning without requiring stack configuration
shouldProcessStacks, shouldCheckStack := shouldProcessStacks(&info)

if shouldProcessStacks {
info, err = ProcessStacks(atmosConfig, info, shouldCheckStack, true, true, nil)
processTemplatesAndYamlFunctions := needProcessTemplatesAndYamlFunctions(info.SubCommand)
info, err = ProcessStacks(atmosConfig, info, shouldCheckStack, processTemplatesAndYamlFunctions, processTemplatesAndYamlFunctions, nil)
if err != nil {
return err
}

if len(info.Stack) < 1 && shouldCheckStack {
return errors.New("stack must be specified when not using --everything or --force flags")
return errors.New("stack must be specified for the terraform command")
}
}

Expand Down
Loading

0 comments on commit 01a0e24

Please sign in to comment.