forked from Azure/aztfexport
-
Notifications
You must be signed in to change notification settings - Fork 0
/
command_before_func.go
176 lines (163 loc) · 5.68 KB
/
command_before_func.go
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
package main
import (
"fmt"
"os"
"strings"
"github.com/Azure/aztfexport/internal/meta"
"github.com/Azure/aztfexport/internal/utils"
"github.com/hashicorp/terraform-config-inspect/tfconfig"
"github.com/urfave/cli/v2"
)
func commandBeforeFunc(fset *FlagSet) func(ctx *cli.Context) error {
return func(_ *cli.Context) error {
// Common flags check
if fset.flagAppend {
if fset.flagOverwrite {
return fmt.Errorf("`--append` conflicts with `--overwrite`")
}
}
if !fset.flagNonInteractive {
if fset.flagContinue {
return fmt.Errorf("`--continue` must be used together with `--non-interactive`")
}
if fset.flagGenerateMappingFile {
return fmt.Errorf("`--generate-mapping-file` must be used together with `--non-interactive`")
}
}
if fset.flagHCLOnly {
if fset.flagAppend {
return fmt.Errorf("`--append` conflicts with `--hcl-only`")
}
if fset.flagModulePath != "" {
return fmt.Errorf("`--module-path` conflicts with `--hcl-only`")
}
}
if fset.flagModulePath != "" {
if !fset.flagAppend {
return fmt.Errorf("`--module-path` must be used together with `--append`")
}
}
if fset.flagDevProvider {
if fset.flagProviderVersion != "" {
return fmt.Errorf("`--dev-provider` conflicts with `--provider-version`")
}
}
if flagLogLevel != "" {
if _, err := logLevel(flagLogLevel); err != nil {
return err
}
}
// Initialize output directory
if _, err := os.Stat(fset.flagOutputDir); os.IsNotExist(err) {
if err := os.MkdirAll(fset.flagOutputDir, 0750); err != nil {
return fmt.Errorf("creating output directory %q: %v", fset.flagOutputDir, err)
}
}
empty, err := utils.DirIsEmpty(fset.flagOutputDir)
if err != nil {
return fmt.Errorf("failed to check emptiness of output directory %q: %v", fset.flagOutputDir, err)
}
var tfblock *utils.TerraformBlockDetail
if !empty {
switch {
case fset.flagOverwrite:
if err := utils.RemoveEverythingUnder(fset.flagOutputDir, meta.ResourceMappingFileName); err != nil {
return fmt.Errorf("failed to clean up output directory %q: %v", fset.flagOutputDir, err)
}
case fset.flagAppend:
tfblock, err = utils.InspecTerraformBlock(fset.flagOutputDir)
if err != nil {
return fmt.Errorf("determine the backend type from the existing files: %v", err)
}
default:
if fset.flagNonInteractive {
return fmt.Errorf("the output directory %q is not empty", fset.flagOutputDir)
}
// Interactive mode
fmt.Printf(`
The output directory is not empty. Please choose one of actions below:
* Press "Y" to overwrite the existing directory with new files
* Press "N" to append new files and add to the existing state instead
* Press other keys to quit
> `)
var ans string
// #nosec G104
fmt.Scanf("%s", &ans)
switch strings.ToLower(ans) {
case "y":
if err := utils.RemoveEverythingUnder(fset.flagOutputDir, meta.ResourceMappingFileName); err != nil {
return err
}
case "n":
if fset.flagHCLOnly {
return fmt.Errorf("`--hcl-only` can only run within an empty directory. Use `-o` to specify an empty directory.")
}
fset.flagAppend = true
tfblock, err = utils.InspecTerraformBlock(fset.flagOutputDir)
if err != nil {
return fmt.Errorf("determine the backend type from the existing files: %v", err)
}
default:
return fmt.Errorf("the output directory %q is not empty", fset.flagOutputDir)
}
}
}
// Deterimine the real backend type to use
var existingBackendType string
if tfblock != nil {
existingBackendType = "local"
if tfblock.BackendType != "" {
existingBackendType = tfblock.BackendType
}
}
switch {
case fset.flagBackendType != "" && existingBackendType != "":
if fset.flagBackendType != existingBackendType {
return fmt.Errorf("the backend type defined in existing files (%s) are not the same as is specified in the CLI (%s)", existingBackendType, fset.flagBackendType)
}
case fset.flagBackendType == "" && existingBackendType == "":
fset.flagBackendType = "local"
case fset.flagBackendType == "" && existingBackendType != "":
fset.flagBackendType = existingBackendType
case fset.flagBackendType != "" && existingBackendType == "":
// do nothing
}
// Check backend related flags
if len(fset.flagBackendConfig.Value()) != 0 {
if existingBackendType != "" {
return fmt.Errorf("`--backend-config` should not be specified when appending to a workspace that has terraform block already defined")
}
if fset.flagBackendType == "local" {
return fmt.Errorf("`--backend-config` only works for non-local backend")
}
}
if fset.flagBackendType != "local" {
if fset.flagHCLOnly {
return fmt.Errorf("`--hcl-only` only works for local backend")
}
}
// Determine any existing provider version constraint if not using a dev provider and the provider version not specified.
if !fset.flagDevProvider && fset.flagProviderVersion == "" {
module, err := tfconfig.LoadModule(fset.flagOutputDir)
if err != nil {
return fmt.Errorf("loading terraform config: %v", err)
}
if azurecfg, ok := module.RequiredProviders["azurerm"]; ok {
fset.flagProviderVersion = strings.Join(azurecfg.VersionConstraints, " ")
}
}
// Identify the subscription id, which comes from one of following (starts from the highest priority):
// - Command line option
// - Env variable: AZTFEXPORT_SUBSCRIPTION_ID
// - Env variable: ARM_SUBSCRIPTION_ID
// - Output of azure cli, the current active subscription
if fset.flagSubscriptionId == "" {
var err error
fset.flagSubscriptionId, err = subscriptionIdFromCLI()
if err != nil {
return fmt.Errorf("retrieving subscription id from CLI: %v", err)
}
}
return nil
}
}