-
Notifications
You must be signed in to change notification settings - Fork 170
/
project.go
227 lines (185 loc) · 7.43 KB
/
project.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
package project
import (
"bytes"
"context"
"fmt"
"log"
"os"
"path/filepath"
"strings"
"github.com/azure/azure-dev/cli/azd/internal"
"github.com/azure/azure-dev/cli/azd/internal/tracing"
"github.com/azure/azure-dev/cli/azd/internal/tracing/fields"
"github.com/azure/azure-dev/cli/azd/pkg/config"
"github.com/azure/azure-dev/cli/azd/pkg/ext"
"github.com/azure/azure-dev/cli/azd/pkg/infra/provisioning"
"github.com/azure/azure-dev/cli/azd/pkg/osutil"
"github.com/blang/semver/v4"
"golang.org/x/exp/slices"
"gopkg.in/yaml.v3"
)
const (
//nolint:lll
projectSchemaAnnotation = "# yaml-language-server: $schema=https://raw.githubusercontent.com/Azure/azure-dev/main/schemas/v1.0/azure.yaml.json"
)
func New(ctx context.Context, projectFilePath string, projectName string) (*ProjectConfig, error) {
newProject := &ProjectConfig{
Name: projectName,
}
err := Save(ctx, newProject, projectFilePath)
if err != nil {
return nil, fmt.Errorf("marshaling project file to yaml: %w", err)
}
return Load(ctx, projectFilePath)
}
// Parse will parse a project from a yaml string and return the project configuration
func Parse(ctx context.Context, yamlContent string) (*ProjectConfig, error) {
var projectConfig ProjectConfig
if strings.TrimSpace(yamlContent) == "" {
return nil, fmt.Errorf("unable to parse azure.yaml file. File is empty.")
}
if err := yaml.Unmarshal([]byte(yamlContent), &projectConfig); err != nil {
return nil, fmt.Errorf(
"unable to parse azure.yaml file. Check the format of the file, "+
"and also verify you have the latest version of the CLI: %w",
err,
)
}
projectConfig.EventDispatcher = ext.NewEventDispatcher[ProjectLifecycleEventArgs]()
if projectConfig.RequiredVersions != nil && projectConfig.RequiredVersions.Azd != nil {
supportedRange, err := semver.ParseRange(*projectConfig.RequiredVersions.Azd)
if err != nil {
return nil, fmt.Errorf("%s is not a valid semver range (for requiredVersions.azd): %w",
*projectConfig.RequiredVersions.Azd, err)
}
if !internal.IsDevVersion() && !supportedRange(internal.VersionInfo().Version) {
return nil, fmt.Errorf("this project requires a version of azd within the range '%s', but you have '%s'. "+
"Visit https://aka.ms/azure-dev/install to install a supported version.",
*projectConfig.RequiredVersions.Azd,
internal.VersionInfo().Version.String())
}
}
var err error
projectConfig.Infra.Provider, err = provisioning.ParseProvider(projectConfig.Infra.Provider)
if err != nil {
return nil, fmt.Errorf("parsing project %s: %w", projectConfig.Name, err)
}
if projectConfig.Infra.Path == "" {
projectConfig.Infra.Path = "infra"
}
for key, svc := range projectConfig.Services {
svc.Name = key
svc.Project = &projectConfig
svc.EventDispatcher = ext.NewEventDispatcher[ServiceLifecycleEventArgs]()
var err error
svc.Language, err = parseServiceLanguage(svc.Language)
if err != nil {
return nil, fmt.Errorf("parsing service %s: %w", svc.Name, err)
}
svc.Host, err = parseServiceHost(svc.Host)
if err != nil {
return nil, fmt.Errorf("parsing service %s: %w", svc.Name, err)
}
svc.Infra.Provider, err = provisioning.ParseProvider(svc.Infra.Provider)
if err != nil {
return nil, fmt.Errorf("parsing service %s: %w", svc.Name, err)
}
// TODO: Move parsing/validation requirements for service targets into their respective components.
// When working within container based applications users may be using external/pre-built images instead of source
// In this case it is valid to have not specified a language but would be required to specify a source image
if svc.Host == ContainerAppTarget && svc.Language == ServiceLanguageNone && svc.Image == "" {
return nil, fmt.Errorf("parsing service %s: must specify language or image", svc.Name)
}
}
return &projectConfig, nil
}
// Load hydrates the azure.yaml configuring into an viewable structure
// This does not evaluate any tooling
func Load(ctx context.Context, projectFilePath string) (*ProjectConfig, error) {
log.Printf("Reading project from file '%s'\n", projectFilePath)
bytes, err := os.ReadFile(projectFilePath)
if err != nil {
return nil, fmt.Errorf("reading project file: %w", err)
}
yaml := string(bytes)
projectConfig, err := Parse(ctx, yaml)
if err != nil {
return nil, fmt.Errorf("parsing project file: %w", err)
}
if projectConfig.Metadata != nil && projectConfig.Metadata.Template != "" {
template := strings.Split(projectConfig.Metadata.Template, "@")
if len(template) == 1 { // no version specifier, just the template ID
tracing.SetUsageAttributes(fields.StringHashed(fields.ProjectTemplateIdKey, template[0]))
} else if len(template) == 2 { // templateID@version
tracing.SetUsageAttributes(fields.StringHashed(fields.ProjectTemplateIdKey, template[0]))
tracing.SetUsageAttributes(fields.StringHashed(fields.ProjectTemplateVersionKey, template[1]))
} else { // unknown format, just send the whole thing
tracing.SetUsageAttributes(fields.StringHashed(fields.ProjectTemplateIdKey, projectConfig.Metadata.Template))
}
}
if projectConfig.Name != "" {
tracing.SetUsageAttributes(fields.StringHashed(fields.ProjectNameKey, projectConfig.Name))
}
if projectConfig.Services != nil {
hosts := make([]string, len(projectConfig.Services))
languages := make([]string, len(projectConfig.Services))
i := 0
for _, svcConfig := range projectConfig.Services {
hosts[i] = string(svcConfig.Host)
languages[i] = string(svcConfig.Language)
i++
}
slices.Sort(hosts)
slices.Sort(languages)
tracing.SetUsageAttributes(fields.ProjectServiceLanguagesKey.StringSlice(languages))
tracing.SetUsageAttributes(fields.ProjectServiceHostsKey.StringSlice(hosts))
}
projectConfig.Path = filepath.Dir(projectFilePath)
return projectConfig, nil
}
func LoadConfig(ctx context.Context, projectFilePath string) (config.Config, error) {
log.Printf("Reading project from file '%s'\n", projectFilePath)
bytes, err := os.ReadFile(projectFilePath)
if err != nil {
return nil, fmt.Errorf("reading project file: %w", err)
}
yamlContent := string(bytes)
rawConfig := map[string]any{}
if err := yaml.Unmarshal([]byte(yamlContent), &rawConfig); err != nil {
return nil, fmt.Errorf(
"unable to parse azure.yaml file. Check the format of the file, "+
"and also verify you have the latest version of the CLI: %w",
err,
)
}
return config.NewConfig(rawConfig), nil
}
func SaveConfig(ctx context.Context, config config.Config, projectFilePath string) error {
projectBytes, err := yaml.Marshal(config.Raw())
if err != nil {
return fmt.Errorf("marshalling project yaml: %w", err)
}
projectConfig, err := Parse(ctx, string(projectBytes))
if err != nil {
return fmt.Errorf("parsing project yaml: %w", err)
}
return Save(ctx, projectConfig, projectFilePath)
}
// Saves the current instance back to the azure.yaml file
func Save(ctx context.Context, projectConfig *ProjectConfig, projectFilePath string) error {
projectBytes, err := yaml.Marshal(projectConfig)
if err != nil {
return fmt.Errorf("marshalling project yaml: %w", err)
}
projectFileContents := bytes.NewBufferString(projectSchemaAnnotation + "\n\n")
_, err = projectFileContents.Write(projectBytes)
if err != nil {
return fmt.Errorf("preparing new project file contents: %w", err)
}
err = os.WriteFile(projectFilePath, projectFileContents.Bytes(), osutil.PermissionFile)
if err != nil {
return fmt.Errorf("saving project file: %w", err)
}
projectConfig.Path = projectFilePath
return nil
}