Skip to content

Commit

Permalink
Add version command to print version (#60)
Browse files Browse the repository at this point in the history
  • Loading branch information
stormcat24 authored Nov 19, 2020
1 parent 801f6ba commit 2b9c395
Show file tree
Hide file tree
Showing 4 changed files with 83 additions and 4 deletions.
21 changes: 18 additions & 3 deletions Makefile
Original file line number Diff line number Diff line change
@@ -1,3 +1,10 @@
ROOT_PACKAGE := github.com/stormcat24/protodep
VERSION_PACKAGE := $(ROOT_PACKAGE)/version
LDFLAG_GIT_COMMIT := "$(VERSION_PACKAGE).gitCommit"
LDFLAG_GIT_COMMIT_FULL := "$(VERSION_PACKAGE).gitCommitFull"
LDFLAG_BUILD_DATE := "$(VERSION_PACKAGE).buildDate"
LDFLAG_VERSION := "$(VERSION_PACKAGE).version"

.PHONY: tidy
tidy:
GO111MODULE=on go mod tidy
Expand All @@ -10,11 +17,19 @@ APP := protodep
GOARCH := $(shell go env GOARCH)
GOOS := $(shell go env GOOS)

build: tidy vendor
GO111MODULE=on GOOS=$(GOOS) GOARCH=$(GOARCH) go build -ldflags="-w -s" -o bin/protodep -mod=vendor main.go
build: tidy vendor version
$(eval GIT_COMMIT := $(shell git describe --tags --always))
$(eval GIT_COMMIT_FULL := $(shell git rev-parse HEAD))
$(eval BUILD_DATE := $(shell date '+%Y%m%d'))
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)" \
-o bin/protodep -mod=vendor main.go

define build-artifact
GO111MODULE=on GOOS=$(1) GOARCH=$(2) go build -ldflags="-w -s" -o artifacts/$(3) -mod=vendor main.go
$(eval GIT_COMMIT := $(shell git describe --tags --always))
$(eval GIT_COMMIT_FULL := $(shell git rev-parse HEAD))
$(eval BUILD_DATE := $(shell date '+%Y%m%d'))
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)" \
-o artifacts/$(3) -mod=vendor main.go
cd artifacts && tar cvzf $(APP)_$(1)_$(2).tar.gz $(3)
rm ./artifacts/$(3)
@echo [INFO]build success: $(1)_$(2)
Expand Down
2 changes: 1 addition & 1 deletion cmd/init.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
package cmd

func init() {
RootCmd.AddCommand(upCmd)
RootCmd.AddCommand(upCmd, versionCmd)
initDepCmd()
}
28 changes: 28 additions & 0 deletions cmd/version.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
package cmd

import (
"fmt"
"github.com/spf13/cobra"

"github.com/stormcat24/protodep/version"
)

const art = `
________ ________ ________ _________ ________ ________ _______ ________
|\ __ \|\ __ \|\ __ \|\___ ___\\ __ \|\ ___ \|\ ___ \ |\ __ \
\ \ \|\ \ \ \|\ \ \ \|\ \|___ \ \_\ \ \|\ \ \ \_|\ \ \ __/|\ \ \|\ \
\ \ ____\ \ _ _\ \ \\\ \ \ \ \ \ \ \\\ \ \ \ \\ \ \ \_|/_\ \ ____\
\ \ \___|\ \ \\ \\ \ \\\ \ \ \ \ \ \ \\\ \ \ \_\\ \ \ \_|\ \ \ \___|
\ \__\ \ \__\\ _\\ \_______\ \ \__\ \ \_______\ \_______\ \_______\ \__\
\|__| \|__|\|__|\|_______| \|__| \|_______|\|_______|\|_______|\|__|`

var versionCmd = &cobra.Command{
Use: "version",
Short: "Show protodep version",
RunE: func(cdm *cobra.Command, args []string) error {
fmt.Println(art)
fmt.Println("")
fmt.Println(version.Get())
return nil
},
}
36 changes: 36 additions & 0 deletions version/version.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
package version

import "fmt"

var (
gitCommit = "unspecified"
gitCommitFull = "unspecified"
buildDate = "unspecified"
version = "unspecified"
)

type Info struct {
GitCommit string
GitCommitFull string
BuildDate string
Version string
}

func Get() Info {
return Info{
GitCommit: gitCommit,
GitCommitFull: gitCommitFull,
BuildDate: buildDate,
Version: version,
}
}

func (i Info) String() string {
return fmt.Sprintf(
`{"Version": "%s", "GitCommit": "%s", "GitCommitFull": "%s", "BuildDate": "%s"}`,
i.Version,
i.GitCommit,
i.GitCommitFull,
i.BuildDate,
)
}

0 comments on commit 2b9c395

Please sign in to comment.