From 2544d3f00855f6857cf121ce04608296b5858e4a Mon Sep 17 00:00:00 2001 From: Alec Thomas Date: Sun, 3 Nov 2024 14:48:36 +1100 Subject: [PATCH] feat: add AfterRun() hook Fixes #288 --- context.go | 24 ++++++++++++++---------- go.mod | 2 +- go.sum | 2 -- hooks.go | 13 ++++++++++--- kong_test.go | 25 +++++++++++++++++++++++++ 5 files changed, 50 insertions(+), 16 deletions(-) diff --git a/context.go b/context.go index 38a2f9b..8a8c2e1 100644 --- a/context.go +++ b/context.go @@ -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. diff --git a/go.mod b/go.mod index a128f6e..411174d 100644 --- a/go.mod +++ b/go.mod @@ -7,4 +7,4 @@ require ( require github.com/hexops/gotextdiff v1.0.3 // indirect -go 1.18 +go 1.20 diff --git a/go.sum b/go.sum index c03ffd4..f571a34 100644 --- a/go.sum +++ b/go.sum @@ -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= diff --git a/hooks.go b/hooks.go index d166b08..9fdf24c 100644 --- a/hooks.go +++ b/hooks.go @@ -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 } diff --git a/kong_test.go b/kong_test.go index 61099d4..36e18e1 100644 --- a/kong_test.go +++ b/kong_test.go @@ -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) +}