cmd/cue: get go: add --package flag

Consider the following scenario: example.com/blah is a command (i.e. Go
main package) that is configured via CUE files passed via -config flags.
The author of the tool wants to make the CUE definitions used to
validate the config available publicly and hence runs cue get go --local
as part of a go:generate directive.

Currently this results in *_gen.cue files which have a package name of
main. This isn't particularly useful for a consumer of this CUE package
because the import path is example.com/blah, hence an import would need
to look like example.com/blah:main.

Instead we now offer a --package (-p) flag for the get go command which
allows overriding of the package name for the generated CUE files.

Change-Id: Id04f0403b962c4f35da64a676a50d641f18a1eac
Reviewed-on: https://cue-review.googlesource.com/c/cue/+/6801
Reviewed-by: Marcel van Lohuizen <mpvl@golang.org>
Reviewed-by: CUE cueckoo <cueckoo@gmail.com>
diff --git a/cmd/cue/cmd/get_go.go b/cmd/cue/cmd/get_go.go
index 85d371a..f76089d 100644
--- a/cmd/cue/cmd/get_go.go
+++ b/cmd/cue/cmd/get_go.go
@@ -214,6 +214,8 @@
 	cmd.Flags().Bool(string(flagLocal), false,
 		"generates files in the main module locally")
 
+	cmd.Flags().StringP(string(flagPackage), "p", "", "package name for generated CUE files")
+
 	return cmd
 }
 
@@ -461,7 +463,12 @@
 		}
 		sort.Strings(pkgs)
 
-		pkg := &cueast.Package{Name: e.ident(p.Name, false)}
+		pName := flagPackage.String(e.cmd)
+		if pName == "" {
+			pName = p.Name
+		}
+
+		pkg := &cueast.Package{Name: e.ident(pName, false)}
 		addDoc(f.Doc, pkg)
 
 		f := &cueast.File{Decls: []cueast.Decl{
diff --git a/cmd/cue/cmd/testdata/script/get_go_basic.txt b/cmd/cue/cmd/testdata/script/get_go_basic.txt
index 0856ea9..a82b230 100644
--- a/cmd/cue/cmd/testdata/script/get_go_basic.txt
+++ b/cmd/cue/cmd/testdata/script/get_go_basic.txt
@@ -9,6 +9,10 @@
 cue get go --local
 cmp blah_go_gen.cue all.cue.golden
 
+# Use an alternative package name
+cue get go --local -p other
+cmp blah_go_gen.cue other.cue.golden
+
 -- go.mod --
 module mod.com/blah
 -- blah.go --
@@ -43,3 +47,19 @@
 
 #LongStringConst: "This is a really long string. Why are we using a long string? Because that way it ensures we are using go/constant.Value.ExactString() instead of go/constant.Value.String()"
 #IntConst:        "test"
+-- other.cue.golden --
+// Code generated by cue get go. DO NOT EDIT.
+
+//cue:generate cue get go mod.com/blah
+
+package other
+
+#S: {
+	Name: string
+	T:    #T
+}
+
+#T: Age: int
+
+#LongStringConst: "This is a really long string. Why are we using a long string? Because that way it ensures we are using go/constant.Value.ExactString() instead of go/constant.Value.String()"
+#IntConst:        "test"