feat: add AfterRun() hook

Fixes #288
This commit is contained in:
Alec Thomas
2024-11-03 14:48:36 +11:00
parent d0beaf7df3
commit 2544d3f008
5 changed files with 50 additions and 16 deletions
+14 -10
View File
@@ -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.
+1 -1
View File
@@ -7,4 +7,4 @@ require (
require github.com/hexops/gotextdiff v1.0.3 // indirect
go 1.18
go 1.20
-2
View File
@@ -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=
+10 -3
View File
@@ -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
View File
@@ -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)
}