compiler cleanup

Now using the same pattern as Go's http package.

- the `compiler.Compiler` struct can be used directly (and tests to do
  to avoid messing with the global version)
- `compiler.DefaultCompiler` contains a public default Compiler instance
- `compiler` exposes proxy functions (e.g. Compile) back to the
  DefaultCompiler

Signed-off-by: Andrea Luzzardi <aluzzardi@gmail.com>
This commit is contained in:
Andrea Luzzardi 2021-02-16 18:31:03 -08:00
parent eb0c0a9375
commit 74e084944e
14 changed files with 195 additions and 192 deletions

View File

@ -11,12 +11,12 @@ import (
"github.com/pkg/errors" "github.com/pkg/errors"
"github.com/rs/zerolog/log" "github.com/rs/zerolog/log"
"dagger.cloud/go/dagger/cc" "dagger.cloud/go/dagger/compiler"
"dagger.cloud/go/stdlib" "dagger.cloud/go/stdlib"
) )
// Build a cue configuration tree from the files in fs. // Build a cue configuration tree from the files in fs.
func CueBuild(ctx context.Context, fs FS, args ...string) (*cc.Value, error) { func CueBuild(ctx context.Context, fs FS, args ...string) (*compiler.Value, error) {
var ( var (
err error err error
lg = log.Ctx(ctx) lg = log.Ctx(ctx)
@ -59,9 +59,9 @@ func CueBuild(ctx context.Context, fs FS, args ...string) (*cc.Value, error) {
if len(instances) != 1 { if len(instances) != 1 {
return nil, errors.New("only one package is supported at a time") return nil, errors.New("only one package is supported at a time")
} }
inst, err := cc.Cue().Build(instances[0]) inst, err := compiler.Cue().Build(instances[0])
if err != nil { if err != nil {
return nil, errors.New(cueerrors.Details(err, &cueerrors.Config{})) return nil, errors.New(cueerrors.Details(err, &cueerrors.Config{}))
} }
return cc.Wrap(inst.Value(), inst), nil return compiler.Wrap(inst.Value(), inst), nil
} }

View File

@ -1,37 +0,0 @@
package cc
import (
"cuelang.org/go/cue"
cueerrors "cuelang.org/go/cue/errors"
"github.com/pkg/errors"
)
var (
// Shared global compiler
cc = &Compiler{}
)
func Compile(name string, src interface{}) (*Value, error) {
return cc.Compile(name, src)
}
func EmptyStruct() (*Value, error) {
return cc.EmptyStruct()
}
// FIXME can be refactored away now?
func Wrap(v cue.Value, inst *cue.Instance) *Value {
return cc.Wrap(v, inst)
}
func Cue() *cue.Runtime {
return cc.Cue()
}
func Err(err error) error {
if err == nil {
return nil
}
return errors.New(cueerrors.Details(err, &cueerrors.Config{}))
}

View File

@ -1,56 +0,0 @@
package cc
import (
"sync"
"cuelang.org/go/cue"
)
// Polyfill for a cue runtime
// (we call it compiler to avoid confusion with dagger runtime)
// Use this instead of cue.Runtime
type Compiler struct {
l sync.RWMutex
cue.Runtime
}
func (cc *Compiler) lock() {
cc.l.Lock()
}
func (cc *Compiler) unlock() {
cc.l.Unlock()
}
func (cc *Compiler) rlock() {
cc.l.RLock()
}
func (cc *Compiler) runlock() {
cc.l.RUnlock()
}
func (cc *Compiler) Cue() *cue.Runtime {
return &(cc.Runtime)
}
// Compile an empty struct
func (cc *Compiler) EmptyStruct() (*Value, error) {
return cc.Compile("", "")
}
func (cc *Compiler) Compile(name string, src interface{}) (*Value, error) {
cc.lock()
defer cc.unlock()
inst, err := cc.Cue().Compile(name, src)
if err != nil {
// FIXME: cleaner way to unwrap cue error details?
return nil, Err(err)
}
return cc.Wrap(inst.Value(), inst), nil
}
func (cc *Compiler) Wrap(v cue.Value, inst *cue.Instance) *Value {
return wrapValue(v, inst)
}

View File

@ -24,7 +24,7 @@ import (
"github.com/containerd/console" "github.com/containerd/console"
"github.com/moby/buildkit/util/progress/progressui" "github.com/moby/buildkit/util/progress/progressui"
"dagger.cloud/go/dagger/cc" "dagger.cloud/go/dagger/compiler"
) )
const ( const (
@ -52,8 +52,8 @@ func NewClient(ctx context.Context, host string) (*Client, error) {
}, nil }, nil
} }
// FIXME: return completed *Env, instead of *cc.Value // FIXME: return completed *Env, instead of *compiler.Value
func (c *Client) Compute(ctx context.Context, env *Env) (*cc.Value, error) { func (c *Client) Compute(ctx context.Context, env *Env) (*compiler.Value, error) {
lg := log.Ctx(ctx) lg := log.Ctx(ctx)
eg, gctx := errgroup.WithContext(ctx) eg, gctx := errgroup.WithContext(ctx)
@ -77,7 +77,7 @@ func (c *Client) Compute(ctx context.Context, env *Env) (*cc.Value, error) {
// Spawn output retriever // Spawn output retriever
var ( var (
out *cc.Value out *compiler.Value
err error err error
) )
eg.Go(func() error { eg.Go(func() error {
@ -86,7 +86,7 @@ func (c *Client) Compute(ctx context.Context, env *Env) (*cc.Value, error) {
return err return err
}) })
return out, cc.Err(eg.Wait()) return out, compiler.Err(eg.Wait())
} }
func (c *Client) buildfn(ctx context.Context, env *Env, ch chan *bk.SolveStatus, w io.WriteCloser) error { func (c *Client) buildfn(ctx context.Context, env *Env, ch chan *bk.SolveStatus, w io.WriteCloser) error {
@ -157,11 +157,11 @@ func (c *Client) buildfn(ctx context.Context, env *Env, ch chan *bk.SolveStatus,
} }
// Read tar export stream from buildkit Build(), and extract cue output // Read tar export stream from buildkit Build(), and extract cue output
func (c *Client) outputfn(ctx context.Context, r io.Reader) (*cc.Value, error) { func (c *Client) outputfn(ctx context.Context, r io.Reader) (*compiler.Value, error) {
lg := log.Ctx(ctx) lg := log.Ctx(ctx)
// FIXME: merge this into env output. // FIXME: merge this into env output.
out, err := cc.EmptyStruct() out, err := compiler.EmptyStruct()
if err != nil { if err != nil {
return nil, err return nil, err
} }
@ -187,7 +187,7 @@ func (c *Client) outputfn(ctx context.Context, r io.Reader) (*cc.Value, error) {
} }
lg.Debug().Msg("outputfn: compiling & merging") lg.Debug().Msg("outputfn: compiling & merging")
v, err := cc.Compile(h.Name, tr) v, err := compiler.Compile(h.Name, tr)
if err != nil { if err != nil {
return nil, err return nil, err
} }

View File

@ -0,0 +1,91 @@
package compiler
import (
"errors"
"sync"
"cuelang.org/go/cue"
cueerrors "cuelang.org/go/cue/errors"
)
var (
// DefaultCompiler is the default Compiler and is used by Compile
DefaultCompiler = &Compiler{}
)
func Compile(name string, src interface{}) (*Value, error) {
return DefaultCompiler.Compile(name, src)
}
func EmptyStruct() (*Value, error) {
return DefaultCompiler.EmptyStruct()
}
// FIXME can be refactored away now?
func Wrap(v cue.Value, inst *cue.Instance) *Value {
return DefaultCompiler.Wrap(v, inst)
}
func Cue() *cue.Runtime {
return DefaultCompiler.Cue()
}
func Err(err error) error {
return DefaultCompiler.Err(err)
}
// Polyfill for a cue runtime
// (we call it compiler to avoid confusion with dagger runtime)
// Use this instead of cue.Runtime
type Compiler struct {
l sync.RWMutex
cue.Runtime
}
func (c *Compiler) lock() {
c.l.Lock()
}
func (c *Compiler) unlock() {
c.l.Unlock()
}
func (c *Compiler) rlock() {
c.l.RLock()
}
func (c *Compiler) runlock() {
c.l.RUnlock()
}
func (c *Compiler) Cue() *cue.Runtime {
return &(c.Runtime)
}
// Compile an empty struct
func (c *Compiler) EmptyStruct() (*Value, error) {
return c.Compile("", "")
}
func (c *Compiler) Compile(name string, src interface{}) (*Value, error) {
c.lock()
defer c.unlock()
inst, err := c.Cue().Compile(name, src)
if err != nil {
// FIXME: cleaner way to unwrap cue error details?
return nil, Err(err)
}
return c.Wrap(inst.Value(), inst), nil
}
func (c *Compiler) Wrap(v cue.Value, inst *cue.Instance) *Value {
return wrapValue(v, inst, c)
}
func (c *Compiler) Err(err error) error {
if err == nil {
return nil
}
return errors.New(cueerrors.Details(err, &cueerrors.Config{}))
}

View File

@ -1,4 +1,4 @@
package cc package compiler
import ( import (
"testing" "testing"
@ -6,7 +6,8 @@ import (
// Test that a non-existing field is detected correctly // Test that a non-existing field is detected correctly
func TestFieldNotExist(t *testing.T) { func TestFieldNotExist(t *testing.T) {
root, err := Compile("test.cue", `foo: "bar"`) c := &Compiler{}
root, err := c.Compile("test.cue", `foo: "bar"`)
if err != nil { if err != nil {
t.Fatal(err) t.Fatal(err)
} }
@ -22,7 +23,8 @@ func TestFieldNotExist(t *testing.T) {
// Test that a non-existing definition is detected correctly // Test that a non-existing definition is detected correctly
func TestDefNotExist(t *testing.T) { func TestDefNotExist(t *testing.T) {
root, err := Compile("test.cue", `foo: #bla: "bar"`) c := &Compiler{}
root, err := c.Compile("test.cue", `foo: #bla: "bar"`)
if err != nil { if err != nil {
t.Fatal(err) t.Fatal(err)
} }
@ -37,14 +39,16 @@ func TestDefNotExist(t *testing.T) {
} }
func TestSimple(t *testing.T) { func TestSimple(t *testing.T) {
_, err := EmptyStruct() c := &Compiler{}
_, err := c.EmptyStruct()
if err != nil { if err != nil {
t.Fatal(err) t.Fatal(err)
} }
} }
func TestJSON(t *testing.T) { func TestJSON(t *testing.T) {
v, err := Compile("", `foo: hello: "world"`) c := &Compiler{}
v, err := c.Compile("", `foo: hello: "world"`)
if err != nil { if err != nil {
t.Fatal(err) t.Fatal(err)
} }

View File

@ -1,4 +1,4 @@
package cc package compiler
import ( import (
"fmt" "fmt"
@ -6,7 +6,6 @@ import (
"cuelang.org/go/cue" "cuelang.org/go/cue"
cuejson "cuelang.org/go/encoding/json" cuejson "cuelang.org/go/encoding/json"
"github.com/KromDaniel/jonson" "github.com/KromDaniel/jonson"
"github.com/pkg/errors"
) )
type JSON []byte type JSON []byte
@ -20,7 +19,7 @@ func (s JSON) Get(path ...string) ([]byte, error) {
) )
root, err := jonson.Parse(s) root, err := jonson.Parse(s)
if err != nil { if err != nil {
return nil, errors.Wrap(err, "parse root json") return nil, fmt.Errorf("parse root json: %w", err)
} }
pointer := root pointer := root
for _, key := range path { for _, key := range path {
@ -40,7 +39,7 @@ func (s JSON) Unset(path ...string) (JSON, error) {
) )
root, err := jonson.Parse(s) root, err := jonson.Parse(s)
if err != nil { if err != nil {
return nil, errors.Wrap(err, "unset: parse root json") return nil, fmt.Errorf("unset: parse root json: %w", err)
} }
var ( var (
pointer = root pointer = root
@ -71,11 +70,11 @@ func (s JSON) Set(valueJSON []byte, path ...string) (JSON, error) {
) )
root, err := jonson.Parse(s) root, err := jonson.Parse(s)
if err != nil { if err != nil {
return nil, errors.Wrap(err, "parse root json") return nil, fmt.Errorf("parse root json: %w", err)
} }
value, err = jonson.Parse(valueJSON) value, err = jonson.Parse(valueJSON)
if err != nil { if err != nil {
return nil, errors.Wrapf(err, "SetJSON: parse value json: |%s|", valueJSON) return nil, fmt.Errorf("SetJSON: parse value json: |%s|: %w", valueJSON, err)
} }
var ( var (
pointer = root pointer = root

View File

@ -1,4 +1,4 @@
package cc package compiler
import ( import (
"cuelang.org/go/cue" "cuelang.org/go/cue"

View File

@ -1,4 +1,4 @@
package cc package compiler
import ( import (
"cuelang.org/go/cue" "cuelang.org/go/cue"
@ -9,6 +9,7 @@ import (
// Use instead of cue.Value and cue.Instance // Use instead of cue.Value and cue.Instance
type Value struct { type Value struct {
val cue.Value val cue.Value
cc *Compiler
inst *cue.Instance inst *cue.Instance
} }
@ -17,20 +18,21 @@ func (v *Value) CueInst() *cue.Instance {
} }
func (v *Value) Wrap(v2 cue.Value) *Value { func (v *Value) Wrap(v2 cue.Value) *Value {
return wrapValue(v2, v.inst) return wrapValue(v2, v.inst, v.cc)
} }
func wrapValue(v cue.Value, inst *cue.Instance) *Value { func wrapValue(v cue.Value, inst *cue.Instance, cc *Compiler) *Value {
return &Value{ return &Value{
val: v, val: v,
cc: cc,
inst: inst, inst: inst,
} }
} }
// Fill the value in-place, unlike Merge which returns a copy. // Fill the value in-place, unlike Merge which returns a copy.
func (v *Value) Fill(x interface{}) error { func (v *Value) Fill(x interface{}) error {
cc.lock() v.cc.lock()
defer cc.unlock() defer v.cc.unlock()
// If calling Fill() with a Value, we want to use the underlying // If calling Fill() with a Value, we want to use the underlying
// cue.Value to fill. // cue.Value to fill.
@ -44,8 +46,8 @@ func (v *Value) Fill(x interface{}) error {
// LookupPath is a concurrency safe wrapper around cue.Value.LookupPath // LookupPath is a concurrency safe wrapper around cue.Value.LookupPath
func (v *Value) LookupPath(p cue.Path) *Value { func (v *Value) LookupPath(p cue.Path) *Value {
cc.rlock() v.cc.rlock()
defer cc.runlock() defer v.cc.runlock()
return v.Wrap(v.val.LookupPath(p)) return v.Wrap(v.val.LookupPath(p))
} }
@ -147,9 +149,9 @@ func (v *Value) Merge(x interface{}, path ...string) (*Value, error) {
x = xval.val x = xval.val
} }
cc.lock() v.cc.lock()
result := v.Wrap(v.val.Fill(x, path...)) result := v.Wrap(v.val.Fill(x, path...))
cc.unlock() v.cc.unlock()
return result, result.Validate() return result, result.Validate()
} }
@ -220,8 +222,8 @@ func (v *Value) Validate() error {
// Return cue source for this value // Return cue source for this value
func (v *Value) Source() ([]byte, error) { func (v *Value) Source() ([]byte, error) {
cc.rlock() v.cc.rlock()
defer cc.runlock() defer v.cc.runlock()
return cueformat.Node(v.val.Eval().Syntax()) return cueformat.Node(v.val.Eval().Syntax())
} }

View File

@ -3,7 +3,7 @@ package dagger
import ( import (
"testing" "testing"
"dagger.cloud/go/dagger/cc" "dagger.cloud/go/dagger/compiler"
) )
func TestLocalDirs(t *testing.T) { func TestLocalDirs(t *testing.T) {
@ -38,14 +38,14 @@ func mkEnv(t *testing.T, updater, input string) *Env {
if err != nil { if err != nil {
t.Fatal(err) t.Fatal(err)
} }
u, err := cc.Compile("updater.cue", updater) u, err := compiler.Compile("updater.cue", updater)
if err != nil { if err != nil {
t.Fatal(err) t.Fatal(err)
} }
if err := env.SetUpdater(u); err != nil { if err := env.SetUpdater(u); err != nil {
t.Fatal(err) t.Fatal(err)
} }
i, err := cc.Compile("input.cue", input) i, err := compiler.Compile("input.cue", input)
if err != nil { if err != nil {
t.Fatal(err) t.Fatal(err)
} }

View File

@ -8,7 +8,7 @@ import (
"github.com/pkg/errors" "github.com/pkg/errors"
"github.com/rs/zerolog/log" "github.com/rs/zerolog/log"
"dagger.cloud/go/dagger/cc" "dagger.cloud/go/dagger/compiler"
) )
type Env struct { type Env struct {
@ -18,30 +18,30 @@ type Env struct {
// FIXME: simplify Env by making it single layer? Each layer is one env. // FIXME: simplify Env by making it single layer? Each layer is one env.
// How to update the base configuration // How to update the base configuration
updater *cc.Value updater *compiler.Value
// Layer 1: base configuration // Layer 1: base configuration
base *cc.Value base *compiler.Value
// Layer 2: user inputs // Layer 2: user inputs
input *cc.Value input *compiler.Value
// Layer 3: computed values // Layer 3: computed values
output *cc.Value output *compiler.Value
// All layers merged together: base + input + output // All layers merged together: base + input + output
state *cc.Value state *compiler.Value
} }
func (env *Env) Updater() *cc.Value { func (env *Env) Updater() *compiler.Value {
return env.updater return env.updater
} }
// Set the updater script for this environment. // Set the updater script for this environment.
func (env *Env) SetUpdater(v *cc.Value) error { func (env *Env) SetUpdater(v *compiler.Value) error {
if v == nil { if v == nil {
var err error var err error
v, err = cc.Compile("", "[]") v, err = compiler.Compile("", "[]")
if err != nil { if err != nil {
return err return err
} }
@ -51,7 +51,7 @@ func (env *Env) SetUpdater(v *cc.Value) error {
} }
func NewEnv() (*Env, error) { func NewEnv() (*Env, error) {
empty, err := cc.EmptyStruct() empty, err := compiler.EmptyStruct()
if err != nil { if err != nil {
return nil, err return nil, err
} }
@ -67,18 +67,18 @@ func NewEnv() (*Env, error) {
return env, nil return env, nil
} }
func (env *Env) State() *cc.Value { func (env *Env) State() *compiler.Value {
return env.state return env.state
} }
func (env *Env) Input() *cc.Value { func (env *Env) Input() *compiler.Value {
return env.input return env.input
} }
func (env *Env) SetInput(i *cc.Value) error { func (env *Env) SetInput(i *compiler.Value) error {
if i == nil { if i == nil {
var err error var err error
i, err = cc.EmptyStruct() i, err = compiler.EmptyStruct()
if err != nil { if err != nil {
return err return err
} }
@ -112,11 +112,11 @@ func (env *Env) Update(ctx context.Context, s Solver) error {
) )
} }
func (env *Env) Base() *cc.Value { func (env *Env) Base() *compiler.Value {
return env.base return env.base
} }
func (env *Env) Output() *cc.Value { func (env *Env) Output() *compiler.Value {
return env.output return env.output
} }
@ -126,9 +126,9 @@ func (env *Env) Output() *cc.Value {
// by user-specified scripts. // by user-specified scripts.
func (env *Env) LocalDirs() map[string]string { func (env *Env) LocalDirs() map[string]string {
dirs := map[string]string{} dirs := map[string]string{}
localdirs := func(code ...*cc.Value) { localdirs := func(code ...*compiler.Value) {
Analyze( Analyze(
func(op *cc.Value) error { func(op *compiler.Value) error {
do, err := op.Get("do").String() do, err := op.Get("do").String()
if err != nil { if err != nil {
return err return err
@ -148,7 +148,7 @@ func (env *Env) LocalDirs() map[string]string {
} }
// 1. Scan the environment state // 1. Scan the environment state
env.State().Walk( env.State().Walk(
func(v *cc.Value) bool { func(v *compiler.Value) bool {
compute := v.Get("#dagger.compute") compute := v.Get("#dagger.compute")
if !compute.Exists() { if !compute.Exists() {
// No compute script // No compute script
@ -164,14 +164,14 @@ func (env *Env) LocalDirs() map[string]string {
return dirs return dirs
} }
// FIXME: this is just a 3-way merge. Add var args to cc.Value.Merge. // FIXME: this is just a 3-way merge. Add var args to compiler.Value.Merge.
func (env *Env) set(base, input, output *cc.Value) (err error) { func (env *Env) set(base, input, output *compiler.Value) (err error) {
// FIXME: make this cleaner in *cc.Value by keeping intermediary instances // FIXME: make this cleaner in *compiler.Value by keeping intermediary instances
// FIXME: state.CueInst() must return an instance with the same // FIXME: state.CueInst() must return an instance with the same
// contents as state.v, for the purposes of cueflow. // contents as state.v, for the purposes of cueflow.
// That is not currently how *cc.Value works, so we prepare the cue // That is not currently how *compiler.Value works, so we prepare the cue
// instance manually. // instance manually.
// --> refactor the cc.Value API to do this for us. // --> refactor the compiler.Value API to do this for us.
stateInst := env.state.CueInst() stateInst := env.state.CueInst()
stateInst, err = stateInst.Fill(base.Cue()) stateInst, err = stateInst.Fill(base.Cue())
@ -187,7 +187,7 @@ func (env *Env) set(base, input, output *cc.Value) (err error) {
return errors.Wrap(err, "merge output with base & input") return errors.Wrap(err, "merge output with base & input")
} }
state := cc.Wrap(stateInst.Value(), stateInst) state := compiler.Wrap(stateInst.Value(), stateInst)
// commit // commit
env.base = base env.base = base
@ -201,9 +201,9 @@ func (env *Env) set(base, input, output *cc.Value) (err error) {
// (Use with FS.Change) // (Use with FS.Change)
func (env *Env) Export(fs FS) (FS, error) { func (env *Env) Export(fs FS) (FS, error) {
// FIXME: we serialize as JSON to guarantee a self-contained file. // FIXME: we serialize as JSON to guarantee a self-contained file.
// cc.Value.Save() leaks imports, so requires a shared cue.mod with // compiler.Value.Save() leaks imports, so requires a shared cue.mod with
// client which is undesirable. // client which is undesirable.
// Once cc.Value.Save() resolves non-builtin imports with a tree shake, // Once compiler.Value.Save() resolves non-builtin imports with a tree shake,
// we can use it here. // we can use it here.
// FIXME: Exporting base/input/output separately causes merge errors. // FIXME: Exporting base/input/output separately causes merge errors.
@ -237,11 +237,11 @@ func (env *Env) Compute(ctx context.Context, s Solver) error {
flowInst := env.state.CueInst() flowInst := env.state.CueInst()
lg. lg.
Debug(). Debug().
Str("value", cc.Wrap(flowInst.Value(), flowInst).JSON().String()). Str("value", compiler.Wrap(flowInst.Value(), flowInst).JSON().String()).
Msg("walking") Msg("walking")
// Initialize empty output // Initialize empty output
output, err := cc.EmptyStruct() output, err := compiler.EmptyStruct()
if err != nil { if err != nil {
return err return err
} }
@ -279,7 +279,7 @@ func (env *Env) Compute(ctx context.Context, s Solver) error {
} }
// Cueflow match func // Cueflow match func
flowMatchFn := func(flowVal cue.Value) (cueflow.Runner, error) { flowMatchFn := func(flowVal cue.Value) (cueflow.Runner, error) {
v := cc.Wrap(flowVal, flowInst) v := compiler.Wrap(flowVal, flowInst)
compute := v.Get("#dagger.compute") compute := v.Get("#dagger.compute")
if !compute.Exists() { if !compute.Exists() {
// No compute script // No compute script
@ -303,7 +303,7 @@ func (env *Env) Compute(ctx context.Context, s Solver) error {
Str("dependency", dep.Path().String()). Str("dependency", dep.Path().String()).
Msg("dependency detected") Msg("dependency detected")
} }
v := cc.Wrap(t.Value(), flowInst) v := compiler.Wrap(t.Value(), flowInst)
p := NewPipeline(s, NewFillable(t)) p := NewPipeline(s, NewFillable(t))
return p.Do(ctx, v) return p.Do(ctx, v)
}), nil }), nil

View File

@ -11,7 +11,7 @@ import (
"github.com/pkg/errors" "github.com/pkg/errors"
fstypes "github.com/tonistiigi/fsutil/types" fstypes "github.com/tonistiigi/fsutil/types"
"dagger.cloud/go/dagger/cc" "dagger.cloud/go/dagger/compiler"
) )
type Stat struct { type Stat struct {
@ -27,7 +27,7 @@ type FS struct {
s Solver s Solver
} }
func (fs FS) WriteValueJSON(filename string, v *cc.Value) FS { func (fs FS) WriteValueJSON(filename string, v *compiler.Value) FS {
return fs.Change(func(st llb.State) llb.State { return fs.Change(func(st llb.State) llb.State {
return st.File( return st.File(
llb.Mkfile(filename, 0600, v.JSON()), llb.Mkfile(filename, 0600, v.JSON()),
@ -35,7 +35,7 @@ func (fs FS) WriteValueJSON(filename string, v *cc.Value) FS {
}) })
} }
func (fs FS) WriteValueCUE(filename string, v *cc.Value) (FS, error) { func (fs FS) WriteValueCUE(filename string, v *compiler.Value) (FS, error) {
src, err := v.Source() src, err := v.Source()
if err != nil { if err != nil {
return fs, err return fs, err

View File

@ -9,16 +9,16 @@ import (
"cuelang.org/go/cue" "cuelang.org/go/cue"
"github.com/spf13/pflag" "github.com/spf13/pflag"
"dagger.cloud/go/dagger/cc" "dagger.cloud/go/dagger/compiler"
) )
// A mutable cue value with an API suitable for user inputs, // A mutable cue value with an API suitable for user inputs,
// such as command-line flag parsing. // such as command-line flag parsing.
type InputValue struct { type InputValue struct {
root *cc.Value root *compiler.Value
} }
func (iv *InputValue) Value() *cc.Value { func (iv *InputValue) Value() *compiler.Value {
return iv.root return iv.root
} }
@ -28,7 +28,7 @@ func (iv *InputValue) String() string {
} }
func NewInputValue(base interface{}) (*InputValue, error) { func NewInputValue(base interface{}) (*InputValue, error) {
root, err := cc.Compile("base", base) root, err := compiler.Compile("base", base)
if err != nil { if err != nil {
return nil, err return nil, err
} }
@ -101,7 +101,7 @@ func (f dirFlag) Set(s string) error {
if err != nil { if err != nil {
return nil, err return nil, err
} }
return cc.Compile("", fmt.Sprintf( return compiler.Compile("", fmt.Sprintf(
`#dagger: compute: [{do:"local",dir:"%s", include:%s}]`, `#dagger: compute: [{do:"local",dir:"%s", include:%s}]`,
s, s,
include, include,
@ -139,7 +139,7 @@ func (f gitFlag) Set(s string) error {
u.Fragment = "" u.Fragment = ""
remote := u.String() remote := u.String()
return cc.Compile("", fmt.Sprintf( return compiler.Compile("", fmt.Sprintf(
`#dagger: compute: [{do:"fetch-git", remote:"%s", ref:"%s"}]`, `#dagger: compute: [{do:"fetch-git", remote:"%s", ref:"%s"}]`,
remote, remote,
ref, ref,
@ -177,7 +177,7 @@ func (f sourceFlag) Set(s string) error {
} }
switch u.Scheme { switch u.Scheme {
case "", "file": case "", "file":
return cc.Compile( return compiler.Compile(
"source", "source",
// FIXME: include only cue files as a shortcut. Make this configurable somehow. // FIXME: include only cue files as a shortcut. Make this configurable somehow.
fmt.Sprintf(`[{do:"local",dir:"%s",include:["*.cue","cue.mod"]}]`, u.Host+u.Path), fmt.Sprintf(`[{do:"local",dir:"%s",include:["*.cue","cue.mod"]}]`, u.Host+u.Path),
@ -210,7 +210,7 @@ type cueFlag struct {
func (f cueFlag) Set(s string) error { func (f cueFlag) Set(s string) error {
return f.iv.Set(s, func(s string) (interface{}, error) { return f.iv.Set(s, func(s string) (interface{}, error) {
return cc.Compile("cue input", s) return compiler.Compile("cue input", s)
}) })
} }

View File

@ -10,7 +10,7 @@ import (
"github.com/rs/zerolog/log" "github.com/rs/zerolog/log"
"gopkg.in/yaml.v3" "gopkg.in/yaml.v3"
"dagger.cloud/go/dagger/cc" "dagger.cloud/go/dagger/compiler"
) )
// An execution pipeline // An execution pipeline
@ -32,8 +32,8 @@ func (p *Pipeline) FS() FS {
return p.fs return p.fs
} }
func ops(code ...*cc.Value) ([]*cc.Value, error) { func ops(code ...*compiler.Value) ([]*compiler.Value, error) {
ops := []*cc.Value{} ops := []*compiler.Value{}
// 1. Decode 'code' into a single flat array of operations. // 1. Decode 'code' into a single flat array of operations.
for _, x := range code { for _, x := range code {
// 1. attachment array // 1. attachment array
@ -58,7 +58,7 @@ func ops(code ...*cc.Value) ([]*cc.Value, error) {
return ops, nil return ops, nil
} }
func Analyze(fn func(*cc.Value) error, code ...*cc.Value) error { func Analyze(fn func(*compiler.Value) error, code ...*compiler.Value) error {
ops, err := ops(code...) ops, err := ops(code...)
if err != nil { if err != nil {
return err return err
@ -71,7 +71,7 @@ func Analyze(fn func(*cc.Value) error, code ...*cc.Value) error {
return nil return nil
} }
func analyzeOp(fn func(*cc.Value) error, op *cc.Value) error { func analyzeOp(fn func(*compiler.Value) error, op *compiler.Value) error {
if err := fn(op); err != nil { if err := fn(op); err != nil {
return err return err
} }
@ -83,7 +83,7 @@ func analyzeOp(fn func(*cc.Value) error, op *cc.Value) error {
case "load", "copy": case "load", "copy":
return Analyze(fn, op.Get("from")) return Analyze(fn, op.Get("from"))
case "exec": case "exec":
return op.Get("mount").RangeStruct(func(dest string, mnt *cc.Value) error { return op.Get("mount").RangeStruct(func(dest string, mnt *compiler.Value) error {
if from := mnt.Get("from"); from.Exists() { if from := mnt.Get("from"); from.Exists() {
return Analyze(fn, from) return Analyze(fn, from)
} }
@ -97,7 +97,7 @@ func analyzeOp(fn func(*cc.Value) error, op *cc.Value) error {
// 1) a single operation // 1) a single operation
// 2) an array of operations // 2) an array of operations
// 3) a value with an attached array of operations // 3) a value with an attached array of operations
func (p *Pipeline) Do(ctx context.Context, code ...*cc.Value) error { func (p *Pipeline) Do(ctx context.Context, code ...*compiler.Value) error {
ops, err := ops(code...) ops, err := ops(code...)
if err != nil { if err != nil {
return err return err
@ -132,7 +132,7 @@ func (p *Pipeline) Do(ctx context.Context, code ...*cc.Value) error {
return nil return nil
} }
func (p *Pipeline) doOp(ctx context.Context, op *cc.Value) error { func (p *Pipeline) doOp(ctx context.Context, op *compiler.Value) error {
do, err := op.Get("do").String() do, err := op.Get("do").String()
if err != nil { if err != nil {
return err return err
@ -165,7 +165,7 @@ func (p *Pipeline) Tmp() *Pipeline {
return NewPipeline(p.s, nil) return NewPipeline(p.s, nil)
} }
func (p *Pipeline) Subdir(ctx context.Context, op *cc.Value) error { func (p *Pipeline) Subdir(ctx context.Context, op *compiler.Value) error {
// FIXME: this could be more optimized by carrying subdir path as metadata, // FIXME: this could be more optimized by carrying subdir path as metadata,
// and using it in copy, load or mount. // and using it in copy, load or mount.
@ -186,7 +186,7 @@ func (p *Pipeline) Subdir(ctx context.Context, op *cc.Value) error {
return nil return nil
} }
func (p *Pipeline) Copy(ctx context.Context, op *cc.Value) error { func (p *Pipeline) Copy(ctx context.Context, op *compiler.Value) error {
// Decode copy options // Decode copy options
src, err := op.Get("src").String() src, err := op.Get("src").String()
if err != nil { if err != nil {
@ -218,7 +218,7 @@ func (p *Pipeline) Copy(ctx context.Context, op *cc.Value) error {
return nil return nil
} }
func (p *Pipeline) Local(ctx context.Context, op *cc.Value) error { func (p *Pipeline) Local(ctx context.Context, op *compiler.Value) error {
dir, err := op.Get("dir").String() dir, err := op.Get("dir").String()
if err != nil { if err != nil {
return err return err
@ -237,7 +237,7 @@ func (p *Pipeline) Local(ctx context.Context, op *cc.Value) error {
return nil return nil
} }
func (p *Pipeline) Exec(ctx context.Context, op *cc.Value) error { func (p *Pipeline) Exec(ctx context.Context, op *compiler.Value) error {
opts := []llb.RunOption{} opts := []llb.RunOption{}
var cmd struct { var cmd struct {
Args []string Args []string
@ -284,9 +284,9 @@ func (p *Pipeline) Exec(ctx context.Context, op *cc.Value) error {
return nil return nil
} }
func (p *Pipeline) mountAll(ctx context.Context, mounts *cc.Value) ([]llb.RunOption, error) { func (p *Pipeline) mountAll(ctx context.Context, mounts *compiler.Value) ([]llb.RunOption, error) {
opts := []llb.RunOption{} opts := []llb.RunOption{}
err := mounts.RangeStruct(func(dest string, mnt *cc.Value) error { err := mounts.RangeStruct(func(dest string, mnt *compiler.Value) error {
o, err := p.mount(ctx, dest, mnt) o, err := p.mount(ctx, dest, mnt)
if err != nil { if err != nil {
return err return err
@ -297,7 +297,7 @@ func (p *Pipeline) mountAll(ctx context.Context, mounts *cc.Value) ([]llb.RunOpt
return opts, err return opts, err
} }
func (p *Pipeline) mount(ctx context.Context, dest string, mnt *cc.Value) (llb.RunOption, error) { func (p *Pipeline) mount(ctx context.Context, dest string, mnt *compiler.Value) (llb.RunOption, error) {
if s, err := mnt.String(); err == nil { if s, err := mnt.String(); err == nil {
// eg. mount: "/foo": "cache" // eg. mount: "/foo": "cache"
switch s { switch s {
@ -338,7 +338,7 @@ func (p *Pipeline) mount(ctx context.Context, dest string, mnt *cc.Value) (llb.R
return llb.AddMount(dest, from.FS().LLB(), mo...), nil return llb.AddMount(dest, from.FS().LLB(), mo...), nil
} }
func (p *Pipeline) Export(ctx context.Context, op *cc.Value) error { func (p *Pipeline) Export(ctx context.Context, op *compiler.Value) error {
source, err := op.Get("source").String() source, err := op.Get("source").String()
if err != nil { if err != nil {
return err return err
@ -419,7 +419,7 @@ func unmarshalAnything(data []byte, fn unmarshaller) (interface{}, error) {
return o, err return o, err
} }
func (p *Pipeline) Load(ctx context.Context, op *cc.Value) error { func (p *Pipeline) Load(ctx context.Context, op *compiler.Value) error {
// Execute 'from' in a tmp pipeline, and use the resulting fs // Execute 'from' in a tmp pipeline, and use the resulting fs
from := p.Tmp() from := p.Tmp()
if err := from.Do(ctx, op.Get("from")); err != nil { if err := from.Do(ctx, op.Get("from")); err != nil {
@ -429,7 +429,7 @@ func (p *Pipeline) Load(ctx context.Context, op *cc.Value) error {
return nil return nil
} }
func (p *Pipeline) FetchContainer(ctx context.Context, op *cc.Value) error { func (p *Pipeline) FetchContainer(ctx context.Context, op *compiler.Value) error {
ref, err := op.Get("ref").String() ref, err := op.Get("ref").String()
if err != nil { if err != nil {
return err return err
@ -439,7 +439,7 @@ func (p *Pipeline) FetchContainer(ctx context.Context, op *cc.Value) error {
return nil return nil
} }
func (p *Pipeline) FetchGit(ctx context.Context, op *cc.Value) error { func (p *Pipeline) FetchGit(ctx context.Context, op *compiler.Value) error {
remote, err := op.Get("remote").String() remote, err := op.Get("remote").String()
if err != nil { if err != nil {
return err return err