Skip to content

Commit

Permalink
cmd/cue/cmd: show output when requesting concrete
Browse files Browse the repository at this point in the history
right now it just validated

Issue cue-lang#52

Change-Id: Ie34afea9b495b7b9cdfc81dd98ce685539d19407
Reviewed-on: https://cue-review.googlesource.com/c/cue/+/2179
Reviewed-by: Marcel van Lohuizen <mpvl@google.com>
  • Loading branch information
mpvl committed Jun 7, 2019
1 parent 00c373d commit e6d6ca5
Show file tree
Hide file tree
Showing 8 changed files with 38 additions and 23 deletions.
19 changes: 2 additions & 17 deletions cmd/cue/cmd/common.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,10 +16,7 @@ package cmd

import (
"bytes"
"fmt"
"io"
"os"
"strings"

"cuelang.org/go/cue"
"cuelang.org/go/cue/build"
Expand All @@ -31,22 +28,11 @@ import (

var runtime = &cue.Runtime{}

// printHeader is a hacky and unprincipled way to sanatize the package path.
func printHeader(w io.Writer, cwd, dir string) {
if cwd != "" {
if dir == cwd {
return
}
dir = strings.Replace(dir, cwd, ".", 1)
}
fmt.Fprintf(w, "--- %s\n", dir)
}

func exitIfErr(cmd *cobra.Command, inst *cue.Instance, err error, fatal bool) {
exitOnErr(cmd, inst.Dir, err, fatal)
exitOnErr(cmd, err, fatal)
}

func exitOnErr(cmd *cobra.Command, file string, err error, fatal bool) {
func exitOnErr(cmd *cobra.Command, err error, fatal bool) {
if err == nil {
return
}
Expand All @@ -56,7 +42,6 @@ func exitOnErr(cmd *cobra.Command, file string, err error, fatal bool) {
}

w := &bytes.Buffer{}
printHeader(w, cwd, file)
errors.Print(w, err)

// TODO: do something more principled than this.
Expand Down
22 changes: 18 additions & 4 deletions cmd/cue/cmd/eval.go
Original file line number Diff line number Diff line change
Expand Up @@ -117,14 +117,28 @@ func runEval(cmd *cobra.Command, args []string) error {
if exprs == nil {
v := inst.Value()
if flagConcrete.Bool(cmd) {
err := v.Validate(cue.Concrete(true))
exitIfErr(cmd, inst, err, false)
continue
if err := v.Validate(cue.Concrete(true)); err != nil {
exitIfErr(cmd, inst, err, false)
continue
}
}
format.Node(w, getSyntax(v, syn), opts...)
}
for _, e := range exprs {
format.Node(w, getSyntax(inst.Eval(e), syn), opts...)
if len(exprs) > 1 {
fmt.Fprint(w, "// ")
format.Node(w, e)
fmt.Fprintln(w)
}
v := inst.Eval(e)
if flagConcrete.Bool(cmd) {
if err := v.Validate(cue.Concrete(true)); err != nil {
exitIfErr(cmd, inst, err, false)
continue
}
}
format.Node(w, getSyntax(v, syn), opts...)
fmt.Fprintln(w)
}
}
return nil
Expand Down
8 changes: 8 additions & 0 deletions cmd/cue/cmd/eval_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,4 +18,12 @@ import "testing"

func TestEval(t *testing.T) {
runCommand(t, newEvalCmd(), "eval")

cmd := newEvalCmd()
cmd.ParseFlags([]string{"-c", "-a"})
runCommand(t, cmd, "eval_conc")

cmd = newEvalCmd()
cmd.ParseFlags([]string{"-c", "-e", "b.a.b", "-e", "b.idx"})
runCommand(t, cmd, "eval_expr")
}
2 changes: 1 addition & 1 deletion cmd/cue/cmd/import.go
Original file line number Diff line number Diff line change
Expand Up @@ -308,7 +308,7 @@ func runImport(cmd *cobra.Command, args []string) error {
})

err := group.Wait()
exitOnErr(cmd, "", err, true)
exitOnErr(cmd, err, true)
return nil
}

Expand Down
2 changes: 2 additions & 0 deletions cmd/cue/cmd/testdata/hello/eval_conc.out
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
who: "World"
message: "Hello World!"
1 change: 0 additions & 1 deletion cmd/cue/cmd/testdata/loaderr/loaderr.out
Original file line number Diff line number Diff line change
@@ -1,3 +1,2 @@
--- non-existing
cannot find package "non-existing"
terminating because of errors
2 changes: 2 additions & 0 deletions cmd/cue/cmd/testdata/partial/eval_conc.out
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
: invalid non-ground value string (must be concrete int|string):
./testdata/partial/partial.cue:7:7
5 changes: 5 additions & 0 deletions cmd/cue/cmd/testdata/partial/eval_expr.out
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
// b.a.b
4
// b.idx
: invalid non-ground value string (must be concrete int|string):
./testdata/partial/partial.cue:7:7

0 comments on commit e6d6ca5

Please sign in to comment.