-
Notifications
You must be signed in to change notification settings - Fork 240
/
Copy pathconfig.go
432 lines (378 loc) · 11.5 KB
/
config.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
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
package devconfig
import (
"context"
"fmt"
"io"
"log/slog"
"maps"
"net/http"
"os"
"path/filepath"
"syscall"
"time"
"github.com/pkg/errors"
"github.com/samber/lo"
"github.com/samber/lo/mutable"
"go.jetify.com/devbox/internal/build"
"go.jetify.com/devbox/internal/cachehash"
"go.jetify.com/devbox/internal/devbox/shellcmd"
"go.jetify.com/devbox/internal/devconfig/configfile"
"go.jetify.com/devbox/internal/lock"
"go.jetify.com/devbox/internal/plugin"
)
// ErrNotFound occurs when [Open] or [Find] cannot find a devbox config file
// after searching a directory (and possibly its parent directories).
var ErrNotFound = errors.New("no devbox config file found")
// errIsDirectory indicates that a file can't be opened because it's a
// directory.
const errIsDirectory = syscall.EISDIR
// errNotDirectory indicates that a file can't be opened because the directory
// portion of its path is not a directory.
const errNotDirectory = syscall.ENOTDIR
// Config represents a base devbox.json as well as any included plugins it may have.
type Config struct {
Root configfile.ConfigFile
pluginData *plugin.PluginOnlyData // pointer by design, to allow for nil
included []*Config
}
const defaultInitHook = "echo 'Welcome to devbox!' > /dev/null"
func DefaultConfig() *Config {
cfg, err := loadBytes([]byte(fmt.Sprintf(`{
"$schema": "https://raw.githubusercontent.com/jetify-com/devbox/%s/.schema/devbox.schema.json",
"devbox_version": "%s",
"packages": [],
"shell": {
"init_hook": [
"%s"
],
"scripts": {
"test": [
"echo \"Error: no test specified\" && exit 1"
]
}
}
}
`,
lo.Ternary(build.IsDev, "main", build.Version),
defaultInitHook,
)))
if err != nil {
panic("default devbox.json is invalid: " + err.Error())
}
return cfg
}
func IsDefault(path string) bool {
cfg, err := readFromFile(path)
if err != nil {
return false
}
return cfg.Root.Equals(&DefaultConfig().Root)
}
// Open loads a Devbox config from a file or project directory. If path is a
// directory, Open looks for a well-known config name (such as devbox.json)
// within it. The error will be [ErrNotFound] if path is a valid directory
// without a config file.
//
// Open does not recursively search outside of path. See [Find] to load a config
// by walking up the directory tree.
func Open(path string) (*Config, error) {
start := time.Now()
slog.Debug("searching for config file (excluding parent directories)", "path", path)
cfg, err := open(path)
if err == nil {
slog.Debug("config file found", "path", cfg.Root.AbsRootPath, "dur", time.Since(start))
} else {
slog.Error("config file search error", "err", err.Error(), "dur", time.Since(start))
}
return cfg, err
}
func open(path string) (*Config, error) {
// First try the happy path by assuming that path is a directory
// containing a devbox.json.
cfg, err := searchDir(path)
if errors.Is(err, ErrNotFound) || errors.Is(err, errNotDirectory) {
// Try reading path directly as a config file.
slog.Debug("trying config file", "path", path)
cfg, err = readFromFile(path)
if errors.Is(err, errIsDirectory) {
return nil, ErrNotFound
}
}
return cfg, err
}
// Find is like [Open] except it recursively searches up the directory tree,
// starting in path. It returns [ErrNotFound] if path is a valid directory and
// neither it nor any of its parents contain a config file.
//
// Find stops searching as soon as it encounters a file with a well-known config
// name (such as devbox.json), even if that config fails to load.
func Find(path string) (*Config, error) {
start := time.Now()
slog.Debug("searching for config file (including parent directories)", "path", path)
cfg, err := open(path)
if errors.Is(err, ErrNotFound) {
cfg, err = searchParentDirs(path)
}
if err == nil {
slog.Debug("config file found", "path", cfg.Root.AbsRootPath, "dur", time.Since(start))
} else {
slog.Error("config file search error", "err", err.Error(), "dur", time.Since(start))
}
return cfg, err
}
// searchDir looks for a config file in dir. It does not search parent
// directories.
func searchDir(dir string) (*Config, error) {
try := []string{configfile.DefaultName}
for _, name := range try {
path := filepath.Join(dir, name)
slog.Debug("trying config file", "path", path)
cfg, err := readFromFile(path)
if err == nil {
return cfg, nil
}
// Keep searching for other valid config filenames.
if errors.Is(err, os.ErrNotExist) {
continue
}
// Ignore directories named devbox.json.
if errors.Is(err, errIsDirectory) {
continue
}
// Stop if we found a config but couldn't load it.
return cfg, err
}
return nil, ErrNotFound
}
// searchParentDirs recursively searches parent directories for a config. It
// starts with filepath.Dir(path) and does not search path itself.
func searchParentDirs(path string) (cfg *Config, err error) {
abs, err := filepath.Abs(path)
if err != nil {
return nil, fmt.Errorf("devconfig: search parent directories: %v", err)
}
err = ErrNotFound
for abs != "/" && errors.Is(err, ErrNotFound) {
abs = filepath.Dir(abs)
cfg, err = searchDir(abs)
}
return cfg, err
}
func readFromFile(path string) (*Config, error) {
b, err := os.ReadFile(path)
if err != nil {
return nil, err
}
config, err := loadBytes(b)
if err != nil {
return nil, err
}
config.Root.AbsRootPath, err = filepath.Abs(path)
return config, err
}
func LoadConfigFromURL(ctx context.Context, url string) (*Config, error) {
req, err := http.NewRequestWithContext(ctx, http.MethodGet, url, nil)
if err != nil {
return nil, errors.WithStack(err)
}
res, err := http.DefaultClient.Do(req)
if err != nil {
return nil, err
}
defer res.Body.Close()
data, err := io.ReadAll(res.Body)
if err != nil {
return nil, errors.WithStack(err)
}
return loadBytes(data)
}
func loadBytes(b []byte) (*Config, error) {
root, err := configfile.LoadBytes(b)
if err != nil {
return nil, err
}
return &Config{
Root: *root,
}, nil
}
func (c *Config) LoadRecursive(lockfile *lock.File) error {
return c.loadRecursive(lockfile, map[string]bool{}, "" /*cyclePath*/)
}
// loadRecursive loads all the included plugins and their included plugins, etc.
// seen should be a cloned map because loading plugins twice is allowed if they
// are in different paths.
func (c *Config) loadRecursive(
lockfile *lock.File,
seen map[string]bool,
cyclePath string,
) error {
included := make([]*Config, 0, len(c.Root.Include))
for _, includeRef := range c.Root.Include {
pluginConfig, err := plugin.LoadConfigFromInclude(
includeRef, lockfile, filepath.Dir(c.Root.AbsRootPath))
if err != nil {
return errors.WithStack(err)
}
newCyclePath := fmt.Sprintf("%s -> %s", cyclePath, includeRef)
if seen[pluginConfig.Source.Hash()] {
// Note that duplicate includes are allowed if they are in different paths
// e.g. 2 different plugins can include the same plugin.
// We do not allow a single plugin to include duplicates.
return errors.Errorf(
"circular or duplicate include detected:\n%s", newCyclePath)
}
seen[pluginConfig.Source.Hash()] = true
includable := createIncludableFromPluginConfig(pluginConfig)
if err := includable.loadRecursive(
lockfile, maps.Clone(seen), newCyclePath); err != nil {
return errors.WithStack(err)
}
included = append(included, includable)
}
builtIns, err := plugin.GetBuiltinsForPackages(
c.Root.TopLevelPackages(),
lockfile,
)
if err != nil {
return errors.WithStack(err)
}
for _, builtIn := range builtIns {
includable := &Config{
Root: builtIn.ConfigFile,
pluginData: &builtIn.PluginOnlyData,
}
newCyclePath := fmt.Sprintf("%s -> %s", cyclePath, builtIn.Source.LockfileKey())
if err := includable.loadRecursive(
lockfile, maps.Clone(seen), newCyclePath); err != nil {
return errors.WithStack(err)
}
included = append(included, includable)
}
c.included = included
return nil
}
func (c *Config) PackageMutator() *configfile.PackagesMutator {
return &c.Root.PackagesMutator
}
func (c *Config) IncludedPluginConfigs() []*plugin.Config {
configs := []*plugin.Config{}
for _, i := range c.included {
configs = append(configs, i.IncludedPluginConfigs()...)
}
if c.pluginData != nil {
configs = append(configs, &plugin.Config{
ConfigFile: c.Root,
PluginOnlyData: *c.pluginData,
})
}
return configs
}
// Returns all packages including those from included plugins.
// If includeRemovedTriggerPackages is true, then trigger packages that have
// been removed will also be returned. These are only used for built-ins
// (e.g. php) when the plugin creates a flake that is meant to replace the
// original package.
func (c *Config) Packages(
includeRemovedTriggerPackages bool,
) []configfile.Package {
packages := []configfile.Package{}
packagesToRemove := map[string]bool{}
for _, i := range c.included {
packages = append(packages, i.Packages(includeRemovedTriggerPackages)...)
if i.pluginData.RemoveTriggerPackage && !includeRemovedTriggerPackages {
packagesToRemove[i.pluginData.Source.LockfileKey()] = true
}
}
// Packages to remove in built ins only affect the devbox.json where they are defined.
// They should not remove packages that are part of other imports.
for _, pkg := range c.Root.TopLevelPackages() {
if !packagesToRemove[pkg.VersionedName()] {
packages = append(packages, pkg)
}
}
// Keep only the last occurrence of each package (by name).
mutable.Reverse(packages)
packages = lo.UniqBy(
packages,
func(p configfile.Package) string { return p.Name },
)
mutable.Reverse(packages)
return packages
}
func (c *Config) NixPkgsCommitHash() string {
return c.Root.NixPkgsCommitHash()
}
func (c *Config) Env() map[string]string {
env := map[string]string{}
for _, i := range c.included {
expandedEnvFromPlugin := OSExpandIfPossible(i.Env(), env)
maps.Copy(env, expandedEnvFromPlugin)
}
rootConfigEnv := OSExpandIfPossible(c.Root.Env, env)
maps.Copy(env, rootConfigEnv)
return env
}
func (c *Config) InitHook() *shellcmd.Commands {
commands := shellcmd.Commands{}
for _, i := range c.included {
commands.Cmds = append(commands.Cmds, i.InitHook().Cmds...)
}
commands.Cmds = append(commands.Cmds, c.Root.InitHook().Cmds...)
return &commands
}
func (c *Config) Scripts() configfile.Scripts {
scripts := configfile.Scripts{}
for _, i := range c.included {
maps.Copy(scripts, i.Scripts())
}
maps.Copy(scripts, c.Root.Scripts())
return scripts
}
func (c *Config) Hash() (string, error) {
data := []byte{}
for _, i := range c.included {
hash, err := i.Hash()
if err != nil {
return "", err
}
data = append(data, hash...)
}
hash, err := c.Root.Hash()
if err != nil {
return "", err
}
data = append(data, hash...)
return cachehash.Bytes(data), nil
}
func (c *Config) IsEnvsecEnabled() bool {
for _, i := range c.included {
if i.IsEnvsecEnabled() {
return true
}
}
return c.Root.IsEnvsecEnabled()
}
func createIncludableFromPluginConfig(pluginConfig *plugin.Config) *Config {
includable := &Config{
Root: pluginConfig.ConfigFile,
pluginData: &pluginConfig.PluginOnlyData,
}
if localPlugin, ok := pluginConfig.Source.(*plugin.LocalPlugin); ok {
includable.Root.AbsRootPath = localPlugin.Path()
}
return includable
}
func OSExpandIfPossible(env, existingEnv map[string]string) map[string]string {
mapping := func(value string) string {
// If the value is not set in existingEnv, return the value wrapped in ${...}
if existingEnv == nil || existingEnv[value] == "" {
return fmt.Sprintf("${%s}", value)
}
return existingEnv[value]
}
res := map[string]string{}
for k, v := range env {
res[k] = os.Expand(v, mapping)
}
return res
}