Skip to content
This repository has been archived by the owner on Apr 29, 2024. It is now read-only.

Commit

Permalink
Merge pull request #51 from rollkit/tux/cmd-version
Browse files Browse the repository at this point in the history
cmd: add version command
  • Loading branch information
tuxcanfly committed Jan 11, 2024
2 parents a824137 + 789f83c commit 8e29d28
Show file tree
Hide file tree
Showing 3 changed files with 87 additions and 3 deletions.
4 changes: 2 additions & 2 deletions Makefile
Original file line number Diff line number Diff line change
@@ -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.
Expand All @@ -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

Expand Down
2 changes: 1 addition & 1 deletion cmd/celestia-da/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -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() {
Expand Down
84 changes: 84 additions & 0 deletions cmd/celestia-da/version.go
Original file line number Diff line number Diff line change
@@ -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)
}

0 comments on commit 8e29d28

Please sign in to comment.