From c8b11445da94fb727f1e9b72afb3ee379c9ce968 Mon Sep 17 00:00:00 2001 From: Martin Polden Date: Mon, 6 Sep 2021 14:18:44 +0200 Subject: Add version command --- client/go/Makefile | 7 ++++++- client/go/cmd/version.go | 21 +++++++++++++++++++++ client/go/cmd/version_test.go | 11 +++++++++++ client/go/vespa/version.go | 4 ++++ 4 files changed, 42 insertions(+), 1 deletion(-) create mode 100644 client/go/cmd/version.go create mode 100644 client/go/cmd/version_test.go create mode 100644 client/go/vespa/version.go (limited to 'client') diff --git a/client/go/Makefile b/client/go/Makefile index db16b495b74..abda68427e4 100644 --- a/client/go/Makefile +++ b/client/go/Makefile @@ -2,8 +2,13 @@ all: test checkfmt install +# When building a new release the build system should set the VERSION +# environment variable to version being built +VERSION ?= $(shell echo "0.0.0-`git rev-parse --short HEAD`") + install: - env GOBIN=$(PWD)/bin go install ./... + env GOBIN=$(PWD)/bin \ + go install -ldflags "-X github.com/vespa-engine/vespa/client/go/build.Version=$(VERSION)" ./... test: go test ./... diff --git a/client/go/cmd/version.go b/client/go/cmd/version.go new file mode 100644 index 00000000000..c9146fc1f92 --- /dev/null +++ b/client/go/cmd/version.go @@ -0,0 +1,21 @@ +package cmd + +import ( + "log" + + "github.com/spf13/cobra" + "github.com/vespa-engine/vespa/client/go/build" +) + +func init() { + rootCmd.AddCommand(versionCmd) +} + +var versionCmd = &cobra.Command{ + Use: "version", + Short: "Show version number", + Args: cobra.ExactArgs(0), + Run: func(cmd *cobra.Command, args []string) { + log.Print("vespa version ", build.Version) + }, +} diff --git a/client/go/cmd/version_test.go b/client/go/cmd/version_test.go new file mode 100644 index 00000000000..02303a08e21 --- /dev/null +++ b/client/go/cmd/version_test.go @@ -0,0 +1,11 @@ +package cmd + +import ( + "testing" + + "github.com/stretchr/testify/assert" +) + +func TestVersion(t *testing.T) { + assert.Equal(t, "vespa version 0.0.0-devel\n", execute(command{args: []string{"version"}}, t, nil)) +} diff --git a/client/go/vespa/version.go b/client/go/vespa/version.go new file mode 100644 index 00000000000..e5a89f4cb58 --- /dev/null +++ b/client/go/vespa/version.go @@ -0,0 +1,4 @@ +package vespa + +// Version is the Vespa CLI version number +var Version string = "0.0.0-devel" // Overriden by linker flag as part of build -- cgit v1.2.3 From 420e48dcf6acf12ee18c28a73c4ddf9f8f05af77 Mon Sep 17 00:00:00 2001 From: Martin Polden Date: Mon, 6 Sep 2021 14:56:21 +0200 Subject: Set user agent --- client/go/util/http.go | 6 ++++++ 1 file changed, 6 insertions(+) (limited to 'client') diff --git a/client/go/util/http.go b/client/go/util/http.go index 4e524fa2d79..a8e2dbc2195 100644 --- a/client/go/util/http.go +++ b/client/go/util/http.go @@ -10,6 +10,8 @@ import ( "net/http" "net/url" "time" + + "github.com/vespa-engine/vespa/client/go/build" ) // Set this to a mock HttpClient instead to unit test HTTP requests @@ -53,6 +55,10 @@ func HttpGet(host string, path string, description string) (*http.Response, erro } func HttpDo(request *http.Request, timeout time.Duration, description string) (*http.Response, error) { + if request.Header == nil { + request.Header = make(http.Header) + } + request.Header.Set("User-Agent", fmt.Sprintf("Vespa CLI/%s", build.Version)) response, err := ActiveHttpClient.Do(request, timeout) if err != nil { return nil, err -- cgit v1.2.3