Skip to content

Commit

Permalink
cmd/cue,pkg/tool/exec: rely on cue schema for exec.Run defaults
Browse files Browse the repository at this point in the history
Currently, exec.Run is unable to properly leverage its defined schema
to evaluate defaults, due to the fields not being defined in the legacy
schema definitions.

This updates the logic in the task runner to unify the old schemas with
the new, to allow for new fields to be referenced.

Signed-off-by: Nick Figgins <figginsn@gmail.com>
Change-Id: I7b1eda6a002bb12a5059ddbc7ced42c76102ecc2
Reviewed-on: https://review.gerrithub.io/c/cue-lang/cue/+/1176665
Reviewed-by: Daniel Martí <mvdan@mvdan.cc>
TryBot-Result: CUEcueckoo <cueckoo@gmail.com>
Unity-Result: CUE porcuepine <cue.porcuepine@gmail.com>
  • Loading branch information
nickfiggins authored and mvdan committed Feb 10, 2024
1 parent d37ea66 commit b460e71
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 16 deletions.
9 changes: 8 additions & 1 deletion cmd/cue/cmd/custom.go
Expand Up @@ -221,8 +221,10 @@ func newTaskFunc(cmd *Command) flow.TaskFunc {
return nil, errors.Promote(err1, "newTask")
}
}
var isLegacy bool
if k, ok := legacyKinds[kind]; ok {
kind = k
isLegacy = true
}
rf := itask.Lookup(kind)
if rf == nil {
Expand All @@ -242,12 +244,17 @@ func newTaskFunc(cmd *Command) flow.TaskFunc {
}

return flow.RunnerFunc(func(t *flow.Task) error {
obj := t.Value()

if isLegacy {
obj = obj.Unify(v)
}
c := &itask.Context{
Context: t.Context(),
Stdin: cmd.InOrStdin(),
Stdout: cmd.OutOrStdout(),
Stderr: cmd.OutOrStderr(),
Obj: t.Value(),
Obj: obj,
}
value, err := runner.Run(c)
if err != nil {
Expand Down
22 changes: 7 additions & 15 deletions pkg/tool/exec/exec.go
Expand Up @@ -45,13 +45,8 @@ func (c *execCmd) Run(ctx *task.Context) (res interface{}, err error) {

// TODO: set environment variables, if defined.
stream := func(name string) (stream cue.Value, ok bool) {
c := ctx.Obj.Lookup(name)
// Although the schema defines a default versions, older implementations
// may not use it yet.
if !c.Exists() {
return
}
if err := c.Null(); ctx.Err != nil || err == nil {
c := ctx.Obj.LookupPath(cue.ParsePath(name))
if err := c.Null(); c.Err() != nil || err == nil {
return
}
return c, true
Expand All @@ -71,14 +66,10 @@ func (c *execCmd) Run(ctx *task.Context) (res interface{}, err error) {
cmd.Stderr = ctx.Stderr
}

// TODO(mvdan): exec.Run declares mustSucceed as a regular field with a default of true.
// We should be able to rely on that here, removing the need for Exists and repeating the default.
mustSucceed := true
if v := ctx.Obj.LookupPath(cue.ParsePath("mustSucceed")); v.Exists() {
mustSucceed, err = v.Bool()
if err != nil {
return nil, errors.Wrapf(err, v.Pos(), "invalid bool value")
}
v := ctx.Obj.LookupPath(cue.ParsePath("mustSucceed"))
mustSucceed, err := v.Bool()
if err != nil {
return nil, errors.Wrapf(err, v.Pos(), "invalid bool value")
}

update := map[string]interface{}{}
Expand All @@ -102,6 +93,7 @@ func (c *execCmd) Run(ctx *task.Context) (res interface{}, err error) {
update["stderr"] = err.Error()
}
}

if !mustSucceed {
return update, nil
}
Expand Down

0 comments on commit b460e71

Please sign in to comment.