mirror of
https://github.com/jimeh/rbheap.git
synced 2026-02-19 12:56:46 +00:00
Switch from kingpin to cobra
This enables easier use of sub-commands for flexibility
This commit is contained in:
62
cmd/root.go
Normal file
62
cmd/root.go
Normal file
@@ -0,0 +1,62 @@
|
||||
package cmd
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"fmt"
|
||||
"os"
|
||||
"runtime"
|
||||
"strings"
|
||||
|
||||
"github.com/spf13/cobra"
|
||||
)
|
||||
|
||||
// BuildInfo represents info collected as build-time.
|
||||
type BuildInfo struct {
|
||||
Version string
|
||||
Commit string
|
||||
Date string
|
||||
}
|
||||
|
||||
var rootCmd = &cobra.Command{
|
||||
Use: "rbheapleak",
|
||||
Short: "rbheapleak analyzes ObjectSpace dumps from Ruby processes.",
|
||||
SilenceUsage: true,
|
||||
SilenceErrors: true,
|
||||
}
|
||||
|
||||
func versionString(info *BuildInfo) string {
|
||||
var buffer bytes.Buffer
|
||||
var meta []string
|
||||
|
||||
buffer.WriteString(info.Version)
|
||||
|
||||
if info.Commit != "unknown" {
|
||||
meta = append(meta, info.Commit)
|
||||
}
|
||||
|
||||
meta = append(meta, runtime.Version())
|
||||
|
||||
if info.Date != "unknown" {
|
||||
meta = append(meta, info.Date)
|
||||
}
|
||||
|
||||
if len(meta) > 0 {
|
||||
buffer.WriteString(fmt.Sprintf(" (%s)", strings.Join(meta, ", ")))
|
||||
}
|
||||
|
||||
return buffer.String()
|
||||
}
|
||||
|
||||
func Execute(info *BuildInfo) {
|
||||
rootCmd.Version = versionString(info)
|
||||
rootCmd.SetVersionTemplate("{{.Use}} {{.Version}}\n")
|
||||
|
||||
if err := rootCmd.Execute(); err != nil {
|
||||
fmt.Println(err)
|
||||
os.Exit(1)
|
||||
}
|
||||
}
|
||||
|
||||
func init() {
|
||||
rootCmd.Flags().BoolP("version", "v", false, "Show version.")
|
||||
}
|
||||
Reference in New Issue
Block a user