From 6d1e1a8f0bd904bdb020844154858772d25bace1 Mon Sep 17 00:00:00 2001 From: Deepak Sharma Date: Thu, 8 Apr 2021 17:21:15 +0530 Subject: [PATCH] chore: move version params to a package (#45) * chore: move version to a package * releaser * reducing redirection * pkgs -> pkg --- .goreleaser.yml | 8 ++++---- Makefile | 6 +++--- cmd/version.go | 41 ++++--------------------------------- pkg/version/version.go | 46 ++++++++++++++++++++++++++++++++++++++++++ 4 files changed, 57 insertions(+), 44 deletions(-) create mode 100644 pkg/version/version.go diff --git a/.goreleaser.yml b/.goreleaser.yml index 4ebae02..c80c512 100644 --- a/.goreleaser.yml +++ b/.goreleaser.yml @@ -23,10 +23,10 @@ builds: - CGO_ENABLED=0 main: ./main.go ldflags: - - -s -w -X github.com/fabric8-analytics/cli-tools/cmd.Version={{.Version}} - - "-X 'github.com/fabric8-analytics/cli-tools/cmd.VendorInfo=Red Hat'" - - -X github.com/fabric8-analytics/cli-tools/cmd.Timestamp={{ .Timestamp }} - - -X github.com/fabric8-analytics/cli-tools/cmd.CommitHash={{ .ShortCommit }} + - -s -w -X github.com/fabric8-analytics/cli-tools/pkg/version.version={{.Version}} + - "-X 'github.com/fabric8-analytics/cli-tools/pkg/version.vendorInfo=Red Hat'" + - -X github.com/fabric8-analytics/cli-tools/pkg/version.timestamp={{ .Timestamp }} + - -X github.com/fabric8-analytics/cli-tools/pkg/version.commitHash={{ .ShortCommit }} id: crda binary: crda goos: diff --git a/Makefile b/Makefile index 569cb29..8248910 100644 --- a/Makefile +++ b/Makefile @@ -7,9 +7,9 @@ REPO=github.com/fabric8-analytics/cli-tools VERSION?=0.0.1 EXPORT_RESULT?=false CGO_ENABLED:=0 -LDFLAGS +=-X ${REPO}/cmd.Timestamp=$(shell date +%s) -LDFLAGS +=-X ${REPO}/cmd.Version=${VERSION} -LDFLAGS +=-X ${REPO}/cmd.CommitHash=${GITCOMMIT} +LDFLAGS +=-X ${REPO}/pkg/version.timestamp=$(shell date +%s) +LDFLAGS +=-X ${REPO}/pkg/version.version=${VERSION} +LDFLAGS +=-X ${REPO}/pkg/version.commitHash=${GITCOMMIT} GREEN := $(shell tput -Txterm setaf 2) YELLOW := $(shell tput -Txterm setaf 3) diff --git a/cmd/version.go b/cmd/version.go index d76473f..4656999 100644 --- a/cmd/version.go +++ b/cmd/version.go @@ -2,10 +2,8 @@ package cmd import ( "fmt" - "runtime" - "strconv" - "time" + "github.com/fabric8-analytics/cli-tools/pkg/version" "github.com/spf13/cobra" ) @@ -17,42 +15,11 @@ var versionCmd = &cobra.Command{ Run: getVersion, } -// Version string populated by releaser Job -var ( - Version string = "0.0.0" - // commitHash contains the current Git revision. - CommitHash string = "abcd" - // Timestamp contains the UnixTimestamp of the binary build. - Timestamp string = "0" - // VendorInfo contains vendor notes about the current build. - VendorInfo string = "Local Build" -) - func init() { rootCmd.AddCommand(versionCmd) } -func getVersion(cmd *cobra.Command, args []string) { - - version := "v" + Version - if CommitHash != "" { - version += "-" + CommitHash - } - osArch := runtime.GOOS + "/" + runtime.GOARCH - - versionString := fmt.Sprintf("%s %s ", - version, osArch) - - if Timestamp != "" { - i, err := strconv.ParseInt(Timestamp, 10, 64) - if err == nil { - tm := time.Unix(i, 0) - versionString += fmt.Sprintf(" BuildDate: %s", tm.Format(time.RFC1123)) - } - } - if VendorInfo != "" { - versionString += fmt.Sprintf(" Vendor: %s", VendorInfo) - } - - fmt.Println(versionString) +func getVersion(_ *cobra.Command, args []string) { + version := version.BuildVersion() + fmt.Println(version) } diff --git a/pkg/version/version.go b/pkg/version/version.go new file mode 100644 index 0000000..3b63aac --- /dev/null +++ b/pkg/version/version.go @@ -0,0 +1,46 @@ +package version + +import ( + "fmt" + "runtime" + "strconv" + "time" +) + +// Version string populated by releaser Job +var ( + // The current version of crda cli + version string = "0.0.0" + + // commitHash contains the current Git revision. + commitHash string = "abcd" + + // Timestamp contains the UnixTimestamp of the binary build. + timestamp string = "0" + + // VendorInfo contains vendor notes about the current build. + vendorInfo string = "Local Build" +) + +// GetCRDAVersion returns CRDA CLI Version +func GetCRDAVersion() string { + return version +} + +// BuildVersion builds version string for command output +func BuildVersion() string { + version := "v" + version + version += "-" + commitHash + osArch := runtime.GOOS + "/" + runtime.GOARCH + + versionString := fmt.Sprintf("%s %s ", + version, osArch) + + i, err := strconv.ParseInt(timestamp, 10, 64) + if err == nil { + tm := time.Unix(i, 0) + versionString += fmt.Sprintf(" BuildDate: %s", tm.Format(time.RFC1123)) + } + versionString += fmt.Sprintf(" Vendor: %s", vendorInfo) + return versionString +}