8000 cmd: add version command by tuxcanfly · Pull Request #51 · rollkit/celestia-da · GitHub
[go: up one dir, main page]
More Web Proxy on the site http://driver.im/
Skip to content
This repository was archived by the owner on Apr 29, 2024. It is now read-only.

cmd: add version command #51

Merged
merged 1 commit into from
Jan 11, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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 := 8000 $(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