Repository navigation
Expand file tree
/
Copy pathenvFiles.go
More file actions
98 lines (88 loc) · 2.97 KB
/
Copy pathenvFiles.go
File metadata and controls
98 lines (88 loc) · 2.97 KB
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
package main
import (
"errors"
"fmt"
)
// EnvFile is a single entry in an `env:` list: the path to a file of
// KEY=VALUE lines, and whether Ahoy should stay quiet when it is absent.
//
// A file is required by default. Ahoy still runs when a required file is
// missing, because that has always been the behaviour and changing it would
// break existing configurations, but it now says so on stderr. Marking an
// entry optional is how you say the absence is deliberate, which is the same
// meaning `optional` already carries for `imports`.
type EnvFile struct {
Path string
Optional bool
}
// EnvFiles allows `env:` to be written as a single file, a list of files, or
// a list mixing plain paths with entries that mark a file optional:
//
// env: .env
//
// env:
// - .env.base
// - .env.local
//
// env:
// - .env
// - path: .env.local
// optional: true
type EnvFiles []EnvFile
var (
// errEnvFileNoPath covers an entry that decodes cleanly but names no
// file: `- optional: true` on its own, `- path: ""`, `- ""`, `- null`.
// An empty path would otherwise resolve to the config directory and be
// reported as a missing file called '', which tells nobody anything.
errEnvFileNoPath = errors.New("env entry has no file path. Give a path such as '.env', or a mapping with a 'path' key (e.g. `- path: .env.local` with `optional: true`)")
// errEnvFileShape covers an entry that is neither a path nor a mapping,
// such as a nested list. Reported in our own words because the decoder's
// version names the internal struct this type unmarshals through.
errEnvFileShape = errors.New("env entry must be a file path, or a mapping with a 'path' key (e.g. `- path: .env.local` with `optional: true`)")
)
// UnmarshalYAML accepts either a bare path or a mapping with `path` and
// `optional` keys, so both forms can appear in the same list.
func (f *EnvFile) UnmarshalYAML(unmarshal func(any) error) error {
var path string
if err := unmarshal(&path); err == nil {
f.Path = path
f.Optional = false
return nil
}
// A named type avoids recursing into this method.
var entry struct {
Path string `yaml:"path"`
Optional bool `yaml:"optional"`
}
if err := unmarshal(&entry); err != nil {
return errEnvFileShape
}
f.Path = entry.Path
f.Optional = entry.Optional
return nil
}
// UnmarshalYAML accepts a list of entries or a single entry, preserving the
// single-value shorthand that `env:` has always allowed.
func (e *EnvFiles) UnmarshalYAML(unmarshal func(any) error) error {
var multi []EnvFile
if err := unmarshal(&multi); err == nil {
// `env:` with no value decodes to a nil slice, which stays valid and
// simply means no env files.
for i, envFile := range multi {
if envFile.Path == "" {
return fmt.Errorf("env entry %d: %w", i+1, errEnvFileNoPath)
}
}
*e = multi
return nil
}
var single EnvFile
if err := unmarshal(&single); err != nil {
return err
}
if single.Path == "" {
return errEnvFileNoPath
}
*e = EnvFiles{single}
return nil
}