cmd/cue: don't print optional outer curly braces
Issue #50.
Change-Id: I742002d9b3264789d8cbbbe2954859a14a422541
Reviewed-on: https://cue-review.googlesource.com/c/cue/+/2166
Reviewed-by: Marcel van Lohuizen <mpvl@google.com>
diff --git a/cmd/cue/cmd/eval.go b/cmd/cue/cmd/eval.go
index 45224c4..2d154ea 100644
--- a/cmd/cue/cmd/eval.go
+++ b/cmd/cue/cmd/eval.go
@@ -108,6 +108,10 @@
format.UseSpaces(4),
format.TabIndent(false),
}
+ if flagSimplify.Bool(cmd) {
+ opts = append(opts, format.Simplify())
+ }
+
if exprs == nil {
v := inst.Value()
if flagConcrete.Bool(cmd) {
@@ -115,13 +119,22 @@
exitIfErr(cmd, inst, err, false)
continue
}
- format.Node(w, v.Syntax(syn...), opts...)
+ format.Node(w, getSyntax(v, syn), opts...)
fmt.Fprintln(w)
}
for _, e := range exprs {
- format.Node(w, inst.Eval(e).Syntax(syn...), opts...)
+ format.Node(w, getSyntax(inst.Eval(e), syn), opts...)
fmt.Fprintln(w)
}
}
return nil
}
+
+func getSyntax(v cue.Value, opts []cue.Option) ast.Node {
+ n := v.Syntax(opts...)
+ switch x := n.(type) {
+ case *ast.StructLit:
+ n = &ast.File{Decls: x.Elts}
+ }
+ return n
+}
diff --git a/cmd/cue/cmd/testdata/hello/eval.out b/cmd/cue/cmd/testdata/hello/eval.out
index 38aec46..0c32af3 100644
--- a/cmd/cue/cmd/testdata/hello/eval.out
+++ b/cmd/cue/cmd/testdata/hello/eval.out
@@ -1,5 +1,4 @@
// $CWD/testdata/hello
-{
- who: "World"
- message: "Hello World!"
-}
+who: "World"
+message: "Hello World!"
+
diff --git a/cmd/cue/cmd/testdata/partial/eval.out b/cmd/cue/cmd/testdata/partial/eval.out
index ac847f1..fd0cf4d 100644
--- a/cmd/cue/cmd/testdata/partial/eval.out
+++ b/cmd/cue/cmd/testdata/partial/eval.out
@@ -1,24 +1,23 @@
// $CWD/testdata/partial
-{
- def: 1
- sum: 1 | 2
- A = a
- b: {
- idx: A[str]
- a: {
- b: 4
- }
- str: string
- }
+def: 1
+sum: 1 | 2
+A = a
+b: {
+ idx: A[str]
a: {
- b: 3
- c: 4
+ b: 4
}
- c: {
- idx: 3
- a: {
- b: 4
- }
- str: "b"
- }
+ str: string
}
+a: {
+ b: 3
+ c: 4
+}
+c: {
+ idx: 3
+ a: {
+ b: 4
+ }
+ str: "b"
+}
+
diff --git a/cmd/cue/cmd/testdata/tasks/eval.out b/cmd/cue/cmd/testdata/tasks/eval.out
index c9828a3..e883d86 100644
--- a/cmd/cue/cmd/testdata/tasks/eval.out
+++ b/cmd/cue/cmd/testdata/tasks/eval.out
@@ -1,4 +1,3 @@
// $CWD/testdata/tasks
-{
- message: "Hello world!"
-}
+message: "Hello world!"
+