diff --git a/Makefile b/Makefile index 839d8beed..a69365e62 100644 --- a/Makefile +++ b/Makefile @@ -1,3 +1,7 @@ +REPO_PATH := gitlab.ricebook.net/platform/core +REVISION := $(shell git rev-parse HEAD || unknown) +GO_LDFLAGS ?= -s -X $(REPO_PATH)/version.REVISION=$(REVISION) + golang: cd ./rpc/gen/; protoc --go_out=plugins=grpc:. core.proto @@ -18,3 +22,6 @@ deps: go get gopkg.in/libgit2/git2go.v23 go get golang.org/x/net/context go get google.golang.org/grpc + +build: + go build -ldflags "$(GO_LDFLAGS)" -a -tags netgo -installsuffix netgo -o eru-core diff --git a/main.go b/main.go index 435927600..76027186f 100644 --- a/main.go +++ b/main.go @@ -1,6 +1,7 @@ package main import ( + "fmt" "io/ioutil" "net" "net/http" @@ -15,6 +16,7 @@ import ( "gitlab.ricebook.net/platform/core/rpc" "gitlab.ricebook.net/platform/core/rpc/gen" "gitlab.ricebook.net/platform/core/types" + "gitlab.ricebook.net/platform/core/version" "google.golang.org/grpc" "gopkg.in/yaml.v2" ) @@ -98,10 +100,14 @@ func serve() { } func main() { + cli.VersionPrinter = func(c *cli.Context) { + fmt.Print(version.VersionString()) + } + app := cli.NewApp() - app.Name = "Eru-Core" + app.Name = version.NAME app.Usage = "Run eru core" - app.Version = "2.0.0" + app.Version = version.VERSION app.Flags = []cli.Flag{ cli.StringFlag{ Name: "config", diff --git a/version/version.go b/version/version.go new file mode 100644 index 000000000..6fabc505f --- /dev/null +++ b/version/version.go @@ -0,0 +1,24 @@ +package version + +import ( + "fmt" + "runtime" + "time" +) + +var ( + NAME = "Eru-Core" + VERSION = "1.0.0" + REVISION = "HEAD" +) + +func VersionString() string { + build := time.Now() + // 暂时隐藏吧, 也不知道按啥version发布的好 + // version := fmt.Sprintf("Version: %s\n", VERSION) + version := fmt.Sprintf("Git hash: %s\n", REVISION) + version += fmt.Sprintf("Built: %s\n", build.Format(time.RFC1123Z)) + version += fmt.Sprintf("Golang version: %s\n", runtime.Version()) + version += fmt.Sprintf("OS/Arch: %s/%s\n", runtime.GOOS, runtime.GOARCH) + return version +}