Skip to content

Commit

Permalink
Update atmos describe affected and atmos terraform commands (#654)
Browse files Browse the repository at this point in the history
* updates

* updates

* updates

* updates

* updates

* updates

* updates
  • Loading branch information
aknysh authored Jul 18, 2024
1 parent 8060adb commit db0ac7c
Show file tree
Hide file tree
Showing 9 changed files with 100 additions and 57 deletions.
6 changes: 3 additions & 3 deletions examples/quick-start-advanced/Dockerfile
Original file line number Diff line number Diff line change
@@ -1,15 +1,15 @@
# Geodesic: https://github.com/cloudposse/geodesic/
ARG GEODESIC_VERSION=2.11.3
ARG GEODESIC_VERSION=3.0.0
ARG GEODESIC_OS=debian

# Atmos
# https://atmos.tools/
# https://github.com/cloudposse/atmos
# https://github.com/cloudposse/atmos/releases
ARG ATMOS_VERSION=1.84.0
ARG ATMOS_VERSION=1.85.0

# Terraform: https://github.com/hashicorp/terraform/releases
ARG TF_VERSION=1.9.1
ARG TF_VERSION=1.9.2

FROM cloudposse/geodesic:${GEODESIC_VERSION}-${GEODESIC_OS}

Expand Down
10 changes: 5 additions & 5 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -10,11 +10,11 @@ require (
github.com/bmatcuk/doublestar/v4 v4.6.1
github.com/charmbracelet/bubbles v0.18.0
github.com/charmbracelet/bubbletea v0.26.6
github.com/charmbracelet/lipgloss v0.11.1
github.com/charmbracelet/lipgloss v0.12.1
github.com/elewis787/boa v0.1.2
github.com/fatih/color v1.17.0
github.com/go-git/go-git/v5 v5.12.0
github.com/google/go-containerregistry v0.20.0
github.com/google/go-containerregistry v0.20.1
github.com/google/go-github/v59 v59.0.0
github.com/google/uuid v1.6.0
github.com/hairyhenderson/gomplate/v3 v3.11.8
Expand All @@ -33,13 +33,13 @@ require (
github.com/open-policy-agent/opa v0.66.0
github.com/otiai10/copy v1.14.0
github.com/pkg/errors v0.9.1
github.com/samber/lo v1.44.0
github.com/samber/lo v1.46.0
github.com/santhosh-tekuri/jsonschema/v5 v5.3.1
github.com/spf13/cobra v1.8.1
github.com/spf13/pflag v1.0.5
github.com/spf13/viper v1.19.0
github.com/stretchr/testify v1.9.0
github.com/zclconf/go-cty v1.14.4
github.com/zclconf/go-cty v1.15.0
gopkg.in/yaml.v2 v2.4.0
mvdan.cc/sh/v3 v3.8.0
)
Expand Down Expand Up @@ -92,7 +92,7 @@ require (
github.com/cenkalti/backoff/v3 v3.2.2 // indirect
github.com/cespare/xxhash/v2 v2.2.0 // indirect
github.com/chainguard-dev/git-urls v1.0.2 // indirect
github.com/charmbracelet/x/ansi v0.1.3 // indirect
github.com/charmbracelet/x/ansi v0.1.4 // indirect
github.com/charmbracelet/x/input v0.1.2 // indirect
github.com/charmbracelet/x/term v0.1.1 // indirect
github.com/charmbracelet/x/windows v0.1.2 // indirect
Expand Down
20 changes: 10 additions & 10 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -389,10 +389,10 @@ github.com/charmbracelet/bubbles v0.18.0 h1:PYv1A036luoBGroX6VWjQIE9Syf2Wby2oOl/
github.com/charmbracelet/bubbles v0.18.0/go.mod h1:08qhZhtIwzgrtBjAcJnij1t1H0ZRjwHyGsy6AL11PSw=
github.com/charmbracelet/bubbletea v0.26.6 h1:zTCWSuST+3yZYZnVSvbXwKOPRSNZceVeqpzOLN2zq1s=
github.com/charmbracelet/bubbletea v0.26.6/go.mod h1:dz8CWPlfCCGLFbBlTY4N7bjLiyOGDJEnd2Muu7pOWhk=
github.com/charmbracelet/lipgloss v0.11.1 h1:a8KgVPHa7kOoP95vm2tQQrjD2AKhbWmfr4uJ2RW6kNk=
github.com/charmbracelet/lipgloss v0.11.1/go.mod h1:beLlcmkF7MWA+5UrKKIRo/VJ21xGXr7YJ9miWfdMRIU=
github.com/charmbracelet/x/ansi v0.1.3 h1:RBh/eleNWML5R524mjUF0yVRePTwqN9tPtV+DPgO5Lw=
github.com/charmbracelet/x/ansi v0.1.3/go.mod h1:dk73KoMTT5AX5BsX0KrqhsTqAnhZZoCBjs7dGWp4Ktw=
github.com/charmbracelet/lipgloss v0.12.1 h1:/gmzszl+pedQpjCOH+wFkZr/N90Snz40J/NR7A0zQcs=
github.com/charmbracelet/lipgloss v0.12.1/go.mod h1:V2CiwIuhx9S1S1ZlADfOj9HmxeMAORuz5izHb0zGbB8=
github.com/charmbracelet/x/ansi v0.1.4 h1:IEU3D6+dWwPSgZ6HBH+v6oUuZ/nVawMiWj5831KfiLM=
github.com/charmbracelet/x/ansi v0.1.4/go.mod h1:dk73KoMTT5AX5BsX0KrqhsTqAnhZZoCBjs7dGWp4Ktw=
github.com/charmbracelet/x/input v0.1.2 h1:QJAZr33eOhDowkkEQ24rsJy4Llxlm+fRDf/cQrmqJa0=
github.com/charmbracelet/x/input v0.1.2/go.mod h1:LGBim0maUY4Pitjn/4fHnuXb4KirU3DODsyuHuXdOyA=
github.com/charmbracelet/x/term v0.1.1 h1:3cosVAiPOig+EV4X9U+3LDgtwwAoEzJjNdwbXDjF6yI=
Expand Down Expand Up @@ -635,8 +635,8 @@ github.com/google/go-cmp v0.5.8/go.mod h1:17dUlkBOakJ0+DkrSSNjCkIjxS6bF9zb3elmeN
github.com/google/go-cmp v0.5.9/go.mod h1:17dUlkBOakJ0+DkrSSNjCkIjxS6bF9zb3elmeNGIjoY=
github.com/google/go-cmp v0.6.0 h1:ofyhxvXcZhMsU5ulbFiLKl/XBFqE1GSq7atu8tAmTRI=
github.com/google/go-cmp v0.6.0/go.mod h1:17dUlkBOakJ0+DkrSSNjCkIjxS6bF9zb3elmeNGIjoY=
github.com/google/go-containerregistry v0.20.0 h1:wRqHpOeVh3DnenOrPy9xDOLdnLatiGuuNRVelR2gSbg=
github.com/google/go-containerregistry v0.20.0/go.mod h1:YCMFNQeeXeLF+dnhhWkqDItx/JSkH01j1Kis4PsjzFI=
github.com/google/go-containerregistry v0.20.1 h1:eTgx9QNYugV4DN5mz4U8hiAGTi1ybXn0TPi4Smd8du0=
github.com/google/go-containerregistry v0.20.1/go.mod h1:YCMFNQeeXeLF+dnhhWkqDItx/JSkH01j1Kis4PsjzFI=
github.com/google/go-github/v59 v59.0.0 h1:7h6bgpF5as0YQLLkEiVqpgtJqjimMYhBkD4jT5aN3VA=
github.com/google/go-github/v59 v59.0.0/go.mod h1:rJU4R0rQHFVFDOkqGWxfLNo6vEk4dv40oDjhV/gH6wM=
github.com/google/go-querystring v1.1.0 h1:AnCroh3fv4ZBgVIf1Iwtovgjaw/GiKJo8M8yD/fhyJ8=
Expand Down Expand Up @@ -1089,8 +1089,8 @@ github.com/sagikazarmark/slog-shim v0.1.0 h1:diDBnUNK9N/354PgrxMywXnAwEr1QZcOr6g
github.com/sagikazarmark/slog-shim v0.1.0/go.mod h1:SrcSrq8aKtyuqEI1uvTDTK1arOWRIczQRv+GVI1AkeQ=
github.com/sahilm/fuzzy v0.1.1-0.20230530133925-c48e322e2a8f h1:MvTmaQdww/z0Q4wrYjDSCcZ78NoftLQyHBSLW/Cx79Y=
github.com/sahilm/fuzzy v0.1.1-0.20230530133925-c48e322e2a8f/go.mod h1:VFvziUEIMCrT6A6tw2RFIXPXXmzXbOsSHF0DOI8ZK9Y=
github.com/samber/lo v1.44.0 h1:5il56KxRE+GHsm1IR+sZ/6J42NODigFiqCWpSc2dybA=
github.com/samber/lo v1.44.0/go.mod h1:RmDH9Ct32Qy3gduHQuKJ3gW1fMHAnE/fAzQuf6He5cU=
github.com/samber/lo v1.46.0 h1:w8G+oaCPgz1PoCJztqymCFaKwXt+5cCXn51uPxExFfQ=
github.com/samber/lo v1.46.0/go.mod h1:RmDH9Ct32Qy3gduHQuKJ3gW1fMHAnE/fAzQuf6He5cU=
github.com/santhosh-tekuri/jsonschema/v5 v5.3.1 h1:lZUw3E0/J3roVtGQ+SCrUrg3ON6NgVqpn3+iol9aGu4=
github.com/santhosh-tekuri/jsonschema/v5 v5.3.1/go.mod h1:uToXkOrWAZ6/Oc07xWQrPOhJotwFIyu2bBVN41fcDUY=
github.com/satori/go.uuid v1.2.0/go.mod h1:dA0hQrYB0VpLJoorglMZABFdXlWrHn1NEOzdhQKdks0=
Expand Down Expand Up @@ -1185,8 +1185,8 @@ github.com/yuin/goldmark v1.2.1/go.mod h1:3hX8gzYuyVAZsxl0MRgGTJEmQBFcNTphYh9dec
github.com/yuin/goldmark v1.3.5/go.mod h1:mwnBkeHKe2W/ZEtQ+71ViKU8L12m81fl3OWwC1Zlc8k=
github.com/yuin/goldmark v1.4.0/go.mod h1:mwnBkeHKe2W/ZEtQ+71ViKU8L12m81fl3OWwC1Zlc8k=
github.com/yuin/goldmark v1.4.13/go.mod h1:6yULJ656Px+3vBD8DxQVa3kxgyrAnzto9xy5taEt/CY=
github.com/zclconf/go-cty v1.14.4 h1:uXXczd9QDGsgu0i/QFR/hzI5NYCHLf6NQw/atrbnhq8=
github.com/zclconf/go-cty v1.14.4/go.mod h1:VvMs5i0vgZdhYawQNq5kePSpLAoz8u1xvZgrPIxfnZE=
github.com/zclconf/go-cty v1.15.0 h1:tTCRWxsexYUmtt/wVxgDClUe+uQusuI443uL6e+5sXQ=
github.com/zclconf/go-cty v1.15.0/go.mod h1:VvMs5i0vgZdhYawQNq5kePSpLAoz8u1xvZgrPIxfnZE=
github.com/zclconf/go-cty-debug v0.0.0-20240509010212-0d6042c53940 h1:4r45xpDWB6ZMSMNJFMOjqrGHynW3DIBuR2H9j0ug+Mo=
github.com/zclconf/go-cty-debug v0.0.0-20240509010212-0d6042c53940/go.mod h1:CmBdvvj3nqzfzJ6nTCIwDTPZ56aVGvDrmztiO5g3qrM=
github.com/zealic/xignore v0.3.3 h1:EpLXUgZY/JEzFkTc+Y/VYypzXtNz+MSOMVCGW5Q4CKQ=
Expand Down
49 changes: 39 additions & 10 deletions internal/exec/describe_affected_utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import (
"time"

"github.com/go-git/go-git/v5"
"github.com/go-git/go-git/v5/config"
"github.com/go-git/go-git/v5/plumbing"
"github.com/go-git/go-git/v5/plumbing/transport/ssh"
"github.com/hashicorp/terraform-config-inspect/tfconfig"
Expand Down Expand Up @@ -56,7 +57,7 @@ func ExecuteDescribeAffectedWithTargetRefClone(
}

// Get the Git config of the local repo
localRepoConfig, err := localRepo.Config()
localRepoConfig, err := getRepoConfig(localRepo)
if err != nil {
return nil, nil, nil, "", errors.Join(err, localRepoIsNotGitRepoError)
}
Expand Down Expand Up @@ -231,7 +232,7 @@ func ExecuteDescribeAffectedWithTargetRefCheckout(
}

// Check the Git config of the local repo
localRepoConfig, err := localRepo.Config()
localRepoConfig, err := getRepoConfig(localRepo)
if err != nil {
return nil, nil, nil, "", errors.Join(err, localRepoIsNotGitRepoError)
}
Expand Down Expand Up @@ -312,7 +313,7 @@ func ExecuteDescribeAffectedWithTargetRefCheckout(
}

// Check the Git config of the target ref
_, err = remoteRepo.Config()
_, err = getRepoConfig(remoteRepo)
if err != nil {
return nil, nil, nil, "", errors.Join(err, remoteRepoIsNotGitRepoError)
}
Expand Down Expand Up @@ -410,7 +411,7 @@ func ExecuteDescribeAffectedWithTargetRepoPath(
}

// Check the Git config of the local repo
localRepoConfig, err := localRepo.Config()
localRepoConfig, err := getRepoConfig(localRepo)
if err != nil {
return nil, nil, nil, "", errors.Join(err, localRepoIsNotGitRepoError)
}
Expand Down Expand Up @@ -452,7 +453,7 @@ func ExecuteDescribeAffectedWithTargetRepoPath(
}

// Check the Git config of the remote target repo
_, err = remoteRepo.Config()
_, err = getRepoConfig(remoteRepo)
if err != nil {
return nil, nil, nil, "", errors.Join(err, remoteRepoIsNotGitRepoError)
}
Expand Down Expand Up @@ -1559,7 +1560,7 @@ func addDependentsToDependents(

func processIncludedInDependencies(affected *[]schema.Affected) bool {
for i := 0; i < len(*affected); i++ {
a := &(*affected)[i]
a := &((*affected)[i])
a.IncludedInDependents = processIncludedInDependenciesForAffected(affected, a.StackSlug, i)
}
return false
Expand All @@ -1571,26 +1572,54 @@ func processIncludedInDependenciesForAffected(affected *[]schema.Affected, stack
continue
}

a := &(*affected)[i]
a := &((*affected)[i])

if len(a.Dependents) > 0 {
return processIncludedInDependenciesForDependents(&a.Dependents, stackSlug)
includedInDeps := processIncludedInDependenciesForDependents(&a.Dependents, stackSlug)
if includedInDeps {
return true
}
}
}
return false
}

func processIncludedInDependenciesForDependents(dependents *[]schema.Dependent, stackSlug string) bool {
for i := 0; i < len(*dependents); i++ {
d := &(*dependents)[i]
d := &((*dependents)[i])

if d.StackSlug == stackSlug {
return true
}

if len(d.Dependents) > 0 {
return processIncludedInDependenciesForDependents(&d.Dependents, stackSlug)
includedInDeps := processIncludedInDependenciesForDependents(&d.Dependents, stackSlug)
if includedInDeps {
return true
}
}
}
return false
}

func getRepoConfig(repo *git.Repository) (*config.Config, error) {
repoConfig, err := repo.Config()
if err != nil {
return nil, err
}

core := repoConfig.Raw.Section("core")

// Remove the untrackedCache option if it exists
if coreOption := core.Option("untrackedCache"); coreOption != "" {
core.RemoveOption("untrackedCache")

// Save the updated configuration
err = repo.Storer.SetConfig(repoConfig)
if err != nil {
return nil, err
}
}

return repoConfig, nil
}
13 changes: 11 additions & 2 deletions internal/exec/template_utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,17 @@ import (
)

// ProcessTmpl parses and executes Go templates
func ProcessTmpl(tmplName string, tmplValue string, tmplData any, ignoreMissingTemplateValues bool) (string, error) {
funcs := lo.Assign(FuncMap(context.TODO(), &data.Data{}), sprig.FuncMap())
func ProcessTmpl(
tmplName string,
tmplValue string,
tmplData any,
ignoreMissingTemplateValues bool,
) (string, error) {
d := data.Data{}
ctx := context.TODO()

// Add Gomplate, Sprig and Atmos template functions
funcs := lo.Assign(gomplate.CreateFuncs(ctx, &d), sprig.FuncMap(), FuncMap(ctx, &d))

t, err := template.New(tmplName).Funcs(funcs).Parse(tmplValue)
if err != nil {
Expand Down
51 changes: 28 additions & 23 deletions internal/exec/terraform.go
Original file line number Diff line number Diff line change
Expand Up @@ -371,41 +371,46 @@ func ExecuteTerraform(info schema.ConfigAndStacksInfo) error {
allArgsAndFlags = append(allArgsAndFlags, info.AdditionalArgsAndFlags...)

// Run `terraform workspace` before executing other terraform commands
// only if the `TF_WORKSPACE` environment variable is not set by the caller
if info.SubCommand != "init" && !(info.SubCommand == "workspace" && info.SubCommand2 != "") {
workspaceSelectRedirectStdErr := "/dev/stdout"
tfWorkspaceEnvVar := os.Getenv("TF_WORKSPACE")

// If `--redirect-stderr` flag is not passed, always redirect `stderr` to `stdout` for `terraform workspace select` command
if info.RedirectStdErr != "" {
workspaceSelectRedirectStdErr = info.RedirectStdErr
}
if tfWorkspaceEnvVar == "" {
workspaceSelectRedirectStdErr := "/dev/stdout"

err = ExecuteShellCommand(
cliConfig,
info.Command,
[]string{"workspace", "select", info.TerraformWorkspace},
componentPath,
info.ComponentEnvList,
info.DryRun,
workspaceSelectRedirectStdErr,
)
if err != nil {
var osErr *osexec.ExitError
ok := errors.As(err, &osErr)
if !ok || osErr.ExitCode() != 1 {
// err is not a non-zero exit code or err is not exit code 1, which we are expecting
return err
// If `--redirect-stderr` flag is not passed, always redirect `stderr` to `stdout` for `terraform workspace select` command
if info.RedirectStdErr != "" {
workspaceSelectRedirectStdErr = info.RedirectStdErr
}

err = ExecuteShellCommand(
cliConfig,
info.Command,
[]string{"workspace", "new", info.TerraformWorkspace},
[]string{"workspace", "select", info.TerraformWorkspace},
componentPath,
info.ComponentEnvList,
info.DryRun,
info.RedirectStdErr,
workspaceSelectRedirectStdErr,
)
if err != nil {
return err
var osErr *osexec.ExitError
ok := errors.As(err, &osErr)
if !ok || osErr.ExitCode() != 1 {
// err is not a non-zero exit code or err is not exit code 1, which we are expecting
return err
}
err = ExecuteShellCommand(
cliConfig,
info.Command,
[]string{"workspace", "new", info.TerraformWorkspace},
componentPath,
info.ComponentEnvList,
info.DryRun,
info.RedirectStdErr,
)
if err != nil {
return err
}
}
}
}
Expand Down
4 changes: 2 additions & 2 deletions internal/tui/workflow/tui.go
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
package workflow

import (
"github.com/cloudposse/atmos/pkg/schema"

tea "github.com/charmbracelet/bubbletea"
mouseZone "github.com/lrstanley/bubblezone"

"github.com/cloudposse/atmos/pkg/schema"
)

// Execute starts the TUI app and returns the selected items from the views
Expand Down
2 changes: 1 addition & 1 deletion website/docs/integrations/atlantis.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -673,7 +673,7 @@ on:
branches: [ main ]
env:
ATMOS_VERSION: 1.84.0
ATMOS_VERSION: 1.85.0
ATMOS_CLI_CONFIG_PATH: ./
jobs:
Expand Down
2 changes: 1 addition & 1 deletion website/docs/integrations/github-actions/setup-atmos.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,6 @@ jobs:
uses: cloudposse/github-action-setup-atmos
with:
# Make sure to pin to the latest version of atmos
atmos_version: 1.84.0
atmos_version: 1.85.0
```
</File>

0 comments on commit db0ac7c

Please sign in to comment.