-
-
Notifications
You must be signed in to change notification settings - Fork 153
Expand file tree
/
Copy pathatmos.go
More file actions
190 lines (165 loc) · 5.7 KB
/
atmos.go
File metadata and controls
190 lines (165 loc) · 5.7 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
package exec
import (
"fmt"
"strings"
"github.com/cloudposse/atmos/pkg/perf"
log "github.com/cloudposse/atmos/pkg/logger"
"github.com/samber/lo"
tui "github.com/cloudposse/atmos/internal/tui/atmos"
cfg "github.com/cloudposse/atmos/pkg/config"
"github.com/cloudposse/atmos/pkg/schema"
u "github.com/cloudposse/atmos/pkg/utils"
)
// ExecuteAtmosCmd executes `atmos` command.
func ExecuteAtmosCmd() error {
defer perf.Track(nil, "exec.ExecuteAtmosCmd")()
commands := []string{
"terraform plan",
"terraform apply",
"terraform destroy",
"terraform init",
"terraform output",
"terraform clean",
"terraform workspace",
"terraform refresh",
"terraform show",
"terraform validate",
"terraform shell",
"validate component",
"describe component",
"describe dependents",
}
configAndStacksInfo := schema.ConfigAndStacksInfo{}
atmosConfig, err := cfg.InitCliConfig(configAndStacksInfo, true)
if err != nil {
return err
}
// Get a map of stacks and components in the stacks
// Don't process `Go` templates and YAML functions in Atmos stack manifests since we just need to display the stack and component names in the TUI
stacksMap, err := ExecuteDescribeStacks(&atmosConfig, "", nil, nil, nil, false, false, false, false, nil, nil)
if err != nil {
return err
}
// Create a map of stacks to lists of components in each stack
stacksComponentsMap := lo.MapEntries(stacksMap, func(k string, v any) (string, []string) {
if v2, ok := v.(map[string]any); ok {
if v3, ok := v2["components"].(map[string]any); ok {
if v4, ok := v3["terraform"].(map[string]any); ok {
return k, FilterAbstractComponents(v4)
}
// TODO: process 'helmfile' components and stacks.
// This will require checking the list of commands and filtering the stacks and components depending on the selected command.
}
}
return k, nil
})
// Get a set of all components
componentsSet := lo.Uniq(lo.Flatten(lo.Values(stacksComponentsMap)))
// Create a map of components to lists of stacks for each component
componentsStacksMap := make(map[string][]string)
lo.ForEach(componentsSet, func(c string, _ int) {
var stacksForComponent []string
for k, v := range stacksComponentsMap {
if u.SliceContainsString(v, c) {
stacksForComponent = append(stacksForComponent, k)
}
}
componentsStacksMap[c] = stacksForComponent
})
// Sort the maps by the keys, and sort the lists of values
stacksComponentsMap = u.SortMapByKeysAndValuesUniq(stacksComponentsMap)
componentsStacksMap = u.SortMapByKeysAndValuesUniq(componentsStacksMap)
// Start the UI
app, err := tui.Execute(commands, stacksComponentsMap, componentsStacksMap)
fmt.Println()
if err != nil {
return err
}
selectedCommand := app.GetSelectedCommand()
selectedComponent := app.GetSelectedComponent()
selectedStack := app.GetSelectedStack()
// If the user quit the UI, exit
if app.ExitStatusQuit() || selectedCommand == "" || selectedComponent == "" || selectedStack == "" {
return nil
}
// Process the selected command, stack and component
c := fmt.Sprintf("atmos %s %s --stack %s", selectedCommand, selectedComponent, selectedStack)
log.Info("Executing", "command", c)
if selectedCommand == "describe component" {
data, err := ExecuteDescribeComponent(&ExecuteDescribeComponentParams{
Component: selectedComponent,
Stack: selectedStack,
ProcessTemplates: true,
ProcessYamlFunctions: true,
Skip: nil,
AuthManager: nil,
})
if err != nil {
return err
}
err = u.PrintAsYAML(&atmosConfig, data)
if err != nil {
return err
}
return nil
}
if selectedCommand == "describe dependents" {
data, err := ExecuteDescribeDependents(&atmosConfig, &DescribeDependentsArgs{
Component: selectedComponent,
Stack: selectedStack,
IncludeSettings: false,
ProcessTemplates: true,
ProcessYamlFunctions: true,
Skip: nil,
OnlyInStack: "",
})
if err != nil {
return err
}
err = u.PrintAsYAML(&atmosConfig, data)
if err != nil {
return err
}
return nil
}
if selectedCommand == "validate component" {
_, err = ExecuteValidateComponent(&atmosConfig, schema.ConfigAndStacksInfo{}, selectedComponent, selectedStack, "", "", nil, 0)
if err != nil {
return err
}
log.Info("Validated successfully", "component", selectedComponent, "stack", selectedStack)
return nil
}
// All Terraform commands.
if strings.HasPrefix(selectedCommand, "terraform") {
parts := strings.Split(selectedCommand, " ")
subcommand := parts[1]
// "terraform shell" is an Atmos-only command (not a native terraform subcommand).
// Route it directly to ExecuteTerraformShell to avoid ExecuteTerraform passing
// it to the terraform executable.
if subcommand == "shell" {
return ExecuteTerraformShell(shellOptionsForUI(selectedComponent, selectedStack), &atmosConfig)
}
configAndStacksInfo.ComponentType = "terraform"
configAndStacksInfo.Component = selectedComponent
configAndStacksInfo.ComponentFromArg = selectedComponent
configAndStacksInfo.Stack = selectedStack
configAndStacksInfo.SubCommand = subcommand
configAndStacksInfo.ProcessTemplates = true
configAndStacksInfo.ProcessFunctions = true
err = ExecuteTerraform(configAndStacksInfo)
if err != nil {
return err
}
}
return nil
}
// shellOptionsForUI builds ShellOptions for the interactive UI dispatch path.
// The UI doesn't support DryRun or Identity selection, so those default to zero values.
func shellOptionsForUI(component, stack string) *ShellOptions {
return &ShellOptions{
Component: component,
Stack: stack,
ProcessingOptions: ProcessingOptions{ProcessTemplates: true, ProcessFunctions: true},
}
}