Skip to content

Commit 6d1e1a8

Browse files
author
Deepak Sharma
authored
chore: move version params to a package (#45)
* chore: move version to a package * releaser * reducing redirection * pkgs -> pkg
1 parent 97dfad1 commit 6d1e1a8

File tree

4 files changed

+57
-44
lines changed

4 files changed

+57
-44
lines changed

.goreleaser.yml

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -23,10 +23,10 @@ builds:
2323
- CGO_ENABLED=0
2424
main: ./main.go
2525
ldflags:
26-
- -s -w -X github.com/fabric8-analytics/cli-tools/cmd.Version={{.Version}}
27-
- "-X 'github.com/fabric8-analytics/cli-tools/cmd.VendorInfo=Red Hat'"
28-
- -X github.com/fabric8-analytics/cli-tools/cmd.Timestamp={{ .Timestamp }}
29-
- -X github.com/fabric8-analytics/cli-tools/cmd.CommitHash={{ .ShortCommit }}
26+
- -s -w -X github.com/fabric8-analytics/cli-tools/pkg/version.version={{.Version}}
27+
- "-X 'github.com/fabric8-analytics/cli-tools/pkg/version.vendorInfo=Red Hat'"
28+
- -X github.com/fabric8-analytics/cli-tools/pkg/version.timestamp={{ .Timestamp }}
29+
- -X github.com/fabric8-analytics/cli-tools/pkg/version.commitHash={{ .ShortCommit }}
3030
id: crda
3131
binary: crda
3232
goos:

Makefile

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,9 +7,9 @@ REPO=github.com/fabric8-analytics/cli-tools
77
VERSION?=0.0.1
88
EXPORT_RESULT?=false
99
CGO_ENABLED:=0
10-
LDFLAGS +=-X ${REPO}/cmd.Timestamp=$(shell date +%s)
11-
LDFLAGS +=-X ${REPO}/cmd.Version=${VERSION}
12-
LDFLAGS +=-X ${REPO}/cmd.CommitHash=${GITCOMMIT}
10+
LDFLAGS +=-X ${REPO}/pkg/version.timestamp=$(shell date +%s)
11+
LDFLAGS +=-X ${REPO}/pkg/version.version=${VERSION}
12+
LDFLAGS +=-X ${REPO}/pkg/version.commitHash=${GITCOMMIT}
1313

1414
GREEN := $(shell tput -Txterm setaf 2)
1515
YELLOW := $(shell tput -Txterm setaf 3)

cmd/version.go

Lines changed: 4 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,8 @@ package cmd
22

33
import (
44
"fmt"
5-
"runtime"
6-
"strconv"
7-
"time"
85

6+
"github.com/fabric8-analytics/cli-tools/pkg/version"
97
"github.com/spf13/cobra"
108
)
119

@@ -17,42 +15,11 @@ var versionCmd = &cobra.Command{
1715
Run: getVersion,
1816
}
1917

20-
// Version string populated by releaser Job
21-
var (
22-
Version string = "0.0.0"
23-
// commitHash contains the current Git revision.
24-
CommitHash string = "abcd"
25-
// Timestamp contains the UnixTimestamp of the binary build.
26-
Timestamp string = "0"
27-
// VendorInfo contains vendor notes about the current build.
28-
VendorInfo string = "Local Build"
29-
)
30-
3118
func init() {
3219
rootCmd.AddCommand(versionCmd)
3320
}
3421

35-
func getVersion(cmd *cobra.Command, args []string) {
36-
37-
version := "v" + Version
38-
if CommitHash != "" {
39-
version += "-" + CommitHash
40-
}
41-
osArch := runtime.GOOS + "/" + runtime.GOARCH
42-
43-
versionString := fmt.Sprintf("%s %s ",
44-
version, osArch)
45-
46-
if Timestamp != "" {
47-
i, err := strconv.ParseInt(Timestamp, 10, 64)
48-
if err == nil {
49-
tm := time.Unix(i, 0)
50-
versionString += fmt.Sprintf(" BuildDate: %s", tm.Format(time.RFC1123))
51-
}
52-
}
53-
if VendorInfo != "" {
54-
versionString += fmt.Sprintf(" Vendor: %s", VendorInfo)
55-
}
56-
57-
fmt.Println(versionString)
22+
func getVersion(_ *cobra.Command, args []string) {
23+
version := version.BuildVersion()
24+
fmt.Println(version)
5825
}

pkg/version/version.go

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
package version
2+
3+
import (
4+
"fmt"
5+
"runtime"
6+
"strconv"
7+
"time"
8+
)
9+
10+
// Version string populated by releaser Job
11+
var (
12+
// The current version of crda cli
13+
version string = "0.0.0"
14+
15+
// commitHash contains the current Git revision.
16+
commitHash string = "abcd"
17+
18+
// Timestamp contains the UnixTimestamp of the binary build.
19+
timestamp string = "0"
20+
21+
// VendorInfo contains vendor notes about the current build.
22+
vendorInfo string = "Local Build"
23+
)
24+
25+
// GetCRDAVersion returns CRDA CLI Version
26+
func GetCRDAVersion() string {
27+
return version
28+
}
29+
30+
// BuildVersion builds version string for command output
31+
func BuildVersion() string {
32+
version := "v" + version
33+
version += "-" + commitHash
34+
osArch := runtime.GOOS + "/" + runtime.GOARCH
35+
36+
versionString := fmt.Sprintf("%s %s ",
37+
version, osArch)
38+
39+
i, err := strconv.ParseInt(timestamp, 10, 64)
40+
if err == nil {
41+
tm := time.Unix(i, 0)
42+
versionString += fmt.Sprintf(" BuildDate: %s", tm.Format(time.RFC1123))
43+
}
44+
versionString += fmt.Sprintf(" Vendor: %s", vendorInfo)
45+
return versionString
46+
}

0 commit comments

Comments
 (0)