-
-
Notifications
You must be signed in to change notification settings - Fork 153
Expand file tree
/
Copy pathworkdir_clean.go
More file actions
123 lines (100 loc) · 3.59 KB
/
workdir_clean.go
File metadata and controls
123 lines (100 loc) · 3.59 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
package workdir
import (
"github.com/spf13/cobra"
"github.com/spf13/viper"
errUtils "github.com/cloudposse/atmos/errors"
cfg "github.com/cloudposse/atmos/pkg/config"
"github.com/cloudposse/atmos/pkg/flags"
"github.com/cloudposse/atmos/pkg/perf"
"github.com/cloudposse/atmos/pkg/schema"
"github.com/cloudposse/atmos/pkg/ui"
"github.com/cloudposse/atmos/pkg/ui/theme"
)
var cleanParser *flags.StandardParser
var cleanCmd = &cobra.Command{
Use: "clean [component]",
Short: "Clean workdir(s)",
Long: `Remove component working directories.
Use --all to clean all workdirs, or specify a component and stack to clean a specific workdir.
Workdirs are ephemeral and can be regenerated by running 'atmos terraform init'.`,
Example: ` # Clean a specific workdir
atmos terraform workdir clean vpc --stack dev
# Clean all workdirs
atmos terraform workdir clean --all`,
Args: cobra.MaximumNArgs(1),
RunE: func(cmd *cobra.Command, args []string) error {
defer perf.Track(atmosConfigPtr, "workdir.clean.RunE")()
v := viper.GetViper()
// Bind flags to viper at runtime to ensure flag values are available.
if err := cleanParser.BindFlagsToViper(cmd, v); err != nil {
return err
}
all := v.GetBool("all")
stack := v.GetString("stack")
// Validate arguments.
if all && len(args) > 0 {
return errUtils.Build(errUtils.ErrWorkdirClean).
WithExplanation("Cannot specify both --all and a component").
WithHint("Use --all alone to clean all workdirs, or specify a component and stack").
Err()
}
if !all && len(args) == 0 {
return errUtils.Build(errUtils.ErrWorkdirClean).
WithExplanation("Either --all or a component is required").
WithHint("Use --all to clean all workdirs, or specify a component with --stack").
Err()
}
if !all && stack == "" {
return errUtils.Build(errUtils.ErrWorkdirClean).
WithExplanation("Stack is required when cleaning a specific workdir").
WithHint("Use --stack to specify the stack").
Err()
}
// Initialize config with global flags (--base-path, --config, etc.).
configInfo := buildConfigAndStacksInfo(cmd, v)
atmosConfig, err := cfg.InitCliConfig(configInfo, false)
if err != nil {
return errUtils.Build(errUtils.ErrWorkdirClean).
WithCause(err).
WithExplanation("Failed to load atmos configuration").
Err()
}
// Clean workdirs.
if all {
return cleanAllWorkdirs(&atmosConfig)
}
component := args[0]
return cleanSpecificWorkdir(&atmosConfig, component, stack)
},
}
func cleanAllWorkdirs(atmosConfig *schema.AtmosConfiguration) error {
defer perf.Track(atmosConfig, "workdir.cleanAllWorkdirs")()
if err := workdirManager.CleanAllWorkdirs(atmosConfig); err != nil {
return err
}
ui.Writef("%s All workdirs cleaned\n", theme.Styles.Checkmark.String())
return nil
}
func cleanSpecificWorkdir(atmosConfig *schema.AtmosConfiguration, component, stack string) error {
defer perf.Track(atmosConfig, "workdir.cleanSpecificWorkdir")()
if err := workdirManager.CleanWorkdir(atmosConfig, component, stack); err != nil {
return err
}
ui.Writef("%s Workdir cleaned for %s in %s\n", theme.Styles.Checkmark.String(), component, stack)
return nil
}
func init() {
cleanCmd.DisableFlagParsing = false
// Create parser with functional options.
cleanParser = flags.NewStandardParser(
flags.WithStackFlag(),
flags.WithBoolFlag("all", "a", false, "Clean all workdirs"),
flags.WithEnvVars("all", "ATMOS_WORKDIR_CLEAN_ALL"),
)
// Register flags with the command.
cleanParser.RegisterFlags(cleanCmd)
// Bind flags to Viper.
if err := cleanParser.BindToViper(viper.GetViper()); err != nil {
panic(err)
}
}