cmd/cue: use module information if no -X version information specified

Currently goreleaser is used to build and publish binary artefacts to
GitHub for each release. As part of this process, cmd/cue is built with
cuelang.org/cmd/cue as the main module, and a build flag of:

    -ldflags='-X main.version=<version>'

to inject the release version.

But when cmd/cue is resolved as a module requirement (via
proxy.golang.org) and installed, no version information is available; as
such 'cue version' simply shows 'custom'. This is the case when
installing cue as a tool dependency of a project or via gobin
(github.com/myitcv/gobin).

I have raised goreleaser/goreleaser#1354 to request that goreleaser add
support for building artefacts based on proxy.golang.org-resolved
modules, with module information for the main package's module baked in.
This will ensure consistency between published release artefacts and
anyone install cmd/cue as a tool dependency, for example.  But until
that support lands, we need to continue to support the existing ldflags
version injection approach.

Hence if there is no version information injected we fallback to using
module information if it is available.

Change-Id: Ifae33ae0cd56c353399593a759dc34c616b5d2f7
Reviewed-on: https://cue-review.googlesource.com/c/cue/+/5060
Reviewed-by: Marcel van Lohuizen <mpvl@golang.org>
diff --git a/cmd/cue/cmd/version.go b/cmd/cue/cmd/version.go
index be58c82..8af332e 100644
--- a/cmd/cue/cmd/version.go
+++ b/cmd/cue/cmd/version.go
@@ -17,6 +17,7 @@
 import (
 	"fmt"
 	goruntime "runtime"
+	"runtime/debug"
 
 	"github.com/spf13/cobra"
 )
@@ -31,14 +32,22 @@
 	return cmd
 }
 
+const (
+	defaultVersion = "devel"
+)
+
 // set by goreleaser or other builder using
 // -ldflags='-X cuelang.org/go/cmd/cue/cmd.version=<version>'
 var (
-	version = "custom"
+	version = defaultVersion
 )
 
 func runVersion(cmd *Command, args []string) error {
 	w := cmd.OutOrStdout()
+	if bi, ok := debug.ReadBuildInfo(); ok && version == defaultVersion {
+		// No specific version provided via version
+		version = bi.Main.Version
+	}
 	fmt.Fprintf(w, "cue version %v %s/%s\n",
 		version,
 		goruntime.GOOS, goruntime.GOARCH,