Skip to content

Commit 2b9c395

Browse files
authored
Add version command to print version (#60)
1 parent 801f6ba commit 2b9c395

File tree

4 files changed

+83
-4
lines changed

4 files changed

+83
-4
lines changed

Makefile

Lines changed: 18 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,10 @@
1+
ROOT_PACKAGE := github.com/stormcat24/protodep
2+
VERSION_PACKAGE := $(ROOT_PACKAGE)/version
3+
LDFLAG_GIT_COMMIT := "$(VERSION_PACKAGE).gitCommit"
4+
LDFLAG_GIT_COMMIT_FULL := "$(VERSION_PACKAGE).gitCommitFull"
5+
LDFLAG_BUILD_DATE := "$(VERSION_PACKAGE).buildDate"
6+
LDFLAG_VERSION := "$(VERSION_PACKAGE).version"
7+
18
.PHONY: tidy
29
tidy:
310
GO111MODULE=on go mod tidy
@@ -10,11 +17,19 @@ APP := protodep
1017
GOARCH := $(shell go env GOARCH)
1118
GOOS := $(shell go env GOOS)
1219

13-
build: tidy vendor
14-
GO111MODULE=on GOOS=$(GOOS) GOARCH=$(GOARCH) go build -ldflags="-w -s" -o bin/protodep -mod=vendor main.go
20+
build: tidy vendor version
21+
$(eval GIT_COMMIT := $(shell git describe --tags --always))
22+
$(eval GIT_COMMIT_FULL := $(shell git rev-parse HEAD))
23+
$(eval BUILD_DATE := $(shell date '+%Y%m%d'))
24+
GO111MODULE=on GOOS=$(GOOS) GOARCH=$(GOARCH) go build -ldflags="-w -s -X $(LDFLAG_GIT_COMMIT)=$(GIT_COMMIT) -X $(LDFLAG_GIT_COMMIT_FULL)=$(GIT_COMMIT_FULL) -X $(LDFLAG_BUILD_DATE)=$(BUILD_DATE) -X $(LDFLAG_VERSION)=$(GIT_COMMIT)" \
25+
-o bin/protodep -mod=vendor main.go
1526

1627
define build-artifact
17-
GO111MODULE=on GOOS=$(1) GOARCH=$(2) go build -ldflags="-w -s" -o artifacts/$(3) -mod=vendor main.go
28+
$(eval GIT_COMMIT := $(shell git describe --tags --always))
29+
$(eval GIT_COMMIT_FULL := $(shell git rev-parse HEAD))
30+
$(eval BUILD_DATE := $(shell date '+%Y%m%d'))
31+
GO111MODULE=on GOOS=$(1) GOARCH=$(2) go build -ldflags="-w -s -X $(LDFLAG_GIT_COMMIT)=$(GIT_COMMIT) -X $(LDFLAG_GIT_COMMIT_FULL)=$(GIT_COMMIT_FULL) -X $(LDFLAG_BUILD_DATE)=$(BUILD_DATE) -X $(LDFLAG_VERSION)=$(BUILD_DATE)-$(GIT_COMMIT)" \
32+
-o artifacts/$(3) -mod=vendor main.go
1833
cd artifacts && tar cvzf $(APP)_$(1)_$(2).tar.gz $(3)
1934
rm ./artifacts/$(3)
2035
@echo [INFO]build success: $(1)_$(2)

cmd/init.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
package cmd
22

33
func init() {
4-
RootCmd.AddCommand(upCmd)
4+
RootCmd.AddCommand(upCmd, versionCmd)
55
initDepCmd()
66
}

cmd/version.go

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
package cmd
2+
3+
import (
4+
"fmt"
5+
"github.com/spf13/cobra"
6+
7+
"github.com/stormcat24/protodep/version"
8+
)
9+
10+
const art = `
11+
________ ________ ________ _________ ________ ________ _______ ________
12+
|\ __ \|\ __ \|\ __ \|\___ ___\\ __ \|\ ___ \|\ ___ \ |\ __ \
13+
\ \ \|\ \ \ \|\ \ \ \|\ \|___ \ \_\ \ \|\ \ \ \_|\ \ \ __/|\ \ \|\ \
14+
\ \ ____\ \ _ _\ \ \\\ \ \ \ \ \ \ \\\ \ \ \ \\ \ \ \_|/_\ \ ____\
15+
\ \ \___|\ \ \\ \\ \ \\\ \ \ \ \ \ \ \\\ \ \ \_\\ \ \ \_|\ \ \ \___|
16+
\ \__\ \ \__\\ _\\ \_______\ \ \__\ \ \_______\ \_______\ \_______\ \__\
17+
\|__| \|__|\|__|\|_______| \|__| \|_______|\|_______|\|_______|\|__|`
18+
19+
var versionCmd = &cobra.Command{
20+
Use: "version",
21+
Short: "Show protodep version",
22+
RunE: func(cdm *cobra.Command, args []string) error {
23+
fmt.Println(art)
24+
fmt.Println("")
25+
fmt.Println(version.Get())
26+
return nil
27+
},
28+
}

version/version.go

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
package version
2+
3+
import "fmt"
4+
5+
var (
6+
gitCommit = "unspecified"
7+
gitCommitFull = "unspecified"
8+
buildDate = "unspecified"
9+
version = "unspecified"
10+
)
11+
12+
type Info struct {
13+
GitCommit string
14+
GitCommitFull string
15+
BuildDate string
16+
Version string
17+
}
18+
19+
func Get() Info {
20+
return Info{
21+
GitCommit: gitCommit,
22+
GitCommitFull: gitCommitFull,
23+
BuildDate: buildDate,
24+
Version: version,
25+
}
26+
}
27+
28+
func (i Info) String() string {
29+
return fmt.Sprintf(
30+
`{"Version": "%s", "GitCommit": "%s", "GitCommitFull": "%s", "BuildDate": "%s"}`,
31+
i.Version,
32+
i.GitCommit,
33+
i.GitCommitFull,
34+
i.BuildDate,
35+
)
36+
}

0 commit comments

Comments
 (0)