Skip to content

Commit 2544d3f

Browse files
committed
feat: add AfterRun() hook
Fixes #288
1 parent d0beaf7 commit 2544d3f

File tree

5 files changed

+49
-15
lines changed

5 files changed

+49
-15
lines changed

context.go

Lines changed: 13 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -809,18 +809,22 @@ func (c *Context) RunNode(node *Node, binds ...interface{}) (err error) {
809809
func (c *Context) Run(binds ...interface{}) (err error) {
810810
node := c.Selected()
811811
if node == nil {
812-
if len(c.Path) > 0 {
813-
selected := c.Path[0].Node()
814-
if selected.Type == ApplicationNode {
815-
method := getMethod(selected.Target, "Run")
816-
if method.IsValid() {
817-
return c.RunNode(selected, binds...)
818-
}
812+
if len(c.Path) == 0 {
813+
return fmt.Errorf("no command selected")
814+
}
815+
selected := c.Path[0].Node()
816+
if selected.Type == ApplicationNode {
817+
method := getMethod(selected.Target, "Run")
818+
if method.IsValid() {
819+
node = selected
819820
}
821+
} else {
822+
return fmt.Errorf("no command selected")
820823
}
821-
return fmt.Errorf("no command selected")
822824
}
823-
return c.RunNode(node, binds...)
825+
runErr := c.RunNode(node, binds...)
826+
err = c.Kong.applyHook(c, "AfterRun")
827+
return errors.Join(runErr, err)
824828
}
825829

826830
// PrintUsage to Kong's stdout.

go.mod

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,4 +7,4 @@ require (
77

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

10-
go 1.18
10+
go 1.20

go.sum

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,3 @@
1-
github.com/alecthomas/assert/v2 v2.10.0 h1:jjRCHsj6hBJhkmhznrCzoNpbA3zqy0fYiUcYZP/GkPY=
2-
github.com/alecthomas/assert/v2 v2.10.0/go.mod h1:Bze95FyfUr7x34QZrjL+XP+0qgp/zg8yS+TtBj1WA3k=
31
github.com/alecthomas/assert/v2 v2.11.0 h1:2Q9r3ki8+JYXvGsDyBXwH3LcJ+WK5D0gc5E8vS6K3D0=
42
github.com/alecthomas/assert/v2 v2.11.0/go.mod h1:Bze95FyfUr7x34QZrjL+XP+0qgp/zg8yS+TtBj1WA3k=
53
github.com/alecthomas/repr v0.4.0 h1:GhI2A8MACjfegCPVq9f1FLvIBS+DrQ2KQBFZP1iFzXc=

hooks.go

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,17 +3,24 @@ package kong
33
// BeforeResolve is a documentation-only interface describing hooks that run before resolvers are applied.
44
type BeforeResolve interface {
55
// This is not the correct signature - see README for details.
6-
BeforeResolve(args ...interface{}) error
6+
BeforeResolve(args ...any) error
77
}
88

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

1515
// AfterApply is a documentation-only interface describing hooks that run after values are set.
1616
type AfterApply interface {
1717
// This is not the correct signature - see README for details.
18-
AfterApply(args ...interface{}) error
18+
AfterApply(args ...any) error
19+
}
20+
21+
// AfterRun is a documentation-only interface describing hooks that run after Run() returns.
22+
type AfterRun interface {
23+
// This is not the correct signature - see README for details.
24+
// AfterRun is called after Run() returns.
25+
AfterRun(args ...any) error
1926
}

kong_test.go

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2327,3 +2327,28 @@ func TestRecursiveVariableExpansion(t *testing.T) {
23272327
assert.NoError(t, err)
23282328
assert.Contains(t, w.String(), "Default: /etc/config")
23292329
}
2330+
2331+
type afterRunCLI struct {
2332+
runCalled bool `kong:"-"`
2333+
afterRunCalled bool `kong:"-"`
2334+
}
2335+
2336+
func (c *afterRunCLI) Run() error {
2337+
c.runCalled = true
2338+
return nil
2339+
}
2340+
2341+
func (c *afterRunCLI) AfterRun() error {
2342+
c.afterRunCalled = true
2343+
return nil
2344+
}
2345+
2346+
func TestAfterRun(t *testing.T) {
2347+
var cli afterRunCLI
2348+
k := mustNew(t, &cli)
2349+
kctx, err := k.Parse([]string{})
2350+
assert.NoError(t, err)
2351+
err = kctx.Run()
2352+
assert.NoError(t, err)
2353+
assert.Equal(t, afterRunCLI{runCalled: true, afterRunCalled: true}, cli)
2354+
}

0 commit comments

Comments
 (0)