Skip to content

Commit 6bde3fc

Browse files
authored
Update atmos describe affected command (#1267)
* updates * updates * updates * updates * updates * updates * updates * updates * updates * updates * updates * updates * updates * updates * updates * updates * updates * updates * updates * updates * updates * updates * updates * updates * updates * updates * updates * updates * updates
1 parent 1953e23 commit 6bde3fc

File tree

17 files changed

+922
-439
lines changed

17 files changed

+922
-439
lines changed

go.mod

Lines changed: 2 additions & 2 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

go.sum

Lines changed: 4 additions & 4 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

internal/exec/atlantis_generate_repo_config.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -155,7 +155,7 @@ func ExecuteAtlantisGenerateRepoConfigAffectedOnly(
155155

156156
if repoPath != "" {
157157
affected, _, _, _, err = ExecuteDescribeAffectedWithTargetRepoPath(
158-
atmosConfig,
158+
&atmosConfig,
159159
repoPath,
160160
verbose,
161161
false,
@@ -167,7 +167,7 @@ func ExecuteAtlantisGenerateRepoConfigAffectedOnly(
167167
)
168168
} else if cloneTargetRef {
169169
affected, _, _, _, err = ExecuteDescribeAffectedWithTargetRefClone(
170-
atmosConfig,
170+
&atmosConfig,
171171
ref,
172172
sha,
173173
sshKeyPath,
@@ -182,7 +182,7 @@ func ExecuteAtlantisGenerateRepoConfigAffectedOnly(
182182
)
183183
} else {
184184
affected, _, _, _, err = ExecuteDescribeAffectedWithTargetRefCheckout(
185-
atmosConfig,
185+
&atmosConfig,
186186
ref,
187187
sha,
188188
verbose,

internal/exec/describe_affected.go

Lines changed: 2 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ import (
1616
)
1717

1818
type DescribeAffectedCmdArgs struct {
19-
CLIConfig schema.AtmosConfiguration
19+
CLIConfig *schema.AtmosConfiguration
2020
CloneTargetRef bool
2121
Format string
2222
IncludeDependents bool
@@ -178,7 +178,7 @@ func parseDescribeAffectedCliArgs(cmd *cobra.Command, args []string) (DescribeAf
178178
}
179179

180180
result := DescribeAffectedCmdArgs{
181-
CLIConfig: atmosConfig,
181+
CLIConfig: &atmosConfig,
182182
CloneTargetRef: cloneTargetRef,
183183
Format: format,
184184
IncludeDependents: includeDependents,
@@ -316,11 +316,6 @@ func ExecuteDescribeAffectedCmd(cmd *cobra.Command, args []string) error {
316316
}
317317
}
318318
} else {
319-
atmosConfig, err := cfg.InitCliConfig(schema.ConfigAndStacksInfo{}, true)
320-
if err != nil {
321-
return err
322-
}
323-
324319
res, err := u.EvaluateYqExpression(&atmosConfig, affected, a.Query)
325320
if err != nil {
326321
return err
Lines changed: 123 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,123 @@
1+
package exec
2+
3+
import (
4+
"os"
5+
"testing"
6+
7+
"github.com/spf13/cobra"
8+
"github.com/stretchr/testify/assert"
9+
10+
cfg "github.com/cloudposse/atmos/pkg/config"
11+
"github.com/cloudposse/atmos/pkg/schema"
12+
u "github.com/cloudposse/atmos/pkg/utils"
13+
)
14+
15+
func TestExecuteDescribeAffectedCmd(t *testing.T) {
16+
stacksPath := "../../tests/fixtures/scenarios/atmos-describe-affected"
17+
18+
err := os.Setenv("ATMOS_CLI_CONFIG_PATH", stacksPath)
19+
assert.NoError(t, err, "Setting 'ATMOS_CLI_CONFIG_PATH' environment variable should execute without error")
20+
21+
err = os.Setenv("ATMOS_BASE_PATH", stacksPath)
22+
assert.NoError(t, err, "Setting 'ATMOS_BASE_PATH' environment variable should execute without error")
23+
24+
defer func() {
25+
err := os.Unsetenv("ATMOS_BASE_PATH")
26+
assert.NoError(t, err)
27+
err = os.Unsetenv("ATMOS_CLI_CONFIG_PATH")
28+
assert.NoError(t, err)
29+
}()
30+
31+
cmd := &cobra.Command{
32+
Use: "describe affected",
33+
Short: "List Atmos components and stacks affected by two Git commits",
34+
Long: "Identify and list Atmos components and stacks impacted by changes between two Git commits.",
35+
FParseErrWhitelist: struct{ UnknownFlags bool }{UnknownFlags: false},
36+
Run: func(cmd *cobra.Command, args []string) {
37+
err := ExecuteDescribeAffectedCmd(cmd, args)
38+
if err != nil {
39+
u.PrintErrorMarkdownAndExit("", err, "")
40+
}
41+
},
42+
}
43+
44+
cmd.PersistentFlags().String("repo-path", "", "Filesystem path to the already cloned target repository with which to compare the current branch")
45+
cmd.PersistentFlags().String("ref", "", "Git reference with which to compare the current branch. Refer to [10.3 Git Internals Git References](https://git-scm.com/book/en/v2/Git-Internals-Git-References) for more details")
46+
cmd.PersistentFlags().String("sha", "", "Git commit SHA with which to compare the current branch")
47+
cmd.PersistentFlags().String("file", "", "Write the result to the file")
48+
cmd.PersistentFlags().String("format", "json", "The output format. (`json` is default)")
49+
cmd.PersistentFlags().Bool("verbose", false, "Print more detailed output when cloning and checking out the Git repository")
50+
cmd.PersistentFlags().String("ssh-key", "", "Path to PEM-encoded private key to clone private repos using SSH")
51+
cmd.PersistentFlags().String("ssh-key-password", "", "Encryption password for the PEM-encoded private key if the key contains a password-encrypted PEM block")
52+
cmd.PersistentFlags().Bool("include-spacelift-admin-stacks", false, "Include the Spacelift admin stack of any stack that is affected by config changes")
53+
cmd.PersistentFlags().Bool("include-dependents", false, "Include the dependent components and stacks")
54+
cmd.PersistentFlags().Bool("include-settings", false, "Include the `settings` section for each affected component")
55+
cmd.PersistentFlags().Bool("upload", false, "Upload the affected components and stacks to a specified HTTP endpoint")
56+
cmd.PersistentFlags().Bool("clone-target-ref", false, "Clone the target reference with which to compare the current branch\n"+
57+
"If set to `false` (default), the target reference will be checked out instead\n"+
58+
"This requires that the target reference is already cloned by Git, and the information about it exists in the `.git` directory")
59+
cmd.PersistentFlags().Bool("process-templates", true, "Enable/disable Go template processing in Atmos stack manifests when executing the command")
60+
cmd.PersistentFlags().Bool("process-functions", true, "Enable/disable YAML functions processing in Atmos stack manifests when executing the command")
61+
cmd.PersistentFlags().StringSlice("skip", nil, "Skip executing a YAML function when processing Atmos stack manifests")
62+
cmd.PersistentFlags().Bool("no-color", false, "Disable color output")
63+
cmd.PersistentFlags().StringP("query", "q", "", "Query the results of an `atmos describe` command using `yq` expressions")
64+
cmd.PersistentFlags().String("logs-level", "Info", "Logs level. Supported log levels are Trace, Debug, Info, Warning, Off. If the log level is set to Off, Atmos will not log any messages")
65+
cmd.PersistentFlags().String("logs-file", "/dev/stderr", "The file to write Atmos logs to. Logs can be written to any file or any standard file descriptor, including '/dev/stdout', '/dev/stderr' and '/dev/null'")
66+
cmd.PersistentFlags().String("base-path", "", "Base path for Atmos project")
67+
cmd.PersistentFlags().StringSlice("config", []string{}, "Paths to configuration files (comma-separated or repeated flag)")
68+
cmd.PersistentFlags().StringSlice("config-path", []string{}, "Paths to configuration directories (comma-separated or repeated flag)")
69+
cmd.PersistentFlags().StringP("stack", "s", "",
70+
"Filter by a specific stack\n"+
71+
"The filter supports names of the top-level stack manifests (including subfolder paths), and `atmos` stack names (derived from the context vars)",
72+
)
73+
74+
// Execute the command
75+
cmd.SetArgs([]string{"--clone-target-ref=true", "--include-settings=true"})
76+
err = cmd.Execute()
77+
assert.NoError(t, err, "'atmos describe affected' command should execute without error")
78+
}
79+
80+
func TestExecuteDescribeAffectedWithTargetRepoPath(t *testing.T) {
81+
stacksPath := "../../tests/fixtures/scenarios/atmos-describe-affected"
82+
83+
err := os.Setenv("ATMOS_CLI_CONFIG_PATH", stacksPath)
84+
assert.NoError(t, err, "Setting 'ATMOS_CLI_CONFIG_PATH' environment variable should execute without error")
85+
86+
err = os.Setenv("ATMOS_BASE_PATH", stacksPath)
87+
assert.NoError(t, err, "Setting 'ATMOS_BASE_PATH' environment variable should execute without error")
88+
89+
defer func() {
90+
err := os.Unsetenv("ATMOS_BASE_PATH")
91+
assert.NoError(t, err)
92+
err = os.Unsetenv("ATMOS_CLI_CONFIG_PATH")
93+
assert.NoError(t, err)
94+
}()
95+
96+
atmosConfig, err := cfg.InitCliConfig(schema.ConfigAndStacksInfo{}, true)
97+
assert.Nil(t, err)
98+
99+
// We are using `atmos.yaml` from this dir. This `atmos.yaml` has set base_path: "./",
100+
// which will be wrong for the remote repo which is cloned into a temp dir.
101+
// Set the correct base path for the cloned remote repo
102+
atmosConfig.BasePath = "./tests/fixtures/scenarios/atmos-describe-affected"
103+
104+
// Point to the same local repository
105+
// This will compare this local repository with itself as the remote target, which should result in an empty `affected` list
106+
repoPath := "../../"
107+
108+
affected, _, _, _, err := ExecuteDescribeAffectedWithTargetRepoPath(
109+
&atmosConfig,
110+
repoPath,
111+
false,
112+
false,
113+
true,
114+
"",
115+
false,
116+
false,
117+
nil,
118+
)
119+
assert.Nil(t, err)
120+
121+
// The `affected` list should be empty, since the local repo is compared with itself.
122+
assert.Equal(t, 0, len(affected))
123+
}

0 commit comments

Comments
 (0)