Skip to content

Commit

Permalink
Add a --version flag for the root command
Browse files Browse the repository at this point in the history
  • Loading branch information
iBug committed Jan 17, 2024
1 parent f98735e commit bee8488
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 5 deletions.
3 changes: 2 additions & 1 deletion Makefile
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@
BIN := vct
VERSION := $(shell git describe --tags --always --dirty)

.PHONY: all $(BIN) clean

all: $(BIN)

$(BIN):
go build -ldflags='-s -w'
go build -ldflags='-s -w -X main.version=$(VERSION)'

clean:
rm -f $(BIN)
14 changes: 10 additions & 4 deletions cmd/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,22 +8,28 @@ import (
"github.com/spf13/cobra"
)

func showHelp(cmd *cobra.Command, args []string) {
cmd.Help()
}
var Version string

func MakeCmd() *cobra.Command {
cmd := &cobra.Command{
Use: "vct",
Short: "Vlab's Container Tool",
Long: "Vlab's Container Tool, a versatile tool for managing containers on Proxmox VE",
Args: cobra.NoArgs,
Run: showHelp,
}
cmd.CompletionOptions.HiddenDefaultCmd = true
cmd.AddCommand(df.MakeCmd())
cmd.AddCommand(findpid.MakeCmd())
cmd.AddCommand(iostat.MakeCmd())
cmd.AddCommand(pressure.MakeCmd())
pVersion := cmd.Flags().BoolP("version", "v", false, "show version")

cmd.Run = func(cmd *cobra.Command, args []string) {
if *pVersion {
cmd.Println(cmd.Name(), Version)
} else {
cmd.Help()
}
}
return cmd
}
3 changes: 3 additions & 0 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,10 @@ import (
"github.com/USTC-vlab/vct/cmd"
)

var version string = "<unknown>"

func main() {
cmd.Version = version
if err := cmd.MakeCmd().Execute(); err != nil {
os.Exit(1)
}
Expand Down

0 comments on commit bee8488

Please sign in to comment.