cleanup: move packages to top level, change vanity URL
Signed-off-by: Andrea Luzzardi <aluzzardi@gmail.com>
This commit is contained in:
78
compiler/build.go
Normal file
78
compiler/build.go
Normal file
@@ -0,0 +1,78 @@
|
||||
package compiler
|
||||
|
||||
import (
|
||||
"errors"
|
||||
"fmt"
|
||||
"io/fs"
|
||||
"path"
|
||||
"path/filepath"
|
||||
|
||||
cueerrors "cuelang.org/go/cue/errors"
|
||||
cueload "cuelang.org/go/cue/load"
|
||||
)
|
||||
|
||||
// Build a cue configuration tree from the files in fs.
|
||||
func Build(sources map[string]fs.FS, args ...string) (*Value, error) {
|
||||
c := DefaultCompiler
|
||||
|
||||
buildConfig := &cueload.Config{
|
||||
// The CUE overlay needs to be prefixed by a non-conflicting path with the
|
||||
// local filesystem, otherwise Cue will merge the Overlay with whatever Cue
|
||||
// files it finds locally.
|
||||
Dir: "/config",
|
||||
Overlay: map[string]cueload.Source{},
|
||||
}
|
||||
|
||||
// Map the source files into the overlay
|
||||
for mnt, f := range sources {
|
||||
f := f
|
||||
mnt := mnt
|
||||
err := fs.WalkDir(f, ".", func(p string, entry fs.DirEntry, err error) error {
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
if !entry.Type().IsRegular() {
|
||||
return nil
|
||||
}
|
||||
|
||||
if filepath.Ext(entry.Name()) != ".cue" {
|
||||
return nil
|
||||
}
|
||||
|
||||
contents, err := fs.ReadFile(f, p)
|
||||
if err != nil {
|
||||
return fmt.Errorf("%s: %w", p, err)
|
||||
}
|
||||
|
||||
overlayPath := path.Join(buildConfig.Dir, mnt, p)
|
||||
buildConfig.Overlay[overlayPath] = cueload.FromBytes(contents)
|
||||
return nil
|
||||
})
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
}
|
||||
instances := cueload.Instances(args, buildConfig)
|
||||
if len(instances) != 1 {
|
||||
return nil, errors.New("only one package is supported at a time")
|
||||
}
|
||||
for _, value := range instances {
|
||||
if value.Err != nil {
|
||||
return nil, value.Err
|
||||
}
|
||||
}
|
||||
v, err := c.Context.BuildInstances(instances)
|
||||
if err != nil {
|
||||
return nil, errors.New(cueerrors.Details(err, &cueerrors.Config{}))
|
||||
}
|
||||
for _, value := range v {
|
||||
if value.Err() != nil {
|
||||
return nil, value.Err()
|
||||
}
|
||||
}
|
||||
if len(v) != 1 {
|
||||
return nil, errors.New("internal: wrong number of values")
|
||||
}
|
||||
return Wrap(v[0]), nil
|
||||
}
|
Reference in New Issue
Block a user