Skip to content

Commit

Permalink
Merge pull request #26 from jimmykodes/version
Browse files Browse the repository at this point in the history
Add --version support
  • Loading branch information
jimmykodes authored Oct 6, 2023
2 parents acb469b + d3adfda commit 5bbdc28
Show file tree
Hide file tree
Showing 3 changed files with 40 additions and 0 deletions.
38 changes: 38 additions & 0 deletions command.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ var (
ErrNoRunner = errors.New("gommand: command has no run function")
ErrNoSubcommand = errors.New("gommand: must specify a subcommand")
errShowHelp = errors.New("show help")
errShowVersion = errors.New("show version")
)

// Command represents a command line command
Expand Down Expand Up @@ -66,6 +67,15 @@ type Command struct {
// Description is the longer description of the command printed out by the help text
Description string

// Version is the value that will be printed when `--version` is passed to the command.
// When retrieving the command version, the call tree is traversed backwards until a Command
// is reached that has a non-zero value for the version. This means that it is possible
// to version individual branches of the call tree, though this is not recommended. It is
// intended to be set at the root of the tree, ideally through a package level var that can
// be set using ldflags at build time
// ie: go build -ldflags="cmd.Version=1.1.0"
Version string

// ArgValidator is an ArgValidator to be called on the args of the function being executed. This is called before any of
// the functions for this command are called.
// If this is not defined ArgsNone is used.
Expand Down Expand Up @@ -158,6 +168,15 @@ func (c *Command) ExecuteContext(ctx context.Context) error {
cmdCtx.cmd.help()
return nil
}
if errors.Is(err, errShowVersion) {
v := c._version()
if v == "" {
v = "N/A"
}
fmt.Println(v)
return nil
}

if mErr := errors.Join(err, c.err); mErr != nil {
if !cmdCtx.silenceHelp {
cmdCtx.cmd.help()
Expand Down Expand Up @@ -185,6 +204,17 @@ func (c *Command) name() (name string) {
return
}

func (c *Command) _version() string {
_c := c
for _c.Version == "" {
if _c.parent == nil {
break
}
_c = _c.parent
}
return _c.Version
}

func (c *Command) help() {
if c.Description != "" {
fmt.Println(c.Description)
Expand All @@ -194,6 +224,11 @@ func (c *Command) help() {
fmt.Println()
}

if v := c._version(); v != "" {
fmt.Println("Version:", v)
fmt.Println()
}

fmt.Println("Usage:")
usage := []string{c.Name}
for parent := c.parent; parent != nil; parent = parent.parent {
Expand Down Expand Up @@ -352,6 +387,9 @@ func (c *Command) execute(ctx *Context) error {
if token.Name == "help" {
return errShowHelp
}
if token.Name == "version" {
return errShowVersion
}
f = fs.FromName(token.Name)
}

Expand Down
1 change: 1 addition & 0 deletions examples/simple/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ var rootCmd = &gommand.Command{
Name: "sum",
Usage: "sum [...n]",
Description: "sum all provided numbers",
Version: "1.0.0",
ArgValidator: gommand.ArgsEvery(gommand.ArgsMin(1), ints),
Run: func(ctx *gommand.Context) error {
var total int
Expand Down
1 change: 1 addition & 0 deletions examples/subcommands/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ var (
rootCmd = &gommand.Command{
Name: "math",
Usage: "a collection of math commands",
Version: "1.0.0",
SilenceError: true,
PersistentFlagSet: flags.NewFlagSet().AddFlags(
flags.StringFlag("host", "", "host address"),
Expand Down

0 comments on commit 5bbdc28

Please sign in to comment.