From e35c061fb5834adc75d752e12f3dc204f21bf455 Mon Sep 17 00:00:00 2001 From: terrytangyuan Date: Thu, 2 Jun 2022 22:51:15 -0500 Subject: [PATCH 1/3] feat: Add version command with enriched information Signed-off-by: terrytangyuan --- Makefile | 8 +++-- cmd/envd-ssh/main.go | 6 ++-- cmd/envd/main.go | 3 +- cmd/envd/version.go | 43 +++++++++++++++++++++++++++ pkg/version/version.go | 66 +++++++++++++++++++++++++++++++++++++----- 5 files changed, 112 insertions(+), 14 deletions(-) create mode 100644 cmd/envd/version.go diff --git a/Makefile b/Makefile index bcc25780a..a63f0e27a 100644 --- a/Makefile +++ b/Makefile @@ -68,7 +68,11 @@ DEBUG_DIR := ./debug-bin BUILD_DIR := ./build # Current version of the project. -VERSION ?= $(shell git describe --match 'v[0-9]*' --dirty='.m' --always) +VERSION ?= $(shell git describe --match 'v[0-9]*' --always) +BUILD_DATE=$(shell date -u +'%Y-%m-%dT%H:%M:%SZ') +GIT_COMMIT=$(shell git rev-parse HEAD) +GIT_TAG=$(shell if [ -z "`git status --porcelain`" ]; then git describe --exact-match --tags HEAD 2>/dev/null; fi) +GIT_TREE_STATE=$(shell if [ -z "`git status --porcelain`" ]; then echo "clean" ; else echo "dirty"; fi) GITSHA ?= $(shell git rev-parse --short HEAD) # Track code version with Docker Label. @@ -113,7 +117,7 @@ addlicense-install: build-local: @for target in $(TARGETS); do \ CGO_ENABLED=$(CGO_ENABLED) go build -trimpath -v -o $(OUTPUT_DIR)/$${target} \ - -ldflags "-s -w -X $(ROOT)/pkg/version.Version=$(VERSION)" \ + -ldflags "-s -w -X $(ROOT)/pkg/version.version=$(VERSION) -X $(ROOT)/pkg/version.buildDate=$(BUILD_DATE) -X $(ROOT)/pkg/version.gitCommit=$(GIT_COMMIT) -X $(ROOT)/pkg/version.gitTreeState=$(GIT_TREE_STATE)" \ $(CMD_DIR)/$${target}; \ done diff --git a/cmd/envd-ssh/main.go b/cmd/envd-ssh/main.go index 8d67efcb7..e3016c318 100644 --- a/cmd/envd-ssh/main.go +++ b/cmd/envd-ssh/main.go @@ -41,13 +41,13 @@ const ( func main() { cli.VersionPrinter = func(c *cli.Context) { - fmt.Println(c.App.Name, version.Package, c.App.Version, version.Revision) + fmt.Println(c.App.Name, version.Package, version.GetVersion().String()) } app := cli.NewApp() app.Name = "envd-ssh" app.Usage = "ssh server for envd" - app.Version = version.Version + app.Version = version.GetVersion().String() app.Flags = []cli.Flag{ &cli.BoolFlag{ Name: flagDebug, @@ -128,7 +128,7 @@ func sshServer(c *cli.Context) error { AuthorizedKeys: keys, } - logrus.Infof("ssh server %s started in 0.0.0.0:%d", version.Version, srv.Port) + logrus.Infof("ssh server %s started in 0.0.0.0:%d", version.GetVersion().String(), srv.Port) return srv.ListenAndServe() } diff --git a/cmd/envd/main.go b/cmd/envd/main.go index 3af556087..1cc910e51 100644 --- a/cmd/envd/main.go +++ b/cmd/envd/main.go @@ -43,7 +43,7 @@ func run(args []string) (bool, error) { app.EnableBashCompletion = true app.Name = "envd" app.Usage = "Build tools for data scientists" - app.Version = version.Version + app.Version = version.GetVersion().String() app.Flags = []cli.Flag{ &cli.BoolFlag{ Name: "debug", @@ -69,6 +69,7 @@ func run(args []string) (bool, error) { CommandPause, CommandResume, CommandUp, + CommandVersion, } // Deal with debug flag. diff --git a/cmd/envd/version.go b/cmd/envd/version.go new file mode 100644 index 000000000..966eab1b4 --- /dev/null +++ b/cmd/envd/version.go @@ -0,0 +1,43 @@ +// Copyright 2022 The envd Authors +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. + +package main + +import ( + "fmt" + "github.com/tensorchord/envd/pkg/version" + "github.com/urfave/cli/v2" +) + +var CommandVersion = &cli.Command{ + Name: "version", + Aliases: []string{"v"}, + Usage: "Print envd version information", + Action: printVersion, +} + +func printVersion(*cli.Context) error { + ver := version.GetVersion() + fmt.Printf("envd: %s\n", ver) + fmt.Printf(" BuildDate: %s\n", ver.BuildDate) + fmt.Printf(" GitCommit: %s\n", ver.GitCommit) + fmt.Printf(" GitTreeState: %s\n", ver.GitTreeState) + if ver.GitTag != "" { + fmt.Printf(" GitTag: %s\n", ver.GitTag) + } + fmt.Printf(" GoVersion: %s\n", ver.GoVersion) + fmt.Printf(" Compiler: %s\n", ver.Compiler) + fmt.Printf(" Platform: %s\n", ver.Platform) + return nil +} diff --git a/pkg/version/version.go b/pkg/version/version.go index 1874f6396..236e817e2 100644 --- a/pkg/version/version.go +++ b/pkg/version/version.go @@ -19,26 +19,76 @@ package version import ( + "fmt" "regexp" + "runtime" "sync" ) -const ( - defaultVersion = "0.0.0+unknown" -) - var ( // Package is filled at linking time Package = "github.com/tensorchord/envd" - // Version holds the complete version number. Filled in at linking time. - Version = defaultVersion - // Revision is filled with the VCS (e.g. git) revision being used to build // the program at linking time. Revision = "" + + version = "0.0.0+unknown" + buildDate = "1970-01-01T00:00:00Z" // output from `date -u +'%Y-%m-%dT%H:%M:%SZ'` + gitCommit = "" // output from `git rev-parse HEAD` + gitTag = "" // output from `git describe --exact-match --tags HEAD` (if clean tree state) + gitTreeState = "" // determined from `git status --porcelain`. either 'clean' or 'dirty' ) +// Version contains envd version information +type Version struct { + Version string + BuildDate string + GitCommit string + GitTag string + GitTreeState string + GoVersion string + Compiler string + Platform string +} + +func (v Version) String() string { + return v.Version +} + +// GetVersion returns the version information +func GetVersion() Version { + var versionStr string + + if gitCommit != "" && gitTag != "" && gitTreeState == "clean" { + // if we have a clean tree state and the current commit is tagged, + // this is an official release. + versionStr = gitTag + } else { + // otherwise formulate a version string based on as much metadata + // information we have available. + versionStr = "v" + version + if len(gitCommit) >= 7 { + versionStr += "+" + gitCommit[0:7] + if gitTreeState != "clean" { + versionStr += ".dirty" + } + } else { + versionStr += "+unknown" + } + } + return Version{ + Version: versionStr, + BuildDate: buildDate, + GitCommit: gitCommit, + GitTag: gitTag, + GitTreeState: gitTreeState, + GoVersion: runtime.Version(), + Compiler: runtime.Compiler, + Platform: fmt.Sprintf("%s/%s", runtime.GOOS, runtime.GOARCH), + } +} + var ( reRelease *regexp.Regexp reDev *regexp.Regexp @@ -46,7 +96,7 @@ var ( ) func UserAgent() string { - version := defaultVersion + version := GetVersion().String() reOnce.Do(func() { reRelease = regexp.MustCompile(`^(v[0-9]+\.[0-9]+)\.[0-9]+$`) From 30943903846b1d55e2428f70cda20575bce019c3 Mon Sep 17 00:00:00 2001 From: terrytangyuan Date: Thu, 2 Jun 2022 23:22:20 -0500 Subject: [PATCH 2/3] Update goreleaser config Signed-off-by: terrytangyuan --- .goreleaser.yaml | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/.goreleaser.yaml b/.goreleaser.yaml index 59499a79a..49a9bcbb8 100644 --- a/.goreleaser.yaml +++ b/.goreleaser.yaml @@ -14,7 +14,7 @@ builds: binary: envd ldflags: - -s -w - - -X github.com/tensorchord/envd/pkg/version.Version={{ .Version }} + - -X github.com/tensorchord/envd/pkg/version.version={{ .Version }} - -X github.com/tensorchord/envd/pkg/version.Revision={{ .Commit }} - env: - CGO_ENABLED=0 @@ -26,7 +26,7 @@ builds: binary: envd-ssh ldflags: - -s -w - - -X github.com/tensorchord/envd/pkg/version.Version={{ .Version }} + - -X github.com/tensorchord/envd/pkg/version.version={{ .Version }} - -X github.com/tensorchord/envd/pkg/version.Revision={{ .Commit }} archives: - id: envd From 6120cafd226560bb61c344341fb3f25d4a4239e6 Mon Sep 17 00:00:00 2001 From: terrytangyuan Date: Thu, 2 Jun 2022 23:34:39 -0500 Subject: [PATCH 3/3] Use goreleaser env variables Signed-off-by: terrytangyuan --- .goreleaser.yaml | 10 +++++++--- 1 file changed, 7 insertions(+), 3 deletions(-) diff --git a/.goreleaser.yaml b/.goreleaser.yaml index 49a9bcbb8..c17c60be6 100644 --- a/.goreleaser.yaml +++ b/.goreleaser.yaml @@ -15,7 +15,9 @@ builds: ldflags: - -s -w - -X github.com/tensorchord/envd/pkg/version.version={{ .Version }} - - -X github.com/tensorchord/envd/pkg/version.Revision={{ .Commit }} + - -X github.com/tensorchord/envd/pkg/version.buildDate={{ .Date }} + - -X github.com/tensorchord/envd/pkg/version.gitCommit={{ .Commit }} + - -X github.com/tensorchord/envd/pkg/version.gitTreeState=clean - env: - CGO_ENABLED=0 goos: @@ -25,9 +27,11 @@ builds: main: ./cmd/envd-ssh binary: envd-ssh ldflags: - - -s -w + - -s -w - -X github.com/tensorchord/envd/pkg/version.version={{ .Version }} - - -X github.com/tensorchord/envd/pkg/version.Revision={{ .Commit }} + - -X github.com/tensorchord/envd/pkg/version.buildDate={{ .Date }} + - -X github.com/tensorchord/envd/pkg/version.gitCommit={{ .Commit }} + - -X github.com/tensorchord/envd/pkg/version.gitTreeState=clean archives: - id: envd format: binary