From d799075c637f4238541b8c5b1576b04fb7c83bc4 Mon Sep 17 00:00:00 2001 From: CHAMI Rachid Date: Fri, 27 Oct 2023 23:23:51 +0200 Subject: [PATCH] feat: add version command (#565) --- Makefile | 7 +++--- cmd/blobstream/root/cmd.go | 2 ++ cmd/blobstream/version/build_info.go | 35 ++++++++++++++++++++++++++++ cmd/blobstream/version/version.go | 23 ++++++++++++++++++ 4 files changed, 64 insertions(+), 3 deletions(-) create mode 100644 cmd/blobstream/version/build_info.go create mode 100644 cmd/blobstream/version/version.go diff --git a/Makefile b/Makefile index 6c360e5b..36860c61 100644 --- a/Makefile +++ b/Makefile @@ -1,14 +1,15 @@ #!/usr/bin/make -f VERSION := $(shell echo $(shell git describe --tags 2>/dev/null || git log -1 --format='%h') | sed 's/^v//') -COMMIT := $(shell git log -1 --format='%H') DOCKER := $(shell which docker) +versioningPath := "github.com/celestiaorg/orchestrator-relayer/cmd/blobstream/version" +LDFLAGS=-ldflags="-X '$(versioningPath).buildTime=$(shell date)' -X '$(versioningPath).lastCommit=$(shell git rev-parse HEAD)' -X '$(versioningPath).semanticVersion=$(shell git describe --tags --dirty=-dev 2>/dev/null || git rev-parse --abbrev-ref HEAD)'" all: install install: go.sum @echo "--> Installing blobstream" - @go install -mod=readonly ./cmd/blobstream + @go install -mod=readonly ${LDFLAGS} ./cmd/blobstream go.sum: mod @echo "--> Verifying dependencies have expected content" @@ -24,7 +25,7 @@ pre-build: build: mod @mkdir -p build/ - @go build -o build ./cmd/blobstream + @go build -o build ${LDFLAGS} ./cmd/blobstream build-docker: @echo "--> Building Docker image" diff --git a/cmd/blobstream/root/cmd.go b/cmd/blobstream/root/cmd.go index 3d955c0a..47e061f8 100644 --- a/cmd/blobstream/root/cmd.go +++ b/cmd/blobstream/root/cmd.go @@ -4,6 +4,7 @@ import ( "github.com/celestiaorg/orchestrator-relayer/cmd/blobstream/bootstrapper" "github.com/celestiaorg/orchestrator-relayer/cmd/blobstream/generate" "github.com/celestiaorg/orchestrator-relayer/cmd/blobstream/query" + "github.com/celestiaorg/orchestrator-relayer/cmd/blobstream/version" "github.com/celestiaorg/celestia-app/x/qgb/client" "github.com/celestiaorg/orchestrator-relayer/cmd/blobstream/deploy" @@ -30,6 +31,7 @@ func Cmd() *cobra.Command { generate.Command(), query.Command(), bootstrapper.Command(), + version.Cmd, ) rootCmd.SetHelpCommand(&cobra.Command{}) diff --git a/cmd/blobstream/version/build_info.go b/cmd/blobstream/version/build_info.go new file mode 100644 index 00000000..f75292ab --- /dev/null +++ b/cmd/blobstream/version/build_info.go @@ -0,0 +1,35 @@ +package version + +import ( + "fmt" + "runtime" +) + +var ( + buildTime string + lastCommit string + semanticVersion string + + systemVersion = fmt.Sprintf("%s/%s", runtime.GOARCH, runtime.GOOS) + golangVersion = runtime.Version() +) + +// BuildInfo represents all necessary information about the current build. +type BuildInfo struct { + BuildTime string + LastCommit string + SemanticVersion string + SystemVersion string + GolangVersion string +} + +// GetBuildInfo returns information about the current build. +func GetBuildInfo() *BuildInfo { + return &BuildInfo{ + buildTime, + lastCommit, + semanticVersion, + systemVersion, + golangVersion, + } +} diff --git a/cmd/blobstream/version/version.go b/cmd/blobstream/version/version.go new file mode 100644 index 00000000..9fa62caf --- /dev/null +++ b/cmd/blobstream/version/version.go @@ -0,0 +1,23 @@ +package version + +import ( + "fmt" + + "github.com/spf13/cobra" +) + +var Cmd = &cobra.Command{ + Use: "version", + Short: "Show information about the current binary build", + Args: cobra.NoArgs, + Run: printBuildInfo, +} + +func printBuildInfo(_ *cobra.Command, _ []string) { + buildInfo := GetBuildInfo() + fmt.Printf("Semantic version: %s\n", buildInfo.SemanticVersion) + fmt.Printf("Commit: %s\n", buildInfo.LastCommit) + fmt.Printf("Build Date: %s\n", buildInfo.BuildTime) + fmt.Printf("System version: %s\n", buildInfo.SystemVersion) + fmt.Printf("Golang version: %s\n", buildInfo.GolangVersion) +}