-
Notifications
You must be signed in to change notification settings - Fork 278
/
build.go
245 lines (213 loc) · 6.82 KB
/
build.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
package cueutil
import (
"bytes"
"errors"
"fmt"
"io"
"io/fs"
"os"
"path/filepath"
"strings"
"cuelang.org/go/cue"
"cuelang.org/go/cue/ast"
"cuelang.org/go/cue/build"
"cuelang.org/go/cue/cuecontext"
"cuelang.org/go/cue/load"
"cuelang.org/go/cue/parser"
"cuelang.org/go/cue/token"
"golang.org/x/text/encoding/unicode"
"golang.org/x/text/transform"
"encr.dev/pkg/eerror"
"encr.dev/pkg/errinsrc/srcerrors"
)
// LoadFromFS takes a given filesystem object and the app-relative path to the service's root package
// and loads the full configuration needed for that service.
func LoadFromFS(filesys fs.FS, serviceRelPath string, meta *Meta) (cue.Value, error) {
// Work out of a temporary directory
tmpPath, err := os.MkdirTemp("", "encr-cfg-")
if err != nil {
return cue.Value{}, eerror.Wrap(err, "config", "unable to create temporary directory", nil)
}
defer func() { _ = os.RemoveAll(tmpPath) }()
// Write the FS to the file system
err = writeFSToPath(filesys, tmpPath)
if err != nil {
return cue.Value{}, err
}
// Find all config files for the service
configFilesForService, err := allFilesUnder(filesys, serviceRelPath)
if err != nil {
return cue.Value{}, eerror.Wrap(err, "config", "unable to list all config files for service", map[string]any{"path": serviceRelPath})
}
// If there are no config files, return an empty value
if len(configFilesForService) == 0 {
return cue.Value{}, nil
}
// Tell CUE to load all the files
loaderCfg := &load.Config{
Dir: tmpPath,
Tools: true,
Tags: meta.ToTags(),
}
pkgs := load.Instances(configFilesForService, loaderCfg)
for _, pkg := range pkgs {
if pkg.Err != nil {
return cue.Value{}, srcerrors.UnableToLoadCUEInstances(pkg.Err, tmpPath)
}
// Non CUE files may be orphaned (JSON/YAML), so need to be parsed into the CUE AST and added to the package.
if err := addOrphanedFiles(pkg); err != nil {
return cue.Value{}, srcerrors.UnableToAddOrphanedCUEFiles(err, tmpPath)
}
}
// Build the CUE values
ctx := cuecontext.New()
values, err := ctx.BuildInstances(pkgs)
if err != nil {
return cue.Value{}, srcerrors.UnableToLoadCUEInstances(err, tmpPath)
}
if len(values) == 0 {
return cue.Value{}, eerror.New("config", "no values generated from config", nil)
}
// Unify all returned values into a single value
// Note; to get all errors in the CUE files, we want to wait until
// the validate output to check for errors
rtnValue := values[0]
for _, value := range values {
rtnValue = rtnValue.Unify(value)
}
// Validate the unified value is concrete
if err := rtnValue.Validate(cue.Concrete(true)); err != nil {
return cue.Value{}, srcerrors.CUEEvaluationFailed(err, tmpPath)
}
return rtnValue, nil
}
// allFilesUnder returns all files under the given path in the given filesystem.
func allFilesUnder(filesys fs.FS, path string) ([]string, error) {
var files []string
// Check if the path exists
// and if it doesn't that means there zero CUE files for this service
// this can happen when a Config struct has zero fields
if _, err := fs.Stat(filesys, path); err != nil {
if errors.Is(err, fs.ErrNotExist) {
return nil, nil
}
return nil, err
}
err := fs.WalkDir(filesys, path, func(path string, info fs.DirEntry, err error) error {
if err != nil {
return err
}
if !info.IsDir() {
files = append(files, path)
}
return nil
})
if err != nil {
return nil, err
}
return files, nil
}
// writeFSToPath writes the contents of the given filesystem to a temporary directory on the local filesystem.
func writeFSToPath(fsys fs.FS, targetPath string) error {
// Copy the files into the temporary directory
err := fs.WalkDir(fsys, ".", func(path string, info fs.DirEntry, err error) error {
if err != nil {
return eerror.Wrap(err, "config", "unable to walk VFS", nil)
}
// Convert the io/fs slash-based path to a filepath.
osPath := filepath.FromSlash(path)
if !info.IsDir() {
// Open the source file from our filesystem
srcFile, err := fsys.Open(path)
if err != nil {
return eerror.Wrap(err, "config", "unable to open src file", nil)
}
dstFile, err := os.OpenFile(filepath.Join(targetPath, osPath), os.O_CREATE|os.O_WRONLY, 0644)
if err != nil {
return eerror.Wrap(err, "config", "unable to open dst file", nil)
}
_, err = io.Copy(dstFile, srcFile)
if err != nil {
return eerror.Wrap(err, "config", "unable to copy file", nil)
}
} else {
if err := os.Mkdir(filepath.Join(targetPath, osPath), 0755); err != nil && !errors.Is(err, os.ErrExist) {
return eerror.Wrap(err, "config", "unable to make dir", nil)
}
}
return nil
})
if err != nil {
return eerror.Wrap(err, "config", "unable to write config files to temporary directory", map[string]any{"path": targetPath})
}
return nil
}
// addOrphanedFiles adds any orphaned files outside the package to the build instance. This could be CUE, YAML or JSON files
//
// The majority of the code in the function is taken directly from the CUE source code as the code is currently only accessible
// from internal paths - they are planning to move this out of the internal path so non-CUE code can directly call it
// as library functions. (Src: cue/internal/encoding/encoding.go : NewDecoder())
func addOrphanedFiles(i *build.Instance) (err error) {
for _, f := range i.OrphanedFiles {
var file ast.Node
rc, err := reader(f)
if err != nil {
return err
}
//goland:noinspection GoDeferInLoop
defer func() { _ = rc.Close() }()
t := unicode.BOMOverride(unicode.UTF8.NewDecoder())
r := transform.NewReader(rc, t)
switch f.Encoding {
case build.CUE:
file, err = parser.ParseFile(f.Filename, r, parser.ParseComments)
if err != nil {
return err
}
default:
return errors.New(fmt.Sprintf("unsupported encoding: %s", f.Encoding))
}
if err != nil {
return err
}
if err := i.AddSyntax(toFile(file)); err != nil {
return err
}
}
return nil
}
// toFile converts an ast.Node to a *ast.File. (from the CUE source code)
func toFile(n ast.Node) *ast.File {
switch x := n.(type) {
case nil:
return nil
case *ast.StructLit:
return &ast.File{Decls: x.Elts}
case ast.Expr:
ast.SetRelPos(x, token.NoSpace)
return &ast.File{Decls: []ast.Decl{&ast.EmbedDecl{Expr: x}}}
case *ast.File:
return x
default:
panic(fmt.Sprintf("Unsupported node type %T", x))
}
}
// reader returns a reader for the given file. (from the CUE source code)
func reader(f *build.File) (io.ReadCloser, error) {
switch s := f.Source.(type) {
case nil:
// Use the file name.
case string:
return io.NopCloser(strings.NewReader(s)), nil
case []byte:
return io.NopCloser(bytes.NewReader(s)), nil
case *bytes.Buffer:
// is io.Reader, but it needs to be readable repeatedly
if s != nil {
return io.NopCloser(bytes.NewReader(s.Bytes())), nil
}
default:
return nil, fmt.Errorf("invalid source type %T", f.Source)
}
return os.Open(f.Filename)
}