Skip to content

Commit

Permalink
✨ feat: enhance cli version cmd (#76)
Browse files Browse the repository at this point in the history
* ✨ Feat: introduce new version --verbose flag

This commit introduces a `--verbose`/`-v` flag for the version command and the top-level logic for the version command to handle verbosity requests.

* ✨ Feat: add version verbose logic

This commit adds `version_manuscript.go` as a new file to the repo. Within, new structs are defined for handling version information for manuscript-core, docker, docker desktop,as well as the build time environment of rust, and go. Additionally, versioning information is  pretty-printed to the console & emojified!

* 🔖 [chore] bump version number to 1.1.0

The CLI was still displaying the old version number (1.0.4) ; this commit bumps the version var in the commander.go package so that the version command will display the proper version.

* 💡 [chore] update build information about ldflags

There was an error previously. Wording  did not contain the correct path for the `go build`instructions in the source code comments of `version_manuscript.go`
  • Loading branch information
KagemniKarimu authored Dec 10, 2024
1 parent 89faf24 commit 5b8690e
Show file tree
Hide file tree
Showing 2 changed files with 116 additions and 5 deletions.
15 changes: 10 additions & 5 deletions commands/commander.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import (

var (
env string
version = "1.0.4"
version = "1.1.0"
)

func Execute(args []string) error {
Expand Down Expand Up @@ -176,12 +176,14 @@ var versionCmd = &cobra.Command{
Long: `ℹ️ Manuscript CLI Version Information
Shows:
🔷 CLI version
🔷 Build info (coming soon)
🔷 API versions (coming soon)`,
🛠️ Manuscript Core version
🏗️ Build info
💻 System information
🐳 Docker environment details`,
Example: `>> manuscript-cli version`,
Run: func(cmd *cobra.Command, args []string) {
fmt.Printf("manuscript-cli version %s\n", version)
verbosity, _ := cmd.Flags().GetBool("verbose")
showVersion(verbosity)
},
}

Expand Down Expand Up @@ -212,6 +214,9 @@ func init() {
deployManuscript.Flags().StringVar(&env, "env", "", "Specify the environment to deploy (local or chainbase)")
deployManuscript.MarkFlagRequired("env")

// Configure version command flags
versionCmd.Flags().BoolP("verbose", "v", false, "Display detailed version information")

// Disable the default completion command
rootCmd.CompletionOptions.HiddenDefaultCmd = true
}
Expand Down
106 changes: 106 additions & 0 deletions commands/version_manuscript.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,106 @@
package commands

import (
"fmt"
"os/exec"
"runtime"
"strings"
)

// Build-time variables set via ldflags
// Build command would be:
//
// go build -ldflags "-X manuscript-core/commands.gitCommit=$(git rev-parse --short HEAD) \
// -X manuscript-core/commands.buildDate=$(date -u '+%Y-%m-%d:%H:%M:%S') \
// -X manuscript-core/commands.goVersion=$(go version | cut -d' ' -f3) \
// -X manuscript-core/commands.rustVersion=$(rustc --version | cut -d' ' -f2)" \
// -o manuscript-cli main.go
var (
gitCommit = "unknown" // Set by build flags
buildDate = "unknown" // Set by build flags
goVersion = "unknown" // Set by build flags
rustVersion = "unknown" // Set by build flags
)

type VersionInfo struct {
Version string `json:"version"`
GoVersion string `json:"go_version"`
RustVersion string `json:"rust_version"`
GitCommit string `json:"git_commit"`
BuildDate string `json:"build_date"`
OS string `json:"os"`
Arch string `json:"arch"`
DockerInfo DockerInfo `json:"docker_info"`
}

type DockerInfo struct {
Version string `json:"version"`
DesktopVersion string `json:"desktop_version"`
}

func showVersion(verbose bool) {
info := getVersionInfo()

if !verbose {
fmt.Printf("manuscript-core version %s\n", info.Version)
return
}

fmt.Printf("\n📜 Manuscript Core:\n")
fmt.Printf(" Version:\t%s\n", info.Version)

fmt.Printf("\n🏗️ Build Information:\n")
fmt.Printf(" Go Version:\t%s\n", info.GoVersion)
fmt.Printf(" Rust Version:\t%s\n", info.RustVersion)
fmt.Printf(" Git Commit:\t%s\n", info.GitCommit)
fmt.Printf(" Built:\t%s\n", info.BuildDate)

fmt.Printf("\n💻 System Information:\n")
uname, _ := exec.Command("uname", "-a").Output()
fmt.Printf(" Unix Name:\t%s", string(uname))
fmt.Printf(" OS/Arch:\t%s/%s\n", info.OS, info.Arch)

fmt.Printf("\n🐳 Docker Environment:\n")
if info.DockerInfo.Version != "" {
fmt.Printf(" Docker:\t'%s'\n", info.DockerInfo.Version)
}
if info.DockerInfo.DesktopVersion != "" {
fmt.Printf(" Docker Desktop:\t'%s'\n", info.DockerInfo.DesktopVersion)
}
}

func getVersionInfo() VersionInfo {
info := VersionInfo{
Version: version,
GoVersion: goVersion,
RustVersion: rustVersion,
GitCommit: gitCommit,
BuildDate: buildDate,
OS: runtime.GOOS,
Arch: runtime.GOARCH,
}

// Get Docker version
if dockerVersion, err := exec.Command("docker", "--version").Output(); err == nil {
version := strings.TrimSpace(string(dockerVersion))
parts := strings.Split(version, " ")
if len(parts) >= 3 {
info.DockerInfo.Version = strings.Trim(parts[2], ",")
}
}

// Get Docker Desktop version
cmd := exec.Command("docker", "version", "--format", "{{.Server}} {{.Server.Platform.Name}}")
if desktopVersion, err := cmd.Output(); err == nil {
// Find "Docker Desktop" and extract version
output := string(desktopVersion)
if strings.Contains(output, "Docker Desktop") {
parts := strings.Split(output, "Docker Desktop")
if len(parts) > 1 {
info.DockerInfo.DesktopVersion = strings.Split(parts[1], " ")[1]
}
}
}

return info
}

0 comments on commit 5b8690e

Please sign in to comment.