Skip to content

Commit eadbea7

Browse files
committed
cue/load: allow file overlays
This will avoid the need to write intermediate files if these are not needed in the final result, such as in generation pipelines. Change-Id: I43d417ebcfa5c9b24704a441bf05dc5a79d6e4dd Reviewed-on: https://cue-review.googlesource.com/c/cue/+/2369 Reviewed-by: Marcel van Lohuizen <[email protected]>
1 parent 70108ad commit eadbea7

File tree

11 files changed

+423
-143
lines changed

11 files changed

+423
-143
lines changed

cue/load/config.go

Lines changed: 34 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ import (
2121

2222
"cuelang.org/go/cue/build"
2323
"cuelang.org/go/cue/errors"
24+
"cuelang.org/go/cue/token"
2425
)
2526

2627
const (
@@ -113,6 +114,16 @@ type Config struct {
113114
// This is mostly used for bootstrapping.
114115
StdRoot string
115116

117+
// Overlay provides a mapping of absolute file paths to file contents.
118+
// If the file with the given path already exists, the parser will use the
119+
// alternative file contents provided by the map.
120+
//
121+
// Overlays provide incomplete support for when a given file doesn't
122+
// already exist on disk. See the package doc above for more details.
123+
//
124+
// If the value must be of type string, []byte, io.Reader, or *ast.File.
125+
Overlay map[string]Source
126+
116127
fileSystem
117128
}
118129

@@ -122,10 +133,19 @@ func (c Config) newInstance(path string) *build.Instance {
122133
return i
123134
}
124135

125-
func (c Config) newErrInstance(m *match, path string, err errors.Error) *build.Instance {
136+
func (c Config) newErrInstance(m *match, path string, err error) *build.Instance {
126137
i := c.Context.NewInstance(path, nil)
127138
i.DisplayPath = path
128-
i.ReportError(err)
139+
switch x := err.(type) {
140+
case errors.Error:
141+
i.ReportError(x)
142+
case errors.List:
143+
for _, e := range x {
144+
i.ReportError(e)
145+
}
146+
default:
147+
i.ReportError(errors.Wrapf(err, token.NoPos, "instance"))
148+
}
129149
return i
130150
}
131151

@@ -144,11 +164,17 @@ func (c Config) complete() (cfg *Config, err error) {
144164
}
145165
}
146166

167+
// TODO: we could populate this already with absolute file paths,
168+
// but relative paths cannot be added. Consider what is reasonable.
169+
if err := c.fileSystem.init(&c); err != nil {
170+
return nil, err
171+
}
172+
147173
// TODO: determine root on a package basis. Maybe we even need a
148174
// pkgname.cue.mod
149175
// Look to see if there is a cue.mod.
150176
if c.modRoot == "" {
151-
abs, err := findRoot(c.Dir)
177+
abs, err := c.findRoot(c.Dir)
152178
if err != nil {
153179
// Not using modules: only consider the current directory.
154180
c.modRoot = c.Dir
@@ -165,20 +191,21 @@ func (c Config) complete() (cfg *Config, err error) {
165191

166192
if c.cache == "" {
167193
c.cache = filepath.Join(home(), defaultDir)
168-
// os.MkdirAll(c.Cache, 0755) // TODO: tools task
169194
}
170195

171196
return &c, nil
172197
}
173198

174-
func findRoot(dir string) (string, error) {
199+
func (c Config) findRoot(dir string) (string, error) {
200+
fs := &c.fileSystem
201+
175202
absDir, err := filepath.Abs(dir)
176203
if err != nil {
177204
return "", err
178205
}
179206
abs := absDir
180207
for {
181-
info, err := os.Stat(filepath.Join(abs, modFile))
208+
info, err := fs.stat(filepath.Join(abs, modFile))
182209
if err == nil && !info.IsDir() {
183210
return abs, nil
184211
}
@@ -190,7 +217,7 @@ func findRoot(dir string) (string, error) {
190217
}
191218
abs = absDir
192219
for {
193-
info, err := os.Stat(filepath.Join(abs, pkgDir))
220+
info, err := fs.stat(filepath.Join(abs, pkgDir))
194221
if err == nil && info.IsDir() {
195222
return abs, nil
196223
}

0 commit comments

Comments
 (0)