From f35a4cfcc704b0d39b0dca9a665f593e50e60ebe Mon Sep 17 00:00:00 2001 From: Kohei Tokunaga Date: Mon, 9 May 2022 17:18:23 +0900 Subject: [PATCH] Add version command Signed-off-by: Kohei Tokunaga --- Makefile | 7 ++++++- README.md | 3 ++- main.go | 13 +++++++++++++ pkg/version/version.go | 9 +++++++++ 4 files changed, 30 insertions(+), 2 deletions(-) create mode 100644 pkg/version/version.go diff --git a/Makefile b/Makefile index e318c563..8342cf8e 100644 --- a/Makefile +++ b/Makefile @@ -2,12 +2,17 @@ CMD_DESTDIR ?= /usr/local PREFIX ?= $(CURDIR)/out/ CMD=buildg +PKG=github.com/ktock/buildg +VERSION=$(shell git describe --match 'v[0-9]*' --dirty='.m' --always --tags) +REVISION=$(shell git rev-parse HEAD)$(shell if ! git diff --no-ext-diff --quiet --exit-code; then echo .m; fi) +GO_LD_FLAGS=-ldflags '-s -w -X $(PKG)/pkg/version.Version=$(VERSION) -X $(PKG)/pkg/version.Revision=$(REVISION) $(GO_EXTRA_LDFLAGS)' + all: build build: buildg buildg: - go build -o $(PREFIX)/buildg -v . + go build -o $(PREFIX)/buildg $(GO_LD_FLAGS) -v . install: install -D -m 755 $(PREFIX)/buildg $(CMD_DESTDIR)/bin diff --git a/README.md b/README.md index 5c402fda..93b07b26 100644 --- a/README.md +++ b/README.md @@ -157,6 +157,7 @@ USAGE: COMMANDS: debug Debug a build + version Version info help, h Shows a list of commands or help for one command GLOBAL OPTIONS: @@ -166,7 +167,7 @@ GLOBAL OPTIONS: ### buildg debug -``` +```console $ buildg debug --help NAME: buildg debug - Debug a build diff --git a/main.go b/main.go index def3fc58..ff2c9cb2 100644 --- a/main.go +++ b/main.go @@ -13,6 +13,7 @@ import ( "github.com/containerd/console" "github.com/containerd/containerd/pkg/userns" "github.com/containerd/containerd/snapshots/native" + "github.com/ktock/buildg/pkg/version" "github.com/moby/buildkit/cache/remotecache" "github.com/moby/buildkit/client" "github.com/moby/buildkit/cmd/buildctl/build" @@ -51,6 +52,7 @@ func main() { } app.Commands = []cli.Command{ newDebugCommand(), + newVersionCommand(), } app.Before = func(context *cli.Context) error { logrus.SetFormatter(&logrus.TextFormatter{FullTimestamp: true}) @@ -65,6 +67,17 @@ func main() { } } +func newVersionCommand() cli.Command { + return cli.Command{ + Name: "version", + Usage: "Version info", + Action: func(clicontext *cli.Context) error { + fmt.Println("buildg", version.Version, version.Revision) + return nil + }, + } +} + func newDebugCommand() cli.Command { var flags []cli.Flag if userns.RunningInUserNS() { diff --git a/pkg/version/version.go b/pkg/version/version.go new file mode 100644 index 00000000..2285e29f --- /dev/null +++ b/pkg/version/version.go @@ -0,0 +1,9 @@ +package version + +var ( + // Version is the version number. Filled in at linking time (via Makefile). + Version = "" + + // Revision is the VCS (e.g. git) revision. Filled in at linking time (via Makefile). + Revision = "" +)