Skip to content

Commit

Permalink
chore: move version params to a package (#45)
Browse files Browse the repository at this point in the history
* chore: move version to a package

* releaser

* reducing redirection

* pkgs -> pkg
  • Loading branch information
Deepak Sharma authored Apr 8, 2021
1 parent 97dfad1 commit 6d1e1a8
Show file tree
Hide file tree
Showing 4 changed files with 57 additions and 44 deletions.
8 changes: 4 additions & 4 deletions .goreleaser.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down
6 changes: 3 additions & 3 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
41 changes: 4 additions & 37 deletions cmd/version.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,8 @@ package cmd

import (
"fmt"
"runtime"
"strconv"
"time"

"github.com/fabric8-analytics/cli-tools/pkg/version"
"github.com/spf13/cobra"
)

Expand All @@ -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)
}
46 changes: 46 additions & 0 deletions pkg/version/version.go
Original file line number Diff line number Diff line change
@@ -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
}

0 comments on commit 6d1e1a8

Please sign in to comment.