-
Notifications
You must be signed in to change notification settings - Fork 170
/
scaffold.go
171 lines (145 loc) · 4.33 KB
/
scaffold.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
package scaffold
import (
"bytes"
"fmt"
"io/fs"
"os"
"path"
"path/filepath"
"strings"
"text/template"
"github.com/azure/azure-dev/cli/azd/pkg/osutil"
"github.com/azure/azure-dev/cli/azd/resources"
)
const baseRoot = "scaffold/base"
const templateRoot = "scaffold/templates"
// Copy base assets to the target directory.
func CopyBase(targetDir string) error {
err := copyFS(
resources.ScaffoldBase,
baseRoot,
targetDir)
if err != nil {
return fmt.Errorf("copying base assets: %w", err)
}
return nil
}
func copyFS(embedFs fs.FS, root string, target string) error {
return fs.WalkDir(embedFs, root, func(name string, d fs.DirEntry, err error) error {
if err != nil {
return err
}
targetPath := filepath.Join(target, name[len(root):])
if d.IsDir() {
return os.MkdirAll(targetPath, osutil.PermissionDirectory)
}
contents, err := fs.ReadFile(embedFs, name)
if err != nil {
return fmt.Errorf("reading file: %w", err)
}
return os.WriteFile(targetPath, contents, osutil.PermissionFile)
})
}
// Load loads all templates as a template.Template.
//
// To execute a named template, call Execute with the defined name.
func Load() (*template.Template, error) {
funcMap := template.FuncMap{
"bicepName": BicepName,
"containerAppName": ContainerAppName,
"upper": strings.ToUpper,
"lower": strings.ToLower,
"formatParam": FormatParameter,
}
t, err := template.New("templates").
Option("missingkey=error").
Funcs(funcMap).
ParseFS(resources.ScaffoldTemplates,
path.Join(templateRoot, "*"))
if err != nil {
return nil, fmt.Errorf("parsing templates: %w", err)
}
return t, nil
}
// Execute applies the template associated with t that has the given name
// to the specified data object and writes the output to the dest path on the filesystem.
func Execute(
t *template.Template,
name string,
data any,
dest string) error {
buf := bytes.NewBufferString("")
err := t.ExecuteTemplate(buf, name, data)
if err != nil {
return fmt.Errorf("executing template: %w", err)
}
err = os.WriteFile(dest, buf.Bytes(), osutil.PermissionFile)
if err != nil {
return fmt.Errorf("writing file: %w", err)
}
return nil
}
// ExecInfra scaffolds infrastructure files for the given spec, using the loaded templates in t. The resulting files
// are written to the target directory.
func ExecInfra(
t *template.Template,
spec InfraSpec,
target string) error {
infraRoot := target
infraApp := filepath.Join(infraRoot, "app")
// Pre-execution expansion. Additional parameters are added, derived from the initial spec.
preExecExpand(&spec)
err := CopyBase(infraRoot)
if err != nil {
return err
}
if err = os.MkdirAll(infraApp, osutil.PermissionDirectory); err != nil {
return err
}
if spec.DbCosmosMongo != nil {
err = Execute(t, "db-cosmos-mongo.bicep", spec.DbCosmosMongo, filepath.Join(infraApp, "db-cosmos-mongo.bicep"))
if err != nil {
return fmt.Errorf("scaffolding cosmos mongodb: %w", err)
}
}
if spec.DbPostgres != nil {
err = Execute(t, "db-postgres.bicep", spec.DbPostgres, filepath.Join(infraApp, "db-postgres.bicep"))
if err != nil {
return fmt.Errorf("scaffolding postgres: %w", err)
}
}
for _, svc := range spec.Services {
err = Execute(t, "host-containerapp.bicep", svc, filepath.Join(infraApp, svc.Name+".bicep"))
if err != nil {
return fmt.Errorf("scaffolding containerapp: %w", err)
}
}
err = Execute(t, "main.bicep", spec, filepath.Join(infraRoot, "main.bicep"))
if err != nil {
return fmt.Errorf("scaffolding main.bicep: %w", err)
}
err = Execute(t, "main.parameters.json", spec, filepath.Join(infraRoot, "main.parameters.json"))
if err != nil {
return fmt.Errorf("scaffolding main.parameters.json: %w", err)
}
return nil
}
func preExecExpand(spec *InfraSpec) {
// postgres requires specific password seeding parameters
if spec.DbPostgres != nil {
spec.Parameters = append(spec.Parameters,
Parameter{
Name: "databasePassword",
Value: "$(secretOrRandomPassword ${AZURE_KEY_VAULT_NAME} databasePassword)",
Type: "string",
Secret: true,
})
}
for _, svc := range spec.Services {
// containerapp requires a global '_exist' parameter for each service
spec.Parameters = append(spec.Parameters,
containerAppExistsParameter(svc.Name))
spec.Parameters = append(spec.Parameters,
serviceDefPlaceholder(svc.Name))
}
}