[#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
This commit is contained in:
Stanislav Egorov 2020-05-04 21:59:56 -07:00
parent d126615f8b
commit 5be10c5814
4 changed files with 45 additions and 5 deletions

View File

@ -1,6 +1,10 @@
SHELL := /bin/bash 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 '-extldflags "-static"' -tags=netgo
GO_FLAGS += -ldflags "-X ${GIT_MODULE}.gitVersion=${GIT_VERSION}"
BINDIR := bin BINDIR := bin
EXECUTABLE_CLI := airshipctl 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. # Used by gates after unit-tests and update-golden targets to ensure no files are deleted.
.PHONY: check-git-diff .PHONY: check-git-diff
check-git-diff: check-git-diff:
@./tools/git_diff_check @./tools/git_diff_check

View File

@ -1 +1 @@
airshipctl: v0.1.0 airshipctl: devel

View File

@ -20,9 +20,10 @@ import (
"github.com/spf13/cobra" "github.com/spf13/cobra"
"opendev.org/airship/airshipctl/pkg/util" "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 { func NewVersionCommand() *cobra.Command {
versionCmd := &cobra.Command{ versionCmd := &cobra.Command{
Use: "version", Use: "version",
@ -39,6 +40,6 @@ func NewVersionCommand() *cobra.Command {
} }
func clientVersion() string { func clientVersion() string {
// TODO(howell): There's gotta be a smarter way to do this v := version.Get()
return "v0.1.0" return v.GitVersion
} }

35
pkg/version/version.go Normal file
View File

@ -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,
}
}