cmd/cue/cmd: move export to filetypes
Change-Id: Iea5d2e61758971566c4a0eab694b2ce397f0843a
Reviewed-on: https://cue-review.googlesource.com/c/cue/+/5023
Reviewed-by: Marcel van Lohuizen <mpvl@golang.org>
diff --git a/internal/encoding/encoding.go b/internal/encoding/encoding.go
index a032bde..300a3f1 100644
--- a/internal/encoding/encoding.go
+++ b/internal/encoding/encoding.go
@@ -22,10 +22,12 @@
"os"
"strings"
+ "cuelang.org/go/cue"
"cuelang.org/go/cue/ast"
"cuelang.org/go/cue/build"
"cuelang.org/go/encoding/json"
"cuelang.org/go/encoding/protobuf"
+ "cuelang.org/go/internal/filetypes"
"cuelang.org/go/internal/third_party/yaml"
)
@@ -51,22 +53,35 @@
}
}
-func (i *Decoder) File() *ast.File {
- if i.file != nil {
- return i.file
- }
- switch x := i.expr.(type) {
+func toFile(x ast.Expr) *ast.File {
+ switch x := x.(type) {
case nil:
return nil
case *ast.StructLit:
return &ast.File{Decls: x.Elts}
default:
- return &ast.File{
- Decls: []ast.Decl{&ast.EmbedDecl{Expr: i.expr}},
- }
+ return &ast.File{Decls: []ast.Decl{&ast.EmbedDecl{Expr: x}}}
}
}
+func valueToFile(v cue.Value) *ast.File {
+ switch x := v.Syntax().(type) {
+ case *ast.File:
+ return x
+ case ast.Expr:
+ return toFile(x)
+ default:
+ panic("unrreachable")
+ }
+}
+
+func (i *Decoder) File() *ast.File {
+ if i.file != nil {
+ return i.file
+ }
+ return toFile(i.expr)
+}
+
func (i *Decoder) Err() error {
if i.err == io.EOF {
return nil
@@ -79,9 +94,17 @@
}
type Config struct {
- Stdin io.Reader
- Stdout io.Writer
- ProtoPath []string
+ Mode filetypes.Mode
+
+ // Out specifies an overwrite destination.
+ Out io.Writer
+ Stdin io.Reader
+ Stdout io.Writer
+
+ Force bool // overwrite existing files.
+
+ EscapeHTML bool
+ ProtoPath []string
}
// NewDecoder returns a stream of non-rooted data expressions. The encoding