-
-
Notifications
You must be signed in to change notification settings - Fork 153
Expand file tree
/
Copy pathbackend.go
More file actions
120 lines (101 loc) · 3.79 KB
/
backend.go
File metadata and controls
120 lines (101 loc) · 3.79 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
package generate
import (
"github.com/spf13/cobra"
"github.com/spf13/viper"
"github.com/cloudposse/atmos/cmd/terraform/shared"
errUtils "github.com/cloudposse/atmos/errors"
e "github.com/cloudposse/atmos/internal/exec"
cfg "github.com/cloudposse/atmos/pkg/config"
"github.com/cloudposse/atmos/pkg/flags"
"github.com/cloudposse/atmos/pkg/schema"
)
// backendParser handles flag parsing for backend command.
var backendParser *flags.StandardParser
// backendCmd generates backend config for a terraform component.
var backendCmd = &cobra.Command{
Use: "backend [component]",
Short: "Generate backend configuration for a Terraform component",
Long: `This command generates the backend configuration for a Terraform component using the specified stack`,
Args: cobra.MaximumNArgs(1),
FParseErrWhitelist: struct{ UnknownFlags bool }{UnknownFlags: false},
RunE: func(cmd *cobra.Command, args []string) error {
var component string
if len(args) > 0 {
component = args[0]
}
// Use Viper to respect precedence (flag > env > config > default)
v := viper.GetViper()
// Bind backend-specific flags to Viper.
if err := backendParser.BindFlagsToViper(cmd, v); err != nil {
return err
}
// Get stack early so we can use it to filter component selection.
stack := v.GetString("stack")
// Prompt for component if missing.
// If stack is already provided (via --stack flag), filter components to that stack.
if component == "" {
prompted, err := shared.PromptForComponent(cmd, stack)
if err = shared.HandlePromptError(err, "component"); err != nil {
return err
}
component = prompted
}
// Validate component after prompting.
if component == "" {
return errUtils.ErrMissingComponent
}
// Get remaining flag values from Viper.
processTemplates := v.GetBool("process-templates")
processFunctions := v.GetBool("process-functions")
skip := v.GetStringSlice("skip")
// Prompt for stack if missing.
if stack == "" {
prompted, err := shared.PromptForStack(cmd, component)
if err = shared.HandlePromptError(err, "stack"); err != nil {
return err
}
stack = prompted
}
// Validate stack after prompting.
if stack == "" {
return errUtils.ErrMissingStack
}
// Initialize Atmos configuration
atmosConfig, err := cfg.InitCliConfig(schema.ConfigAndStacksInfo{}, true)
if err != nil {
return err
}
opts := &e.GenerateBackendOptions{
Component: component,
Stack: stack,
ProcessingOptions: e.ProcessingOptions{
ProcessTemplates: processTemplates,
ProcessFunctions: processFunctions,
Skip: skip,
},
}
return e.ExecuteGenerateBackend(opts, &atmosConfig)
},
}
func init() {
// Create parser with backend-specific flags using functional options.
backendParser = flags.NewStandardParser(
flags.WithStringFlag("stack", "s", "", "Atmos stack (required)"),
flags.WithBoolFlag("process-templates", "", true, "Enable Go template processing in Atmos stack manifests"),
flags.WithBoolFlag("process-functions", "", true, "Enable YAML functions processing in Atmos stack manifests"),
flags.WithStringSliceFlag("skip", "", []string{}, "Skip processing specific Atmos YAML functions"),
flags.WithEnvVars("stack", "ATMOS_STACK"),
flags.WithEnvVars("process-templates", "ATMOS_PROCESS_TEMPLATES"),
flags.WithEnvVars("process-functions", "ATMOS_PROCESS_FUNCTIONS"),
flags.WithEnvVars("skip", "ATMOS_SKIP"),
)
// Register flags with the command.
backendParser.RegisterFlags(backendCmd)
// Bind flags to Viper for environment variable support.
if err := backendParser.BindToViper(viper.GetViper()); err != nil {
panic(err)
}
// Register shell completion for component.
backendCmd.ValidArgsFunction = shared.ComponentsArgCompletion
GenerateCmd.AddCommand(backendCmd)
}