cmd/cue/cmd: show output when requesting concrete
right now it just validated
Issue #52
Change-Id: Ie34afea9b495b7b9cdfc81dd98ce685539d19407
Reviewed-on: https://cue-review.googlesource.com/c/cue/+/2179
Reviewed-by: Marcel van Lohuizen <mpvl@google.com>
diff --git a/cmd/cue/cmd/common.go b/cmd/cue/cmd/common.go
index c301eea..7d23861 100644
--- a/cmd/cue/cmd/common.go
+++ b/cmd/cue/cmd/common.go
@@ -16,10 +16,7 @@
import (
"bytes"
- "fmt"
- "io"
"os"
- "strings"
"cuelang.org/go/cue"
"cuelang.org/go/cue/build"
@@ -31,22 +28,11 @@
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
}
@@ -56,7 +42,6 @@
}
w := &bytes.Buffer{}
- printHeader(w, cwd, file)
errors.Print(w, err)
// TODO: do something more principled than this.
diff --git a/cmd/cue/cmd/eval.go b/cmd/cue/cmd/eval.go
index 1f282dd..82aa873 100644
--- a/cmd/cue/cmd/eval.go
+++ b/cmd/cue/cmd/eval.go
@@ -117,14 +117,28 @@
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
diff --git a/cmd/cue/cmd/eval_test.go b/cmd/cue/cmd/eval_test.go
index d073448..ccb7fa7 100644
--- a/cmd/cue/cmd/eval_test.go
+++ b/cmd/cue/cmd/eval_test.go
@@ -18,4 +18,12 @@
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")
}
diff --git a/cmd/cue/cmd/import.go b/cmd/cue/cmd/import.go
index c0132b2..16801ba 100644
--- a/cmd/cue/cmd/import.go
+++ b/cmd/cue/cmd/import.go
@@ -308,7 +308,7 @@
})
err := group.Wait()
- exitOnErr(cmd, "", err, true)
+ exitOnErr(cmd, err, true)
return nil
}
diff --git a/cmd/cue/cmd/testdata/hello/eval_conc.out b/cmd/cue/cmd/testdata/hello/eval_conc.out
new file mode 100644
index 0000000..1ed63b3
--- /dev/null
+++ b/cmd/cue/cmd/testdata/hello/eval_conc.out
@@ -0,0 +1,2 @@
+who: "World"
+message: "Hello World!"
diff --git a/cmd/cue/cmd/testdata/loaderr/loaderr.out b/cmd/cue/cmd/testdata/loaderr/loaderr.out
index a5cdcdc..68b2374 100644
--- a/cmd/cue/cmd/testdata/loaderr/loaderr.out
+++ b/cmd/cue/cmd/testdata/loaderr/loaderr.out
@@ -1,3 +1,2 @@
---- non-existing
cannot find package "non-existing"
terminating because of errors
diff --git a/cmd/cue/cmd/testdata/partial/eval_conc.out b/cmd/cue/cmd/testdata/partial/eval_conc.out
new file mode 100644
index 0000000..9437c40
--- /dev/null
+++ b/cmd/cue/cmd/testdata/partial/eval_conc.out
@@ -0,0 +1,2 @@
+: invalid non-ground value string (must be concrete int|string):
+ ./testdata/partial/partial.cue:7:7
diff --git a/cmd/cue/cmd/testdata/partial/eval_expr.out b/cmd/cue/cmd/testdata/partial/eval_expr.out
new file mode 100644
index 0000000..a4b4b79
--- /dev/null
+++ b/cmd/cue/cmd/testdata/partial/eval_expr.out
@@ -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