Skip to content

Commit ea47987

Browse files
committed
feat: make version info generating automatically
1 parent f093d71 commit ea47987

3 files changed

Lines changed: 44 additions & 31 deletions

File tree

Makefile

Lines changed: 24 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,23 +1,43 @@
1+
GOPATH ?= $(shell go env GOPATH)
2+
3+
# Ensure GOPATH is set before running build process.
4+
ifeq "$(GOPATH)" ""
5+
$(error Please set the environment variable GOPATH before running `make`)
6+
endif
7+
8+
9+
GO := go
10+
GOBUILD := CGO_ENABLED=0 $(GO) build $(BUILD_FLAG)
11+
12+
13+
LDFLAGS += -X "github.com/moooofly/harbor-go-client/utils.ClientVersion=$(shell cat VERSION)"
14+
LDFLAGS += -X "github.com/moooofly/harbor-go-client/utils.GoVersion=$(shell go version)"
15+
LDFLAGS += -X "github.com/moooofly/harbor-go-client/utils.UTCBuildTime=$(shell date -u '+%Y-%m-%d %I:%M:%S')"
16+
LDFLAGS += -X "github.com/moooofly/harbor-go-client/utils.GitBranch=$(shell git rev-parse --abbrev-ref HEAD)"
17+
LDFLAGS += -X "github.com/moooofly/harbor-go-client/utils.GitTag=$(shell git describe --tags)"
18+
LDFLAGS += -X "github.com/moooofly/harbor-go-client/utils.GitHash=$(shell git rev-parse HEAD)"
19+
120
all: lint build test
221

322
build:
4-
go build -x ./
23+
$(GOBUILD) -ldflags '$(LDFLAGS)' ./
524

625
install:
7-
go install -x ${SRC}
26+
$(GO) install -x ${SRC}
827

928
lint:
1029
gometalinter --exclude=vendor --disable-all --enable=golint --enable=vet --enable=gofmt --enable=misspell ./...
1130
find . -name '*.go' -not -path "./vendor/*" | xargs gofmt -w -s
1231

1332
test:
14-
go test ${SRC}
33+
$(GO) test ${SRC}
1534

1635
misspell:
1736
# misspell - requires that the following be run first:
1837
# go get -u github.com/client9/misspell/cmd/misspell
1938
find . -name '*.go' -not -path './vendor/*' -not -path './_repos/*' | xargs misspell -error
2039

2140
clean:
22-
go clean -x -i ${SRC}
41+
$(GO) clean -x -i ${SRC}
42+
rm -rf *.out
2343

utils/utils.go

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,16 @@ import (
1414
yaml "gopkg.in/yaml.v2"
1515
)
1616

17+
// These variables are populated via the Go linker.
18+
var (
19+
UTCBuildTime = "unknown"
20+
ClientVersion = "unknown"
21+
GoVersion = "unknown"
22+
GitBranch = "unknown"
23+
GitTag = "unknown"
24+
GitHash = "unknown"
25+
)
26+
1727
var errMalCookies = errors.New("get malformed cookies")
1828
var errCookiesNotAvailable = errors.New("target cookies are not available")
1929

@@ -26,7 +36,6 @@ var Request = gorequest.New().TLSClientConfig(&tls.Config{InsecureSkipVerify: tr
2636

2737
var configfile = "conf/config.yaml"
2838
var secretfile = "conf/.cookie.yaml"
29-
var versionfile = "./VERSION"
3039

3140
// Beegocookie is for beegosessionID storage
3241
type Beegocookie struct {

utils/version.go

Lines changed: 10 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,13 @@
11
package utils
22

33
import (
4-
"bufio"
54
"fmt"
6-
"os"
7-
"runtime"
8-
"strings"
95
)
106

117
func init() {
128
Parser.AddCommand("version",
139
"Show version info.",
14-
"Format: <client-version> (<golang-runtime-version> on <GOOS>/<GOARCH>; <Compiler>)",
10+
"Show version infos as \"| Type | Value |\"",
1511
&verinfo)
1612
}
1713

@@ -25,27 +21,15 @@ func (x *verInfo) Execute(args []string) error {
2521
return nil
2622
}
2723

28-
func version() string {
29-
30-
f, err := os.Open(versionfile)
31-
if err != nil {
32-
fmt.Println("error:", err)
33-
return ""
34-
}
35-
defer f.Close()
36-
37-
reader := bufio.NewReader(f)
38-
v, err := reader.ReadString('\n')
39-
if err != nil {
40-
fmt.Println("error:", err)
41-
return ""
42-
}
43-
44-
return fmt.Sprintf("%s (%s on %s/%s; %s)",
45-
strings.Trim(v, "\n"), runtime.Version(), runtime.GOOS, runtime.GOARCH, runtime.Compiler)
46-
}
47-
4824
// PrintVersion print version info.
4925
func PrintVersion() {
50-
fmt.Println(version())
26+
PrintLogo()
27+
fmt.Println("+----------------------+------------------------------------------+")
28+
fmt.Printf("| % -20s | % -40s |\n", "Client Version", ClientVersion)
29+
fmt.Printf("| % -20s | % -40s |\n", "Go Version", GoVersion)
30+
fmt.Printf("| % -20s | % -40s |\n", "UTC Build Time", UTCBuildTime)
31+
fmt.Printf("| % -20s | % -40s |\n", "Git Branch", GitBranch)
32+
fmt.Printf("| % -20s | % -40s |\n", "Git Tag", GitTag)
33+
fmt.Printf("| % -20s | % -40s |\n", "Git Hash", GitHash)
34+
fmt.Println("+----------------------+------------------------------------------+")
5135
}

0 commit comments

Comments
 (0)