-
-
Notifications
You must be signed in to change notification settings - Fork 174
Expand file tree
/
Copy pathoperations.go
More file actions
177 lines (158 loc) · 6.26 KB
/
Copy pathoperations.go
File metadata and controls
177 lines (158 loc) · 6.26 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
package config
import (
"github.com/spf13/cobra"
errUtils "github.com/cloudposse/atmos/errors"
cfg "github.com/cloudposse/atmos/pkg/config"
"github.com/cloudposse/atmos/pkg/data"
"github.com/cloudposse/atmos/pkg/perf"
"github.com/cloudposse/atmos/pkg/schema"
"github.com/cloudposse/atmos/pkg/ui"
u "github.com/cloudposse/atmos/pkg/utils"
atmosyaml "github.com/cloudposse/atmos/pkg/yaml"
)
// valueType holds the --type flag for `config set`.
var valueType string
var configGetCmd = &cobra.Command{
Use: "get <path>",
Short: "Read a value from the effective Atmos configuration by dot-notation path",
Long: `Read a value using a dot-notation path (e.g. logs.level) from the effective,
fully-merged Atmos configuration for this invocation -- the same configuration
"terraform plan", "list stacks", etc. actually use, including every --config
file, --config-path directory, and profile applied on top of each other. This
can differ from what a single physical atmos.yaml file declares on its own
(cloudposse/atmos#2867): use "atmos config format" or read the file directly
to inspect one file's own declared value instead.`,
Example: "atmos config get logs.level",
Args: cobra.ExactArgs(1),
RunE: func(cmd *cobra.Command, args []string) error {
defer perf.Track(atmosConfigPtr, "config.getRunE")()
// Reload rather than reuse atmosConfigPtr, mirroring configListCmd: this keeps `get`
// independently correct (and independently testable via RunE) even before root.go's
// PersistentPreRun has populated the package-level pointer. Safe to call with an empty
// ConfigAndStacksInfo{} -- LoadConfig now falls back to os.Args/env for
// --config/--config-path/--base-path itself (cloudposse/atmos#2868).
atmosConfig, err := cfg.InitCliConfig(schema.ConfigAndStacksInfo{}, false)
if err != nil {
return err
}
effectiveYAML, err := u.ConvertToYAML(atmosConfig)
if err != nil {
return err
}
value, err := atmosyaml.Get([]byte(effectiveYAML), args[0])
if err != nil {
return err
}
return data.Writeln(value)
},
}
var configSetCmd = &cobra.Command{
Use: "set <path> <value>",
Short: "Set a value in atmos.yaml by dot-notation path",
Long: `Set a value in atmos.yaml using a dot-notation path, preserving comments,
anchors, YAML functions, and templates. The value's type (string, int, bool,
float) is inferred from the Atmos config schema when the path matches a known
field (e.g. mcp.enabled infers bool); pass --type explicitly to override, or
for paths the schema doesn't model (falls back to string).`,
Example: "atmos config set logs.level debug\natmos config set mcp.enabled true\natmos config set --type=yaml logs.exclude '[\"a\", \"b\"]'",
Args: cobra.ExactArgs(2),
RunE: func(cmd *cobra.Command, args []string) error {
defer perf.Track(atmosConfigPtr, "config.setRunE")()
file, err := resolveConfigFile(cmd)
if err != nil {
return err
}
created, err := atmosyaml.SetFileWithType(file, args[0], args[1], effectiveValueType(cmd, args[0]))
if err != nil {
return err
}
if created {
ui.Successf("Created `%s` = `%s` in `%s`", args[0], args[1], atmosyaml.DisplayPath(file))
return nil
}
ui.Successf("Updated `%s` to `%s` in `%s`", args[0], args[1], atmosyaml.DisplayPath(file))
return nil
},
}
// effectiveValueType returns the --type flag's value when the user passed it
// explicitly. Otherwise it infers a type from the Atmos config schema for
// dotPath (e.g. a known bool field), falling back to the flag's default
// (atmosyaml.TypeString) when the path isn't modeled by the schema -- most
// commonly free-form sections like vars.
func effectiveValueType(cmd *cobra.Command, dotPath string) string {
if cmd.Flags().Changed("type") {
return valueType
}
if inferred, ok := cfg.InferValueType(dotPath); ok {
return inferred
}
return valueType
}
var configDeleteCmd = &cobra.Command{
Use: "delete <path>",
Aliases: []string{"del", "unset"},
Short: "Delete a value from atmos.yaml by dot-notation path",
Long: "Delete a value from atmos.yaml using a dot-notation path, preserving the rest of the file.",
Example: "atmos config delete components.terraform.append_user_agent",
Args: cobra.ExactArgs(1),
RunE: func(cmd *cobra.Command, args []string) error {
defer perf.Track(atmosConfigPtr, "config.deleteRunE")()
file, err := resolveConfigFile(cmd)
if err != nil {
return err
}
existed, err := atmosyaml.DeleteFile(file, args[0])
if err != nil {
return err
}
if !existed {
ui.Successf("Nothing to delete — `%s` is not set in `%s`", args[0], atmosyaml.DisplayPath(file))
return nil
}
ui.Successf("Deleted `%s` from `%s`", args[0], atmosyaml.DisplayPath(file))
return nil
},
}
var configFormatCmd = &cobra.Command{
Use: "format",
Aliases: []string{"fmt"},
Short: "Format the active atmos.yaml file",
Long: `Format the active atmos.yaml file in place, preserving comments, anchors,
Atmos YAML functions, and Go templates.`,
Example: "atmos config format\natmos --config ./config/atmos.yaml config format",
Args: cobra.NoArgs,
RunE: func(cmd *cobra.Command, args []string) error {
defer perf.Track(atmosConfigPtr, "config.formatRunE")()
file, err := resolveConfigFile(cmd)
if err != nil {
return err
}
if err := atmosyaml.FormatFile(file); err != nil {
return err
}
ui.Successf("Formatted `%s`", atmosyaml.DisplayPath(file))
return nil
},
}
func init() {
configSetCmd.Flags().StringVar(&valueType, "type", atmosyaml.TypeString,
"Value type: string, int, bool, float, null, or yaml (raw literal). "+
"Auto-inferred from the Atmos config schema when omitted and the path is recognized.")
}
// resolveConfigFile picks the atmos.yaml to edit. The inherited persistent
// --config flag (first entry) acts as an explicit override; otherwise the file
// is discovered in the current directory or git root.
func resolveConfigFile(cmd *cobra.Command) (string, error) {
override := ""
if cfgFiles, _ := cmd.Flags().GetStringSlice("config"); len(cfgFiles) > 0 {
override = cfgFiles[0]
}
file, err := cfg.ResolveEditableConfigFile(atmosConfigPtr, override)
if err != nil {
return "", errUtils.Build(errUtils.ErrInvalidArgumentError).
WithExplanation(err.Error()).
WithHint("Run from a directory containing atmos.yaml, or pass --config <file>.").
Err()
}
return file, nil
}