cmd/cue: add version subcommand

Closes #25

Change-Id: Ib9da159ad82f7b8225a48f1f5a61746fba86340c
Reviewed-on: https://cue-review.googlesource.com/c/cue/+/2321
Reviewed-by: Marcel van Lohuizen <mpvl@google.com>
diff --git a/cmd/cue/cmd/root.go b/cmd/cue/cmd/root.go
index 49abeb7..7dbf6e7 100644
--- a/cmd/cue/cmd/root.go
+++ b/cmd/cue/cmd/root.go
@@ -76,6 +76,7 @@
 		newFmtCmd(),
 		newExportCmd(),
 		cmdCmd,
+		newVersionCmd(),
 		newVetCmd(),
 		newAddCmd(),
 	}
diff --git a/cmd/cue/cmd/version.go b/cmd/cue/cmd/version.go
new file mode 100644
index 0000000..d605469
--- /dev/null
+++ b/cmd/cue/cmd/version.go
@@ -0,0 +1,47 @@
+// Copyright 2019 CUE Authors
+//
+// Licensed under the Apache License, Version 2.0 (the "License");
+// you may not use this file except in compliance with the License.
+// You may obtain a copy of the License at
+//
+//     http://www.apache.org/licenses/LICENSE-2.0
+//
+// Unless required by applicable law or agreed to in writing, software
+// distributed under the License is distributed on an "AS IS" BASIS,
+// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+// See the License for the specific language governing permissions and
+// limitations under the License.
+
+package cmd
+
+import (
+	"fmt"
+	goruntime "runtime"
+
+	"github.com/spf13/cobra"
+)
+
+func newVersionCmd() *cobra.Command {
+	cmd := &cobra.Command{
+		Use:   "version",
+		Short: "print CUE version",
+		Long:  ``,
+		RunE:  runVersion,
+	}
+	return cmd
+}
+
+// set by goreleaser or other builder using
+// -ldflags='-X cuelang.org/go/cmd/cue/cmd.version=<version>'
+var (
+	version = "custom"
+)
+
+func runVersion(cmd *cobra.Command, args []string) error {
+	w := cmd.OutOrStdout()
+	fmt.Fprintf(w, "cue version %v %s/%s\n",
+		version,
+		goruntime.GOOS, goruntime.GOARCH,
+	)
+	return nil
+}