diff --git a/Makefile b/Makefile index a64f2f6..7a53ac2 100644 --- a/Makefile +++ b/Makefile @@ -1,6 +1,6 @@ DOCKER := $(shell which docker) DOCKER_BUF := $(DOCKER) run --rm -v $(CURDIR):/workspace --workdir /workspace bufbuild/buf -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)'" +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)' -X '$(versioningPath).nodeVersion=$(shell go list -m all | grep celestia-node | cut -d" " -f2)'" # Define all_pkgs, unit_pkgs, run, and cover vairables for test so that we can override them in # the terminal more easily. @@ -10,7 +10,7 @@ run := . count := 1 build: - @echo "--> Building Celestia" + @echo "--> Building celestia-da" @go build -o build/ ${LDFLAGS} ./cmd/celestia-da .PHONY: build diff --git a/cmd/celestia-da/main.go b/cmd/celestia-da/main.go index 9da941f..5a235c2 100644 --- a/cmd/celestia-da/main.go +++ b/cmd/celestia-da/main.go @@ -31,7 +31,7 @@ func init() { bridgeCmd := cmdnode.NewBridge(WithSubcommands()) lightCmd := cmdnode.NewLight(WithSubcommands()) fullCmd := cmdnode.NewFull(WithSubcommands()) - rootCmd.AddCommand(lightCmd, bridgeCmd, fullCmd) + rootCmd.AddCommand(lightCmd, bridgeCmd, fullCmd, versionCmd) } func main() { diff --git a/cmd/celestia-da/version.go b/cmd/celestia-da/version.go new file mode 100644 index 0000000..d3326b9 --- /dev/null +++ b/cmd/celestia-da/version.go @@ -0,0 +1,84 @@ +package main + +import ( + "fmt" + "runtime/debug" + "strings" + + "github.com/spf13/cobra" +) + +// buildInfo populated in ldflags by Makefile +type buildInfo struct { + BuildTime string + LastCommit string + SemanticVersion string + NodeVersion string +} + +// extractBuildInfo parses the ldflags string and returns buildInfo +func extractBuildInfo(ldflags string) *buildInfo { + var buildInfo buildInfo + + // Split ldflags into individual key-value pairs + keyValuePairs := strings.Split(ldflags, "-X ") + + // Iterate over key-value pairs + for _, pair := range keyValuePairs { + // Skip empty pairs + if pair == "" { + continue + } + + // Remove quotes + pair = strings.Trim(strings.TrimSpace(pair), "'") + + // Split pair into key and value + parts := strings.Split(pair, "=") + if len(parts) != 2 { + // Invalid pair, skip + continue + } + + // Trim leading and trailing spaces from key and value + key := parts[0] + value := strings.TrimSpace(parts[1]) + + // Assign value to corresponding field in BuildInfo + switch key { + case ".buildTime": + buildInfo.BuildTime = value + case ".lastCommit": + buildInfo.LastCommit = value + case ".semanticVersion": + buildInfo.SemanticVersion = value + case ".nodeVersion": + buildInfo.NodeVersion = value + } + } + return &buildInfo +} + +var versionCmd = &cobra.Command{ + Use: "version", + Short: "Show information about the current binary build", + Args: cobra.NoArgs, + Run: printBuildInfo, +} + +func printBuildInfo(_ *cobra.Command, _ []string) { + debugInfo, _ := debug.ReadBuildInfo() + var ldflags string + for _, kv := range debugInfo.Settings { + switch kv.Key { + case "-ldflags": + ldflags = kv.Value + } + } + buildInfo := extractBuildInfo(ldflags) + fmt.Printf("Semantic version: %s\n", buildInfo.SemanticVersion) + fmt.Printf("Build Time: %s\n", buildInfo.BuildTime) + fmt.Printf("Last Commit: %s\n", buildInfo.LastCommit) + fmt.Printf("Golang version: %s\n", debugInfo.GoVersion) + fmt.Printf("Celestia Node version: %s\n", buildInfo.NodeVersion) +}