-
Notifications
You must be signed in to change notification settings - Fork 167
/
infra_synth.go
241 lines (202 loc) · 6.29 KB
/
infra_synth.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
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
package cmd
import (
"context"
"fmt"
"io/fs"
"log"
"os"
"path/filepath"
"github.com/azure/azure-dev/cli/azd/cmd/actions"
"github.com/azure/azure-dev/cli/azd/internal"
"github.com/azure/azure-dev/cli/azd/pkg/alpha"
"github.com/azure/azure-dev/cli/azd/pkg/environment/azdcontext"
"github.com/azure/azure-dev/cli/azd/pkg/input"
"github.com/azure/azure-dev/cli/azd/pkg/osutil"
"github.com/azure/azure-dev/cli/azd/pkg/output"
"github.com/azure/azure-dev/cli/azd/pkg/output/ux"
"github.com/azure/azure-dev/cli/azd/pkg/project"
"github.com/otiai10/copy"
"github.com/spf13/cobra"
"github.com/spf13/pflag"
)
type infraSynthFlags struct {
global *internal.GlobalCommandOptions
*internal.EnvFlag
force bool
}
func newInfraSynthFlags(cmd *cobra.Command, global *internal.GlobalCommandOptions) *infraSynthFlags {
flags := &infraSynthFlags{
EnvFlag: &internal.EnvFlag{},
}
flags.Bind(cmd.Flags(), global)
return flags
}
func (f *infraSynthFlags) Bind(local *pflag.FlagSet, global *internal.GlobalCommandOptions) {
f.global = global
f.EnvFlag.Bind(local, global)
local.BoolVar(&f.force, "force", false, "Overwrite any existing files without prompting")
}
func newInfraSynthCmd() *cobra.Command {
return &cobra.Command{
Use: "synth",
Short: fmt.Sprintf(
"Write IaC for your project to disk, allowing you to manage it by hand. %s", output.WithWarningFormat("(Beta)")),
}
}
type infraSynthAction struct {
projectConfig *project.ProjectConfig
importManager *project.ImportManager
console input.Console
azdCtx *azdcontext.AzdContext
flags *infraSynthFlags
alphaManager *alpha.FeatureManager
}
func newInfraSynthAction(
projectConfig *project.ProjectConfig,
importManager *project.ImportManager,
flags *infraSynthFlags,
console input.Console,
azdCtx *azdcontext.AzdContext,
alphaManager *alpha.FeatureManager,
) actions.Action {
return &infraSynthAction{
projectConfig: projectConfig,
importManager: importManager,
flags: flags,
console: console,
azdCtx: azdCtx,
alphaManager: alphaManager,
}
}
var infraSynthFeature = alpha.MustFeatureKey("infraSynth")
func (a *infraSynthAction) Run(ctx context.Context) (*actions.ActionResult, error) {
if !a.alphaManager.IsEnabled(infraSynthFeature) {
return nil, fmt.Errorf(
"infrastructure synthesis is currently under alpha support and must be explicitly enabled."+
" Run `%s` to enable this feature.", alpha.GetEnableCommand(infraSynthFeature),
)
}
a.console.WarnForFeature(ctx, infraSynthFeature)
spinnerMessage := "Synthesizing infrastructure"
a.console.ShowSpinner(ctx, spinnerMessage, input.Step)
synthFS, err := a.importManager.SynthAllInfrastructure(ctx, a.projectConfig)
if err != nil {
a.console.StopSpinner(ctx, spinnerMessage, input.StepFailed)
return nil, err
}
a.console.StopSpinner(ctx, spinnerMessage, input.StepDone)
staging, err := os.MkdirTemp("", "infra-synth")
if err != nil {
return nil, err
}
defer os.RemoveAll(staging)
err = fs.WalkDir(synthFS, ".", func(path string, d fs.DirEntry, err error) error {
if err != nil {
return err
}
if d.IsDir() {
return nil
}
contents, err := fs.ReadFile(synthFS, path)
if err != nil {
return err
}
err = os.MkdirAll(filepath.Join(staging, filepath.Dir(path)), osutil.PermissionDirectoryOwnerOnly)
if err != nil {
return err
}
return os.WriteFile(filepath.Join(staging, path), contents, d.Type().Perm())
})
if err != nil {
return nil, err
}
options := copy.Options{}
if a.flags.force {
options.Skip = func(fileInfo os.FileInfo, src, dest string) (bool, error) {
return false, nil
}
} else {
skipStagingFiles, err := a.promptForDuplicates(ctx, staging, a.azdCtx.ProjectDirectory())
if err != nil {
return nil, err
}
if skipStagingFiles != nil {
options.Skip = func(fileInfo os.FileInfo, src, dest string) (bool, error) {
_, skip := skipStagingFiles[src]
return skip, nil
}
}
}
if err := copy.Copy(staging, a.azdCtx.ProjectDirectory(), options); err != nil {
return nil, fmt.Errorf("copying contents from temp staging directory: %w", err)
}
return nil, nil
}
func (a *infraSynthAction) promptForDuplicates(
ctx context.Context, staging string, target string) (skipSourceFiles map[string]struct{}, err error) {
log.Printf(
"infrastructure synth, checking for duplicates. source: %s target: %s",
staging,
target,
)
duplicateFiles, err := determineDuplicates(staging, target)
if err != nil {
return nil, fmt.Errorf("checking for overwrites: %w", err)
}
if len(duplicateFiles) > 0 {
a.console.StopSpinner(ctx, "", input.StepDone)
a.console.MessageUxItem(ctx, &ux.WarningMessage{
Description: "The following files would be overwritten by synthesized versions:",
})
for _, file := range duplicateFiles {
a.console.Message(ctx, fmt.Sprintf(" * %s", file))
}
selection, err := a.console.Select(ctx, input.ConsoleOptions{
Message: "What would you like to do with these files?",
Options: []string{
"Overwrite with the synthesized versions",
"Keep my existing files unchanged",
},
})
if err != nil {
return nil, fmt.Errorf("prompting to overwrite: %w", err)
}
switch selection {
case 0: // overwrite
return nil, nil
case 1: // keep
skipSourceFiles = make(map[string]struct{}, len(duplicateFiles))
for _, file := range duplicateFiles {
// this also cleans the result, which is important for matching
sourceFile := filepath.Join(staging, file)
skipSourceFiles[sourceFile] = struct{}{}
}
return skipSourceFiles, nil
}
}
return nil, nil
}
// Returns files that are both present in source and target.
// The files returned are expressed in their relative paths to source/target.
func determineDuplicates(source string, target string) ([]string, error) {
var duplicateFiles []string
if err := filepath.WalkDir(source, func(path string, d fs.DirEntry, walkErr error) error {
if walkErr != nil {
return walkErr
}
if d.IsDir() {
return nil
}
partial, err := filepath.Rel(source, path)
if err != nil {
return fmt.Errorf("computing relative path: %w", err)
}
if _, err := os.Stat(filepath.Join(target, partial)); err == nil {
duplicateFiles = append(duplicateFiles, partial)
}
return nil
}); err != nil {
return nil, fmt.Errorf("enumerating template files: %w", err)
}
return duplicateFiles, nil
}