Skip to content

Commit

Permalink
feat: Add version command with enriched information (#236)
Browse files Browse the repository at this point in the history
* feat: Add version command with enriched information

Signed-off-by: terrytangyuan <terrytangyuan@gmail.com>

* Update goreleaser config

Signed-off-by: terrytangyuan <terrytangyuan@gmail.com>

* Use goreleaser env variables

Signed-off-by: terrytangyuan <terrytangyuan@gmail.com>
  • Loading branch information
terrytangyuan committed Jun 3, 2022
1 parent f14cd6a commit ced22ea
Show file tree
Hide file tree
Showing 6 changed files with 121 additions and 19 deletions.
14 changes: 9 additions & 5 deletions .goreleaser.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,10 @@ builds:
binary: envd
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.version={{ .Version }}
- -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:
Expand All @@ -25,9 +27,11 @@ builds:
main: ./cmd/envd-ssh
binary: envd-ssh
ldflags:
- -s -w
- -X github.com/tensorchord/envd/pkg/version.Version={{ .Version }}
- -X github.com/tensorchord/envd/pkg/version.Revision={{ .Commit }}
- -s -w
- -X github.com/tensorchord/envd/pkg/version.version={{ .Version }}
- -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
Expand Down
8 changes: 6 additions & 2 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down Expand Up @@ -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

Expand Down
6 changes: 3 additions & 3 deletions cmd/envd-ssh/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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()
}

Expand Down
3 changes: 2 additions & 1 deletion cmd/envd/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand All @@ -69,6 +69,7 @@ func run(args []string) (bool, error) {
CommandPause,
CommandResume,
CommandUp,
CommandVersion,
}

// Deal with debug flag.
Expand Down
43 changes: 43 additions & 0 deletions cmd/envd/version.go
Original file line number Diff line number Diff line change
@@ -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
}
66 changes: 58 additions & 8 deletions pkg/version/version.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,34 +19,84 @@
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
reOnce sync.Once
)

func UserAgent() string {
version := defaultVersion
version := GetVersion().String()

reOnce.Do(func() {
reRelease = regexp.MustCompile(`^(v[0-9]+\.[0-9]+)\.[0-9]+$`)
Expand Down

0 comments on commit ced22ea

Please sign in to comment.