Skip to content

Commit

Permalink
👔 up: update the cmd event fire, will notify parents and app
Browse files Browse the repository at this point in the history
  • Loading branch information
inhere committed Feb 11, 2023
1 parent fa84b6f commit cf7abed
Show file tree
Hide file tree
Showing 4 changed files with 18 additions and 4 deletions.
17 changes: 16 additions & 1 deletion cmd.go
Original file line number Diff line number Diff line change
Expand Up @@ -658,9 +658,24 @@ func (c *Command) goodName() string {

// Fire event handler by name
func (c *Command) Fire(event string, data map[string]any) (stop bool) {
hookCtx := newHookCtx(event, c, data)
Debugf("cmd: %s - trigger the event: <mga>%s</>", c.Name, event)

return c.Hooks.Fire(event, newHookCtx(event, c, data))
// notify all parent commands
p := c.parent
for p != nil {
if p.Hooks.Fire(event, hookCtx) {
return true
}
p = p.parent
}

// notify to app
if c.app != nil && c.app.Hooks.Fire(event, hookCtx) {
return
}

return c.Hooks.Fire(event, hookCtx)
}

// On add hook handler for a hook event
Expand Down
2 changes: 1 addition & 1 deletion events/events.go
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ const (
// {args: command-args}
OnCmdOptParsed = "cmd.opts.parsed"

// OnCmdRunBefore cmd run
// OnCmdRunBefore cmd run, flags has been parsed.
OnCmdRunBefore = "cmd.run.before"
// OnCmdRunAfter after cmd success run
OnCmdRunAfter = "cmd.run.after"
Expand Down
1 change: 0 additions & 1 deletion ext.go
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,6 @@ const (
EvtCmdExecError = events.OnCmdExecError

EvtGOptionsParsed = events.OnGlobalOptsParsed
// EvtStop = "stop"
)

// HookFunc definition.
Expand Down
2 changes: 1 addition & 1 deletion show/table/table.go
Original file line number Diff line number Diff line change
Expand Up @@ -211,7 +211,7 @@ type Cell struct {
Align strutil.PosFlag

// Val is the cell data
Val interface{}
Val any
str string // string cache of Val
}

Expand Down

0 comments on commit cf7abed

Please sign in to comment.