Skip to content

Commit

Permalink
feat: add AfterRun() hook
Browse files Browse the repository at this point in the history
Fixes #288
  • Loading branch information
alecthomas committed Nov 3, 2024
1 parent d0beaf7 commit 2544d3f
Show file tree
Hide file tree
Showing 5 changed files with 49 additions and 15 deletions.
22 changes: 13 additions & 9 deletions context.go
Original file line number Diff line number Diff line change
Expand Up @@ -809,18 +809,22 @@ func (c *Context) RunNode(node *Node, binds ...interface{}) (err error) {
func (c *Context) Run(binds ...interface{}) (err error) {
node := c.Selected()
if node == nil {
if len(c.Path) > 0 {
selected := c.Path[0].Node()
if selected.Type == ApplicationNode {
method := getMethod(selected.Target, "Run")
if method.IsValid() {
return c.RunNode(selected, binds...)
}
if len(c.Path) == 0 {
return fmt.Errorf("no command selected")
}
selected := c.Path[0].Node()
if selected.Type == ApplicationNode {
method := getMethod(selected.Target, "Run")
if method.IsValid() {
node = selected
}
} else {
return fmt.Errorf("no command selected")
}
return fmt.Errorf("no command selected")
}
return c.RunNode(node, binds...)
runErr := c.RunNode(node, binds...)
err = c.Kong.applyHook(c, "AfterRun")
return errors.Join(runErr, err)
}

// PrintUsage to Kong's stdout.
Expand Down
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -7,4 +7,4 @@ require (

require github.com/hexops/gotextdiff v1.0.3 // indirect

go 1.18
go 1.20
2 changes: 0 additions & 2 deletions go.sum
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
github.com/alecthomas/assert/v2 v2.10.0 h1:jjRCHsj6hBJhkmhznrCzoNpbA3zqy0fYiUcYZP/GkPY=
github.com/alecthomas/assert/v2 v2.10.0/go.mod h1:Bze95FyfUr7x34QZrjL+XP+0qgp/zg8yS+TtBj1WA3k=
github.com/alecthomas/assert/v2 v2.11.0 h1:2Q9r3ki8+JYXvGsDyBXwH3LcJ+WK5D0gc5E8vS6K3D0=
github.com/alecthomas/assert/v2 v2.11.0/go.mod h1:Bze95FyfUr7x34QZrjL+XP+0qgp/zg8yS+TtBj1WA3k=
github.com/alecthomas/repr v0.4.0 h1:GhI2A8MACjfegCPVq9f1FLvIBS+DrQ2KQBFZP1iFzXc=
Expand Down
13 changes: 10 additions & 3 deletions hooks.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,17 +3,24 @@ package kong
// BeforeResolve is a documentation-only interface describing hooks that run before resolvers are applied.
type BeforeResolve interface {
// This is not the correct signature - see README for details.
BeforeResolve(args ...interface{}) error
BeforeResolve(args ...any) error
}

// BeforeApply is a documentation-only interface describing hooks that run before values are set.
type BeforeApply interface {
// This is not the correct signature - see README for details.
BeforeApply(args ...interface{}) error
BeforeApply(args ...any) error
}

// AfterApply is a documentation-only interface describing hooks that run after values are set.
type AfterApply interface {
// This is not the correct signature - see README for details.
AfterApply(args ...interface{}) error
AfterApply(args ...any) error
}

// AfterRun is a documentation-only interface describing hooks that run after Run() returns.
type AfterRun interface {
// This is not the correct signature - see README for details.
// AfterRun is called after Run() returns.
AfterRun(args ...any) error
}
25 changes: 25 additions & 0 deletions kong_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2327,3 +2327,28 @@ func TestRecursiveVariableExpansion(t *testing.T) {
assert.NoError(t, err)
assert.Contains(t, w.String(), "Default: /etc/config")
}

type afterRunCLI struct {
runCalled bool `kong:"-"`
afterRunCalled bool `kong:"-"`
}

func (c *afterRunCLI) Run() error {
c.runCalled = true
return nil
}

func (c *afterRunCLI) AfterRun() error {
c.afterRunCalled = true
return nil
}

func TestAfterRun(t *testing.T) {
var cli afterRunCLI
k := mustNew(t, &cli)
kctx, err := k.Parse([]string{})
assert.NoError(t, err)
err = kctx.Run()
assert.NoError(t, err)
assert.Equal(t, afterRunCLI{runCalled: true, afterRunCalled: true}, cli)
}

0 comments on commit 2544d3f

Please sign in to comment.