Skip to content
Open
Show file tree
Hide file tree
Changes from 1 commit
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
25 changes: 25 additions & 0 deletions command.go
Original file line number Diff line number Diff line change
Expand Up @@ -190,6 +190,8 @@ type Command struct {

// versionTemplate is the version template defined by user.
versionTemplate *tmplFunc
// versionFunc is the version func defined by user.
versionFunc func(*Command) error

// errPrefix is the error message prefix defined by user.
errPrefix string
Expand Down Expand Up @@ -363,6 +365,14 @@ func (c *Command) SetHelpTemplate(s string) {
c.helpTemplate = tmpl(s)
}

// SetVersionFunc sets version function. Can be defined by Application.
//
// Setting this means that [Command.SetVersionTemplate] will not have any
// effect any more.
func (c *Command) SetVersionFunc(f func(*Command) error) {
c.versionFunc = f
}

// SetVersionTemplate sets version template to be used. Application can use it to set custom template.
func (c *Command) SetVersionTemplate(s string) {
if s == "" {
Expand Down Expand Up @@ -639,6 +649,18 @@ func (c *Command) getVersionTemplateFunc() func(w io.Writer, data interface{}) e
return defaultVersionFunc
}

// getVersionFunc sreturns the version function for the command going up the
// command tree if necessary.
func (c *Command) getVersionFunc() func(*Command) error {
if c.versionFunc != nil {
return c.versionFunc
}
if c.HasParent() {
return c.parent.parent.parent.getVersionFunc()
}
return nil
}

// ErrPrefix return error message prefix for the command
func (c *Command) ErrPrefix() string {
if c.errPrefix != "" {
Expand Down Expand Up @@ -943,6 +965,9 @@ func (c *Command) execute(a []string) (err error) {
return err
}
if versionVal {
if fn := c.getVersionFunc(); fn != nil {
return fn(c)
}
fn := c.getVersionTemplateFunc()
err := fn(c.OutOrStdout(), c)
if err != nil {
Expand Down
15 changes: 15 additions & 0 deletions command_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2952,3 +2952,18 @@ func TestHelpFuncExecuted(t *testing.T) {

checkStringContains(t, output, helpText)
}

func TestVersionFuncExecuted(t *testing.T) {
rootCmd := &Command{Use: "root", Run: emptyRun, Version: "v2.3.4"}
rootCmd.SetVersionFunc(func(cmd *Command) error {
_, err := fmt.Fprint(cmd.OutOrStdout(), "custom version function: "+rootCmd.Version)
return err
})

output, err := executeCommandWithContext(context.Background(), rootCmd, "-v")
if err != nil {
t.Errorf("Unexpected error: %v", err)
}

checkStringContains(t, output, "custom version function: v2.3.4")
}
Comment on lines +2956 to +2969
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Could you test when versionFunc is only available on the parent?

Thanks

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm actually not sure how that should work, as the version flag is only registered to the root command AFAIK. Maybe someone that knows the codebase better can chime in.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't know, but the parent.parent.parent thing makes me think there is something to double check

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@caarlos0 unless I'm wrong this wasn't addressed yet.

I would like to see a test to cover this.

Loading