From 5be10c58140c1675195d1a05a5ed7d5865596a61 Mon Sep 17 00:00:00 2001 From: Stanislav Egorov Date: Mon, 4 May 2020 21:59:56 -0700 Subject: [PATCH] [#204] Refactoring for version cmd Now the version can be changed from Makefile/env. By default version is 'devel' if there is no ldflags in build args. It can be overriden by providing ldflags in Makefile. Change-Id: Ifef5932ec953ccfaeca8a2ef58eb951c900c7a59 --- Makefile | 6 +++- .../TestVersionGoldenOutput/version.golden | 2 +- cmd/version.go | 7 ++-- pkg/version/version.go | 35 +++++++++++++++++++ 4 files changed, 45 insertions(+), 5 deletions(-) create mode 100644 pkg/version/version.go diff --git a/Makefile b/Makefile index a96d6b9f9..d19d2ae76 100644 --- a/Makefile +++ b/Makefile @@ -1,6 +1,10 @@ SHELL := /bin/bash +GIT_VERSION ?= v0.1.0 +GIT_MODULE ?= opendev.org/airship/airshipctl/pkg/version + GO_FLAGS := -ldflags '-extldflags "-static"' -tags=netgo +GO_FLAGS += -ldflags "-X ${GIT_MODULE}.gitVersion=${GIT_VERSION}" BINDIR := bin EXECUTABLE_CLI := airshipctl @@ -185,4 +189,4 @@ delete-golden: # Used by gates after unit-tests and update-golden targets to ensure no files are deleted. .PHONY: check-git-diff check-git-diff: - @./tools/git_diff_check \ No newline at end of file + @./tools/git_diff_check diff --git a/cmd/testdata/TestVersionGoldenOutput/version.golden b/cmd/testdata/TestVersionGoldenOutput/version.golden index 5316bfd0e..21d06811c 100644 --- a/cmd/testdata/TestVersionGoldenOutput/version.golden +++ b/cmd/testdata/TestVersionGoldenOutput/version.golden @@ -1 +1 @@ -airshipctl: v0.1.0 +airshipctl: devel diff --git a/cmd/version.go b/cmd/version.go index ca7d8e313..68ec1db87 100644 --- a/cmd/version.go +++ b/cmd/version.go @@ -20,9 +20,10 @@ import ( "github.com/spf13/cobra" "opendev.org/airship/airshipctl/pkg/util" + "opendev.org/airship/airshipctl/pkg/version" ) -// NewVersionCommand creates a command for displaying the version of airshipctl. +// NewVersionCommand creates a command for displaying the version of airshipctl func NewVersionCommand() *cobra.Command { versionCmd := &cobra.Command{ Use: "version", @@ -39,6 +40,6 @@ func NewVersionCommand() *cobra.Command { } func clientVersion() string { - // TODO(howell): There's gotta be a smarter way to do this - return "v0.1.0" + v := version.Get() + return v.GitVersion } diff --git a/pkg/version/version.go b/pkg/version/version.go new file mode 100644 index 000000000..44af23d7f --- /dev/null +++ b/pkg/version/version.go @@ -0,0 +1,35 @@ +/* + 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 + + https://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 version + +var ( + gitVersion = "devel" +) + +// Info structure provides version data for airshipctl +// For now it has GitVersion with format 'v0.1.0' +// provided from Makefile during building airshipctl +// or defined in a local var for development purposes +type Info struct { + GitVersion string `json:"gitVersion"` +} + +// Get function shows airshipctl version +// returns filled Info structure +func Get() Info { + return Info{ + GitVersion: gitVersion, + } +}