From fb368a6d53f5c589ffed48db27522152e5233b85 Mon Sep 17 00:00:00 2001 From: Marc O'Morain Date: Tue, 17 Jul 2018 10:33:36 +0100 Subject: [PATCH] Add version command --- .goreleaser.yml | 2 ++ client/client.go | 3 ++- cmd/root.go | 1 + cmd/root_test.go | 2 +- cmd/version.go | 17 +++++++++++++++++ version/version.go | 9 +++++++++ 6 files changed, 32 insertions(+), 2 deletions(-) create mode 100644 cmd/version.go create mode 100644 version/version.go diff --git a/.goreleaser.yml b/.goreleaser.yml index aa8ecc4b8..671c25495 100644 --- a/.goreleaser.yml +++ b/.goreleaser.yml @@ -15,3 +15,5 @@ builds: - linux goarch: - amd64 + ldflags: + - -s -w -X version.Version={{.Version}} -X version.Commit={{.Commit}} diff --git a/client/client.go b/client/client.go index 3d15cc7df..7b29bc224 100644 --- a/client/client.go +++ b/client/client.go @@ -5,6 +5,7 @@ import ( "fmt" "github.com/CircleCI-Public/circleci-cli/logger" + "github.com/CircleCI-Public/circleci-cli/version" "github.com/machinebox/graphql" ) @@ -20,7 +21,6 @@ func NewClient(endpoint string, logger *logger.Logger) *graphql.Client { } return client - } // NewAuthorizedRequest returns a new GraphQL request with the @@ -28,6 +28,7 @@ func NewClient(endpoint string, logger *logger.Logger) *graphql.Client { func NewAuthorizedRequest(token, query string) *graphql.Request { req := graphql.NewRequest(query) req.Header.Set("Authorization", token) + req.Header.Set("User-Agent", fmt.Sprintf("circleci-cli/%s-%s", version.Version, version.Commit)) return req } diff --git a/cmd/root.go b/cmd/root.go index 64b9f0b52..4d1ba8372 100644 --- a/cmd/root.go +++ b/cmd/root.go @@ -47,6 +47,7 @@ func MakeCommands() *cobra.Command { rootCmd.AddCommand(newConfigCommand()) rootCmd.AddCommand(newOrbCommand()) rootCmd.AddCommand(newBuildCommand()) + rootCmd.AddCommand(newVersionCommand()) rootCmd.PersistentFlags().BoolP("verbose", "v", false, "Enable verbose logging.") rootCmd.PersistentFlags().StringP("endpoint", "e", defaultEndpoint, "the endpoint of your CircleCI GraphQL API") rootCmd.PersistentFlags().StringP("token", "t", "", "your token for using CircleCI") diff --git a/cmd/root_test.go b/cmd/root_test.go index 7df5eeadd..27d3bf0e3 100644 --- a/cmd/root_test.go +++ b/cmd/root_test.go @@ -16,7 +16,7 @@ var _ = Describe("Root", func() { It("can create commands", func() { commands := cmd.MakeCommands() - Expect(len(commands.Commands())).To(Equal(7)) + Expect(len(commands.Commands())).To(Equal(8)) }) }) diff --git a/cmd/version.go b/cmd/version.go new file mode 100644 index 000000000..544372372 --- /dev/null +++ b/cmd/version.go @@ -0,0 +1,17 @@ +package cmd + +import ( + "github.com/CircleCI-Public/circleci-cli/version" + "github.com/spf13/cobra" +) + +func newVersionCommand() *cobra.Command { + return &cobra.Command{ + Use: "version", + Short: "Display version information", + Run: func(cmd *cobra.Command, args []string) { + Logger.Infof("%s (%s)", version.Version, version.Commit) + }, + } + +} diff --git a/version/version.go b/version/version.go new file mode 100644 index 000000000..ba3bfd01e --- /dev/null +++ b/version/version.go @@ -0,0 +1,9 @@ +package version + +// These vars set by `goreleaser`: +var ( + // Version is the current Git tag (the v prefix is stripped) or the name of the snapshot, if you’re using the --snapshot flag + Version = "local-dev-build" + // Commit is the current git commit SHA + Commit = "dirty-local-tree" +)