Add YAML support to export command

This is a PR for issue #182 where YAML support for the export command is suggested.

Closes #194
https://github.com/cuelang/cue/pull/194

GitOrigin-RevId: 8e9ab9d521b2dfb629fbbfbc274004417d1bc7aa
Change-Id: I01780450a7a1587afc6e6431affc48df7a1da78b
Reviewed-on: https://cue-review.googlesource.com/c/cue/+/4161
Reviewed-by: Marcel van Lohuizen <mpvl@golang.org>
diff --git a/cmd/cue/cmd/export.go b/cmd/cue/cmd/export.go
index 9924273..e7218a3 100644
--- a/cmd/cue/cmd/export.go
+++ b/cmd/cue/cmd/export.go
@@ -15,6 +15,7 @@
 package cmd
 
 import (
+	"cuelang.org/go/pkg/encoding/yaml"
 	"encoding/json"
 	"fmt"
 	"io"
@@ -107,6 +108,9 @@
 		case "text":
 			err := outputText(w, root)
 			exitIfErr(cmd, inst, err, true)
+		case "yaml":
+			err := outputYAML(w, root)
+			exitIfErr(cmd, inst, err, true)
 		default:
 			return fmt.Errorf("export: unknown format %q", media)
 		}
@@ -137,3 +141,12 @@
 	_, err = fmt.Fprint(w, str)
 	return err
 }
+
+func outputYAML(w io.Writer, v cue.Value) error {
+	str, err := yaml.Marshal(v)
+	if err != nil {
+		return err
+	}
+	_, err = fmt.Fprint(w, str)
+	return err
+}
diff --git a/cmd/cue/cmd/flags.go b/cmd/cue/cmd/flags.go
index f202eab..ee20752 100644
--- a/cmd/cue/cmd/flags.go
+++ b/cmd/cue/cmd/flags.go
@@ -42,7 +42,7 @@
 
 var flagMedia = stringFlag{
 	name: "out",
-	text: "output format (json or text)",
+	text: "output format (json, yaml or text)",
 	def:  "json",
 }
 
diff --git a/cmd/cue/cmd/testdata/script/export_yaml.txt b/cmd/cue/cmd/testdata/script/export_yaml.txt
new file mode 100644
index 0000000..1fe971d
--- /dev/null
+++ b/cmd/cue/cmd/testdata/script/export_yaml.txt
@@ -0,0 +1,13 @@
+cue export --out yaml ./hello
+cmp stdout expect-stdout
+-- expect-stdout --
+message: Hello World!
+-- hello/data.cue --
+package hello
+
+who :: "World"
+-- hello/hello.cue --
+package hello
+
+message: "Hello \(who)!" // who declared in data.cue
+-- hello/cue.mod --