cmd/cue: some small improvements to output
- output prints instance
- simplified formatting
- shorter error message
Change-Id: I6201fba03a699bb053f97501a2321d695cb12715
diff --git a/cmd/cue/cmd/common.go b/cmd/cue/cmd/common.go
index 1f93ab9..e8933eb 100644
--- a/cmd/cue/cmd/common.go
+++ b/cmd/cue/cmd/common.go
@@ -15,6 +15,8 @@
package cmd
import (
+ "fmt"
+
"cuelang.org/go/cue"
"cuelang.org/go/cue/build"
"cuelang.org/go/cue/errors"
@@ -22,8 +24,9 @@
"github.com/spf13/cobra"
)
-func exitIfErr(cmd *cobra.Command, err error) {
+func exitIfErr(cmd *cobra.Command, inst *cue.Instance, err error) {
if err != nil {
+ fmt.Fprintf(cmd.OutOrStderr(), "--- %s\n", inst.Dir)
errors.Print(cmd.OutOrStderr(), err)
exit()
}
@@ -51,13 +54,13 @@
for _, inst := range instances {
// TODO: consider merging errors of multiple files, but ensure
// duplicates are removed.
- exitIfErr(cmd, inst.Err)
+ exitIfErr(cmd, inst, inst.Err)
}
for _, inst := range instances {
// TODO: consider merging errors of multiple files, but ensure
// duplicates are removed.
- exitIfErr(cmd, inst.Value().Validate())
+ exitIfErr(cmd, inst, inst.Value().Validate())
}
return instances
}
@@ -81,6 +84,6 @@
}
inst := cue.Merge(buildInstances(cmd, binst)...).Build(ti)
- exitIfErr(cmd, inst.Err)
+ exitIfErr(cmd, inst, inst.Err)
return inst
}
diff --git a/cmd/cue/cmd/eval.go b/cmd/cue/cmd/eval.go
index af90e21..1e58f14 100644
--- a/cmd/cue/cmd/eval.go
+++ b/cmd/cue/cmd/eval.go
@@ -64,9 +64,9 @@
tw := tabwriter.NewWriter(cmd.OutOrStdout(), 0, 0, 1, ' ', 0)
defer tw.Flush()
for _, inst := range instances {
- // TODO: retrieve the fileset from the instance. Probably using an
- // internal structure.
- p := evalPrinter{w: tw, fset: nil}
+ // TODO: use ImportPath or some other sanitized path.
+ fmt.Fprintf(cmd.OutOrStdout(), "// %s\n", inst.Dir)
+ p := evalPrinter{w: tw}
if exprs == nil {
p.print(inst.Value())
fmt.Fprintln(tw)
diff --git a/cmd/cue/cmd/import.go b/cmd/cue/cmd/import.go
index b42a788..1d988b5 100644
--- a/cmd/cue/cmd/import.go
+++ b/cmd/cue/cmd/import.go
@@ -449,7 +449,7 @@
for _, enc := range encoding.All() {
if ident, ok := h.altNames[enc.Name()]; ok {
short := enc.Name()
- name := h.uniqueName(short, "")
+ name := h.uniqueName(short, "", "")
ident.Name = name
if name == short {
ident = nil
@@ -669,11 +669,11 @@
}
if h.altNames[enc.typ] == nil {
- h.altNames[enc.typ] = &ast.Ident{Name: "cue"} // set name later
+ h.altNames[enc.typ] = &ast.Ident{Name: "_cue"} // set name later
}
// found a replacable string
- dataField := h.uniqueName(name, "cue")
+ dataField := h.uniqueName(name, "_", "cue_")
f.Value = &ast.CallExpr{
Fun: &ast.SelectorExpr{
@@ -727,7 +727,7 @@
return nil, nil
}
-func (h *hoister) uniqueName(base, typ string) string {
+func (h *hoister) uniqueName(base, prefix, typ string) string {
base = strings.Map(func(r rune) rune {
if unicode.In(r, unicode.L, unicode.N) {
return r
@@ -735,12 +735,13 @@
return '_'
}, base)
- name := base
+ name := prefix + typ + base
for {
if !h.fields[name] {
+ h.fields[name] = true
return name
}
- name = typ + "_" + base
+ name = prefix + typ + base
typ += "x"
}
}
diff --git a/cmd/cue/cmd/testdata/hello/eval.out b/cmd/cue/cmd/testdata/hello/eval.out
index 8af3d0f..38aec46 100644
--- a/cmd/cue/cmd/testdata/hello/eval.out
+++ b/cmd/cue/cmd/testdata/hello/eval.out
@@ -1,3 +1,4 @@
+// $CWD/testdata/hello
{
who: "World"
message: "Hello World!"
diff --git a/cmd/cue/cmd/testdata/import/import_hoiststr.out b/cmd/cue/cmd/testdata/import/import_hoiststr.out
index d01566f..29e11cc 100644
--- a/cmd/cue/cmd/testdata/import/import_hoiststr.out
+++ b/cmd/cue/cmd/testdata/import/import_hoiststr.out
@@ -1,4 +1,4 @@
-import _json "encoding/json"
+import xjson "encoding/json"
service: {
booster: [{
@@ -8,8 +8,8 @@
supplement: [{
kind: "Service"
name: "supplement"
- json: _json.Marshal(cue_json)
- cue_json = [1, 2]
+ json: xjson.Marshal(_cue_json)
+ _cue_json = [1, 2]
}]
}
deployment booster: [{
diff --git a/cmd/cue/cmd/testdata/loaderr/loaderr.out b/cmd/cue/cmd/testdata/loaderr/loaderr.out
index 2921045..a5cdcdc 100644
--- a/cmd/cue/cmd/testdata/loaderr/loaderr.out
+++ b/cmd/cue/cmd/testdata/loaderr/loaderr.out
@@ -1,3 +1,3 @@
-cannot find package "non-existing":
-
+--- non-existing
+cannot find package "non-existing"
terminating because of errors
diff --git a/cmd/cue/cmd/testdata/tasks/eval.out b/cmd/cue/cmd/testdata/tasks/eval.out
index 8c85ba9..c9828a3 100644
--- a/cmd/cue/cmd/testdata/tasks/eval.out
+++ b/cmd/cue/cmd/testdata/tasks/eval.out
@@ -1,3 +1,4 @@
+// $CWD/testdata/tasks
{
message: "Hello world!"
}
diff --git a/cue/format/node.go b/cue/format/node.go
index a51ad57..f9cef31 100644
--- a/cue/format/node.go
+++ b/cue/format/node.go
@@ -156,7 +156,7 @@
nextFF := f.nextNeedsFormfeed(n.Value)
tab := vtab
- if nextFF || f.nestExpr >= 1 {
+ if nextFF {
tab = blank
}
diff --git a/cue/load/search.go b/cue/load/search.go
index ae11011..8155593 100644
--- a/cue/load/search.go
+++ b/cue/load/search.go
@@ -16,7 +16,6 @@
import (
"fmt" // TODO: remove this usage
- "log"
"os"
"path"
"path/filepath"
@@ -196,8 +195,7 @@
}
return nil
default:
- log.Print(err)
- return nil
+ m.Err = err
}
}
diff --git a/cue/types.go b/cue/types.go
index 0da5f1b..42a3a54 100644
--- a/cue/types.go
+++ b/cue/types.go
@@ -714,7 +714,7 @@
}
if !got.isGround() {
return ctx.mkErr(x, codeIncomplete,
- "non-concrete value %v when evaluating config file", got)
+ "non-concrete value %v", got)
}
return nil
}
diff --git a/go.mod b/go.mod
index 5e108d3..ceebce3 100644
--- a/go.mod
+++ b/go.mod
@@ -8,7 +8,7 @@
github.com/pkg/errors v0.8.0 // indirect
github.com/spf13/cobra v0.0.3
github.com/spf13/viper v1.3.1
- golang.org/x/exp/errors v0.0.0-20181210123644-7d6377eee41f
+ golang.org/x/exp/errors v0.0.0-20181220081853-a8d4f384862a
golang.org/x/sync v0.0.0-20181108010431-42b317875d0f
golang.org/x/tools v0.0.0-20181210225255-6a3e9aa2ab77
)
diff --git a/go.sum b/go.sum
index 7c0bae2..c5f0549 100644
--- a/go.sum
+++ b/go.sum
@@ -42,6 +42,8 @@
golang.org/x/crypto v0.0.0-20181203042331-505ab145d0a9/go.mod h1:6SG95UA2DQfeDnfUPMdvaQW0Q7yPrPDi9nlGo2tz2b4=
golang.org/x/exp/errors v0.0.0-20181210123644-7d6377eee41f h1:B/8yFg7PHSFdahc+fMB+RUy3if9GlZmexAbcdfCwREI=
golang.org/x/exp/errors v0.0.0-20181210123644-7d6377eee41f/go.mod h1:YgqsNsAu4fTvlab/7uiYK9LJrCIzKg/NiZUIH1/ayqo=
+golang.org/x/exp/errors v0.0.0-20181220081853-a8d4f384862a h1:juhXrq7Jv3Yw6H4azsxx0SGft00IiERvkUW6cNqnV1I=
+golang.org/x/exp/errors v0.0.0-20181220081853-a8d4f384862a/go.mod h1:YgqsNsAu4fTvlab/7uiYK9LJrCIzKg/NiZUIH1/ayqo=
golang.org/x/sync v0.0.0-20181108010431-42b317875d0f h1:Bl/8QSvNqXvPGPGXa2z5xUTmV7VDcZyvRZ+QQXkXTZQ=
golang.org/x/sync v0.0.0-20181108010431-42b317875d0f/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM=
golang.org/x/sys v0.0.0-20181205085412-a5c9d58dba9a h1:1n5lsVfiQW3yfsRGu98756EH1YthsFqr/5mxHduZW2A=