Skip to content

Commit

Permalink
Merge branch 'feature/dev-2821-atmos-terraform-help-should-also-show-…
Browse files Browse the repository at this point in the history
…terraform-help' of https://github.com/cloudposse/atmos into feature/dev-2821-atmos-terraform-help-should-also-show-terraform-help
  • Loading branch information
samtholiya committed Dec 26, 2024
2 parents 51bbb79 + 636fb16 commit 7765e3f
Show file tree
Hide file tree
Showing 33 changed files with 175 additions and 130 deletions.
3 changes: 1 addition & 2 deletions .github/workflows/test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -142,8 +142,7 @@ jobs:
- name: Acceptance tests
timeout-minutes: 10
run: |
make testacc
run: make testacc

docker:
name: "Docker Lint"
Expand Down
3 changes: 2 additions & 1 deletion cmd/cmd_utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (
"errors"
"fmt"
"os"
"path"
"path/filepath"
"strings"
"time"
Expand Down Expand Up @@ -404,7 +405,7 @@ func printMessageForMissingAtmosConfig(atmosConfig schema.AtmosConfiguration) {
u.PrintMessageInColor("atmos.yaml", c1)
fmt.Println(" CLI config file was not found.")
fmt.Print("\nThe default Atmos stacks directory is set to ")
u.PrintMessageInColor(filepath.Join(atmosConfig.BasePath, atmosConfig.Stacks.BasePath), c1)
u.PrintMessageInColor(path.Join(atmosConfig.BasePath, atmosConfig.Stacks.BasePath), c1)
fmt.Println(",\nbut the directory does not exist in the current path.")
} else {
// If Atmos found an `atmos.yaml` config file, but it defines invalid paths to Atmos stacks and components
Expand Down
6 changes: 3 additions & 3 deletions cmd/docs.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import (
"fmt"
"os"
"os/exec"
"path/filepath"
"path"
"runtime"

"github.com/charmbracelet/glamour"
Expand Down Expand Up @@ -59,7 +59,7 @@ var docsCmd = &cobra.Command{
}

// Construct the full path to the Terraform component by combining the Atmos base path, Terraform base path, and component name
componentPath := filepath.Join(atmosConfig.BasePath, atmosConfig.Components.Terraform.BasePath, info.Component)
componentPath := path.Join(atmosConfig.BasePath, atmosConfig.Components.Terraform.BasePath, info.Component)
componentPathExists, err := u.IsDirectory(componentPath)
if err != nil {
u.LogErrorAndExit(schema.AtmosConfiguration{}, err)
Expand All @@ -68,7 +68,7 @@ var docsCmd = &cobra.Command{
u.LogErrorAndExit(schema.AtmosConfiguration{}, fmt.Errorf("Component '%s' not found in path: '%s'", info.Component, componentPath))
}

readmePath := filepath.Join(componentPath, "README.md")
readmePath := path.Join(componentPath, "README.md")
if _, err := os.Stat(readmePath); err != nil {
if os.IsNotExist(err) {
u.LogErrorAndExit(schema.AtmosConfiguration{}, fmt.Errorf("No README found for component: %s", info.Component))
Expand Down
2 changes: 1 addition & 1 deletion cmd/terraform.go
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ func terraformRun(cmd *cobra.Command, args []string) error {

// Exit on help
if info.NeedHelp {
if info.SubCommand != "" {
if info.SubCommand != "" && info.SubCommand != "--help" && info.SubCommand != "help" {
suggestions := cmd.SuggestionsFor(args[0])
if len(suggestions) > 0 {
fmt.Printf("Unknown command: '%s'\n\nDid you mean this?\n", args[0])
Expand Down
9 changes: 4 additions & 5 deletions cmd/terraform_commands.go
Original file line number Diff line number Diff line change
Expand Up @@ -247,17 +247,20 @@ Arguments:
// attachTerraformCommands attaches static Terraform commands to a provided parent command
func attachTerraformCommands(parentCmd *cobra.Command) {
parentCmd.PersistentFlags().String("append-user-agent", "", "Sets the TF_APPEND_USER_AGENT environment variable to customize the User-Agent string in Terraform provider requests. Example: 'Atmos/%s (Cloud Posse; +https://atmos.tools)'. This flag works with almost all commands.")
parentCmd.PersistentFlags().Bool("skip-init", false, "Skip running 'terraform init' before executing the command")

commands := getTerraformCommands()

for _, cmd := range commands {
cmd.FParseErrWhitelist.UnknownFlags = true
if setFlags, ok := commandMaps[cmd.Use]; ok {
setFlags(cmd)
}
cmd.RunE = func(cmd_ *cobra.Command, args []string) error {
if len(os.Args) > 3 {
args = os.Args[2:]
}
if parentCmd.Run != nil {
if parentCmd.RunE != nil {
return parentCmd.RunE(parentCmd, args)
}
return nil
Expand All @@ -275,14 +278,10 @@ var commandMaps = map[string]func(cmd *cobra.Command){
"apply": func(cmd *cobra.Command) {
cmd.PersistentFlags().Bool("from-plan", false, "If set atmos will use the previously generated plan file")
cmd.PersistentFlags().String("planfile", "", "Set the plan file to use")
cmd.PersistentFlags().Bool("auto-approve", false, "Set to automatically approve and apply changes without confirmation")
},
"clean": func(cmd *cobra.Command) {
cmd.PersistentFlags().Bool("everything", false, "If set atmos will also delete the Terraform state files and directories for the component.")
cmd.PersistentFlags().Bool("force", false, "Forcefully delete Terraform state files and directories without interaction")
cmd.PersistentFlags().Bool("skip-lock-file", false, "Skip deleting the `.terraform.lock.hcl` file")
},
"destroy": func(cmd *cobra.Command) {
cmd.PersistentFlags().Bool("auto-approve", false, "Set to automatically approve and apply changes without confirmation")
},
}
12 changes: 6 additions & 6 deletions examples/demo-localstack/atmos.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -34,16 +34,16 @@ commands:
- atmos validate stacks
# Test dev stack
- atmos terraform plan demo -s dev
- atmos terraform apply demo -s dev --auto-approve
- atmos terraform destroy demo -s dev --auto-approve
- atmos terraform apply demo -s dev -auto-approve
- atmos terraform destroy demo -s dev -auto-approve
# Test staging stack
- atmos terraform plan demo -s staging
- atmos terraform apply demo -s staging --auto-approve
- atmos terraform destroy demo -s staging --auto-approve
- atmos terraform apply demo -s staging -auto-approve
- atmos terraform destroy demo -s staging -auto-approve
# Test prod stack
- atmos terraform plan demo -s prod
- atmos terraform apply demo -s prod --auto-approve
- atmos terraform destroy demo -s prod --auto-approve
- atmos terraform apply demo -s prod -auto-approve
- atmos terraform destroy demo -s prod -auto-approve

# Use Nested Custom Commands to provide easier interface for Docker Compose
- name: "localstack"
Expand Down
3 changes: 2 additions & 1 deletion internal/exec/atlantis_generate_repo_config.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package exec

import (
"fmt"
"path"
"path/filepath"
"reflect"
"strings"
Expand Down Expand Up @@ -338,7 +339,7 @@ func ExecuteAtlantisGenerateRepoConfig(
}

// Absolute path to the terraform component
terraformComponentPath := filepath.Join(
terraformComponentPath := path.Join(
atmosConfig.BasePath,
atmosConfig.Components.Terraform.BasePath,
terraformComponent,
Expand Down
6 changes: 3 additions & 3 deletions internal/exec/aws_eks_update_kubeconfig.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ package exec

import (
"fmt"
"path/filepath"
"path"
"strings"

"github.com/pkg/errors"
Expand Down Expand Up @@ -158,11 +158,11 @@ func ExecuteAwsEksUpdateKubeconfig(kubeconfigContext schema.AwsEksUpdateKubeconf

configAndStacksInfo.ComponentType = "terraform"
configAndStacksInfo, err = ProcessStacks(atmosConfig, configAndStacksInfo, true, true)
shellCommandWorkingDir = filepath.Join(atmosConfig.TerraformDirAbsolutePath, configAndStacksInfo.ComponentFolderPrefix, configAndStacksInfo.FinalComponent)
shellCommandWorkingDir = path.Join(atmosConfig.TerraformDirAbsolutePath, configAndStacksInfo.ComponentFolderPrefix, configAndStacksInfo.FinalComponent)
if err != nil {
configAndStacksInfo.ComponentType = "helmfile"
configAndStacksInfo, err = ProcessStacks(atmosConfig, configAndStacksInfo, true, true)
shellCommandWorkingDir = filepath.Join(atmosConfig.HelmfileDirAbsolutePath, configAndStacksInfo.ComponentFolderPrefix, configAndStacksInfo.FinalComponent)
shellCommandWorkingDir = path.Join(atmosConfig.HelmfileDirAbsolutePath, configAndStacksInfo.ComponentFolderPrefix, configAndStacksInfo.FinalComponent)
if err != nil {
return err
}
Expand Down
14 changes: 7 additions & 7 deletions internal/exec/describe_affected_utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -432,9 +432,9 @@ func executeDescribeAffected(
}

// Update paths to point to the cloned remote repo dir
atmosConfig.StacksBaseAbsolutePath = filepath.Join(remoteRepoFileSystemPath, basePath, atmosConfig.Stacks.BasePath)
atmosConfig.TerraformDirAbsolutePath = filepath.Join(remoteRepoFileSystemPath, basePath, atmosConfig.Components.Terraform.BasePath)
atmosConfig.HelmfileDirAbsolutePath = filepath.Join(remoteRepoFileSystemPath, basePath, atmosConfig.Components.Helmfile.BasePath)
atmosConfig.StacksBaseAbsolutePath = path.Join(remoteRepoFileSystemPath, basePath, atmosConfig.Stacks.BasePath)
atmosConfig.TerraformDirAbsolutePath = path.Join(remoteRepoFileSystemPath, basePath, atmosConfig.Components.Terraform.BasePath)
atmosConfig.HelmfileDirAbsolutePath = path.Join(remoteRepoFileSystemPath, basePath, atmosConfig.Components.Helmfile.BasePath)

atmosConfig.StackConfigFilesAbsolutePaths, err = u.JoinAbsolutePathWithPaths(
filepath.Join(remoteRepoFileSystemPath, basePath, atmosConfig.Stacks.BasePath),
Expand Down Expand Up @@ -1182,9 +1182,9 @@ func isComponentFolderChanged(

switch componentType {
case "terraform":
componentPath = filepath.Join(atmosConfig.BasePath, atmosConfig.Components.Terraform.BasePath, component)
componentPath = path.Join(atmosConfig.BasePath, atmosConfig.Components.Terraform.BasePath, component)
case "helmfile":
componentPath = filepath.Join(atmosConfig.BasePath, atmosConfig.Components.Helmfile.BasePath, component)
componentPath = path.Join(atmosConfig.BasePath, atmosConfig.Components.Helmfile.BasePath, component)
}

componentPathAbs, err := filepath.Abs(componentPath)
Expand Down Expand Up @@ -1220,7 +1220,7 @@ func areTerraformComponentModulesChanged(
changedFiles []string,
) (bool, error) {

componentPath := filepath.Join(atmosConfig.BasePath, atmosConfig.Components.Terraform.BasePath, component)
componentPath := path.Join(atmosConfig.BasePath, atmosConfig.Components.Terraform.BasePath, component)

componentPathAbs, err := filepath.Abs(componentPath)
if err != nil {
Expand All @@ -1241,7 +1241,7 @@ func areTerraformComponentModulesChanged(
continue
}

modulePath := filepath.Join(path.Dir(moduleConfig.Pos.Filename), moduleConfig.Source)
modulePath := path.Join(path.Dir(moduleConfig.Pos.Filename), moduleConfig.Source)

modulePathAbs, err := filepath.Abs(modulePath)
if err != nil {
Expand Down
10 changes: 5 additions & 5 deletions internal/exec/helmfile.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ package exec
import (
"fmt"
"os"
"path/filepath"
"path"

"github.com/pkg/errors"
"github.com/spf13/cobra"
Expand Down Expand Up @@ -78,20 +78,20 @@ func ExecuteHelmfile(info schema.ConfigAndStacksInfo) error {
}

// Check if the component exists as a helmfile component
componentPath := filepath.Join(atmosConfig.HelmfileDirAbsolutePath, info.ComponentFolderPrefix, info.FinalComponent)
componentPath := path.Join(atmosConfig.HelmfileDirAbsolutePath, info.ComponentFolderPrefix, info.FinalComponent)
componentPathExists, err := u.IsDirectory(componentPath)
if err != nil || !componentPathExists {
return fmt.Errorf("'%s' points to the Helmfile component '%s', but it does not exist in '%s'",
info.ComponentFromArg,
info.FinalComponent,
filepath.Join(atmosConfig.Components.Helmfile.BasePath, info.ComponentFolderPrefix),
path.Join(atmosConfig.Components.Helmfile.BasePath, info.ComponentFolderPrefix),
)
}

// Check if the component is allowed to be provisioned (`metadata.type` attribute)
if (info.SubCommand == "sync" || info.SubCommand == "apply" || info.SubCommand == "deploy") && info.ComponentIsAbstract {
return fmt.Errorf("abstract component '%s' cannot be provisioned since it's explicitly prohibited from being deployed "+
"by 'metadata.type: abstract' attribute", filepath.Join(info.ComponentFolderPrefix, info.Component))
"by 'metadata.type: abstract' attribute", path.Join(info.ComponentFolderPrefix, info.Component))
}

// Print component variables
Expand Down Expand Up @@ -196,7 +196,7 @@ func ExecuteHelmfile(info schema.ConfigAndStacksInfo) error {
u.LogDebug(atmosConfig, "Stack: "+info.StackFromArg)
} else {
u.LogDebug(atmosConfig, "Stack: "+info.StackFromArg)
u.LogDebug(atmosConfig, "Stack path: "+filepath.Join(atmosConfig.BasePath, atmosConfig.Stacks.BasePath, info.Stack))
u.LogDebug(atmosConfig, "Stack path: "+path.Join(atmosConfig.BasePath, atmosConfig.Stacks.BasePath, info.Stack))
}

workingDir := constructHelmfileComponentWorkingDir(atmosConfig, info)
Expand Down
6 changes: 3 additions & 3 deletions internal/exec/oci_utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import (
"fmt"
"io"
"os"
"path/filepath"
"path"

"github.com/google/go-containerregistry/pkg/name"
"github.com/google/go-containerregistry/pkg/v1/remote"
Expand All @@ -30,7 +30,7 @@ func processOciImage(atmosConfig schema.AtmosConfiguration, imageName string, de
defer removeTempDir(atmosConfig, tempDir)

// Temp tarball file name
tempTarFileName := filepath.Join(tempDir, uuid.New().String()) + ".tar"
tempTarFileName := path.Join(tempDir, uuid.New().String()) + ".tar"

// Get the image reference from the OCI registry
ref, err := name.ParseReference(imageName)
Expand Down Expand Up @@ -91,7 +91,7 @@ func processOciImage(atmosConfig schema.AtmosConfiguration, imageName string, de

// Extract the layers into the destination directory
for _, l := range manifest.Layers {
layerPath := filepath.Join(tempDir, l)
layerPath := path.Join(tempDir, l)

err = extractTarball(atmosConfig, layerPath, destDir)
if err != nil {
Expand Down
8 changes: 4 additions & 4 deletions internal/exec/stack_processor_utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -357,7 +357,7 @@ func ProcessYAMLConfigFile(

found := false
for _, extension := range extensions {
testPath := filepath.Join(basePath, imp+extension)
testPath := path.Join(basePath, imp+extension)
if _, err := os.Stat(testPath); err == nil {
impWithExt = imp + extension
found = true
Expand All @@ -372,12 +372,12 @@ func ProcessYAMLConfigFile(
} else if ext == u.YamlFileExtension || ext == u.YmlFileExtension {
// Check if there's a template version of this file
templatePath := impWithExt + u.TemplateExtension
if _, err := os.Stat(filepath.Join(basePath, templatePath)); err == nil {
if _, err := os.Stat(path.Join(basePath, templatePath)); err == nil {
impWithExt = templatePath
}
}

impWithExtPath := filepath.Join(basePath, impWithExt)
impWithExtPath := path.Join(basePath, impWithExt)

if impWithExtPath == filePath {
errorMessage := fmt.Sprintf("invalid import in the manifest '%s'\nThe file imports itself in '%s'",
Expand Down Expand Up @@ -2180,7 +2180,7 @@ func ProcessBaseComponentConfig(
if checkBaseComponentExists {
// Check if the base component exists as Terraform/Helmfile component
// If it does exist, don't throw errors if it is not defined in YAML config
componentPath := filepath.Join(componentBasePath, baseComponent)
componentPath := path.Join(componentBasePath, baseComponent)
componentPathExists, err := u.IsDirectory(componentPath)
if err != nil || !componentPathExists {
return errors.New("The component '" + component + "' inherits from the base component '" +
Expand Down
6 changes: 3 additions & 3 deletions internal/exec/stack_utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ package exec

import (
"fmt"
"path/filepath"
"path"
"strings"

cfg "github.com/cloudposse/atmos/pkg/config"
Expand Down Expand Up @@ -164,9 +164,9 @@ func BuildComponentPath(

if stackComponentSection, ok := componentSectionMap[cfg.ComponentSectionName].(string); ok {
if componentType == "terraform" {
componentPath = filepath.Join(atmosConfig.BasePath, atmosConfig.Components.Terraform.BasePath, stackComponentSection)
componentPath = path.Join(atmosConfig.BasePath, atmosConfig.Components.Terraform.BasePath, stackComponentSection)
} else if componentType == "helmfile" {
componentPath = filepath.Join(atmosConfig.BasePath, atmosConfig.Components.Helmfile.BasePath, stackComponentSection)
componentPath = path.Join(atmosConfig.BasePath, atmosConfig.Components.Helmfile.BasePath, stackComponentSection)
}
}

Expand Down
2 changes: 1 addition & 1 deletion internal/exec/terraform.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ import (
)

const (
autoApproveFlag = "--auto-approve"
autoApproveFlag = "-auto-approve"
outFlag = "-out"
varFileFlag = "-var-file"
skipTerraformLockFileFlag = "--skip-lock-file"
Expand Down
4 changes: 2 additions & 2 deletions internal/exec/terraform_generate_backend.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ package exec

import (
"fmt"
"path/filepath"
"path"

"github.com/pkg/errors"
"github.com/spf13/cobra"
Expand Down Expand Up @@ -75,7 +75,7 @@ func ExecuteTerraformGenerateBackendCmd(cmd *cobra.Command, args []string) error
}

// Write backend config to file
var backendFilePath = filepath.Join(
var backendFilePath = path.Join(
atmosConfig.BasePath,
atmosConfig.Components.Terraform.BasePath,
info.ComponentFolderPrefix,
Expand Down
5 changes: 3 additions & 2 deletions internal/exec/terraform_generate_backends.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package exec
import (
"errors"
"fmt"
"path"
"path/filepath"
"strings"

Expand Down Expand Up @@ -160,7 +161,7 @@ func ExecuteTerraformGenerateBackends(
}

// Path to the terraform component
terraformComponentPath := filepath.Join(
terraformComponentPath := path.Join(
atmosConfig.BasePath,
atmosConfig.Components.Terraform.BasePath,
terraformComponent,
Expand Down Expand Up @@ -290,7 +291,7 @@ func ExecuteTerraformGenerateBackends(

processedTerraformComponents[terraformComponent] = terraformComponent

backendFilePath = filepath.Join(
backendFilePath = path.Join(
terraformComponentPath,
"backend.tf",
)
Expand Down
3 changes: 2 additions & 1 deletion internal/exec/terraform_generate_varfiles.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package exec
import (
"errors"
"fmt"
"path"
"path/filepath"
"strings"

Expand Down Expand Up @@ -159,7 +160,7 @@ func ExecuteTerraformGenerateVarfiles(
}

// Absolute path to the terraform component
terraformComponentPath := filepath.Join(
terraformComponentPath := path.Join(
atmosConfig.BasePath,
atmosConfig.Components.Terraform.BasePath,
terraformComponent,
Expand Down
Loading

0 comments on commit 7765e3f

Please sign in to comment.