Skip to content

Commit

Permalink
up: update some app and cmd event fire logic
Browse files Browse the repository at this point in the history
  • Loading branch information
inhere committed Oct 4, 2022
1 parent 08e163b commit 96aa894
Show file tree
Hide file tree
Showing 7 changed files with 32 additions and 39 deletions.
16 changes: 4 additions & 12 deletions app.go
Original file line number Diff line number Diff line change
Expand Up @@ -152,8 +152,9 @@ func (app *App) initialize() {
return
}

Logf(VerbCrazy, "initialize the cli application")
app.initialized = true
app.Fire(events.OnAppInitBefore, nil)
Logf(VerbCrazy, "initialize the cli application")

// init some info
app.initHelpVars()
Expand All @@ -165,7 +166,6 @@ func (app *App) initialize() {
}

app.Fire(events.OnAppInitAfter, nil)
app.initialized = true
}

// binding app options
Expand All @@ -174,9 +174,6 @@ func (app *App) bindAppOpts() {
// global options flag
fs := app.fs
app.Fire(events.OnAppBindOptsBefore, nil)
// if app.BeforeAddOpts != nil {
// app.BeforeAddOpts(fs)
// }

// binding global options
app.opts.bindingOpts(fs)
Expand All @@ -191,9 +188,6 @@ func (app *App) bindAppOpts() {

// support binding custom global options
app.Fire(events.OnAppBindOptsAfter, nil)
// if app.AfterAddOpts != nil {
// app.AfterAddOpts(app)
// }
}

/*************************************************************
Expand Down Expand Up @@ -224,7 +218,7 @@ func (app *App) AddCommand(c *Command) {

// do add command
app.addCommand(app.Name, c)
app.fireWithCmd(events.OnCmdInit, c, nil)
app.fireWithCmd(events.OnAppCmdAdded, c, nil)
}

// AddHandler to the application
Expand Down Expand Up @@ -491,8 +485,7 @@ func (app *App) Run(args []string) (code int) {
return app.exitOnEnd(code)
}

// trigger event
app.Fire(events.OnAppPrepareAfter, map[string]any{"name": name})
app.Fire(events.OnAppPrepared, map[string]any{"name": name})

// do run input command
code = app.doRunCmd(name, app.args)
Expand Down Expand Up @@ -526,7 +519,6 @@ func (app *App) RunCmd(name string, args []string) int {

func (app *App) doRunCmd(name string, args []string) (code int) {
cmd := app.GetCommand(name)

app.fireWithCmd(events.OnAppRunBefore, cmd, map[string]any{"args": args})
Debugf("will run app command '%s' with args: %v", name, args)

Expand Down
2 changes: 0 additions & 2 deletions base.go
Original file line number Diff line number Diff line change
Expand Up @@ -301,9 +301,7 @@ func (b *base) addCommand(pName string, c *Command) {
Logf(VerbCrazy, "register command '%s'(parent: %s), aliases: %v", cName, pName, c.Aliases)
b.cmdAliases.AddAliases(c.Name, c.Aliases)

// c.app = app
// inherit global flags from application
// c.core.gFlags = app.gFlags
// append
b.commands[cName] = c
}
Expand Down
4 changes: 2 additions & 2 deletions base_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -47,8 +47,8 @@ func TestApp_Hooks_EvtCmdInit(t *testing.T) {
buf.Reset()

cli := newNotExitApp()
cli.On(events.OnCmdInit, func(ctx *gcli.HookCtx) (stop bool) {
buf.WriteString(events.OnCmdInit)
cli.On(events.OnCmdInitAfter, func(ctx *gcli.HookCtx) (stop bool) {
buf.WriteString(events.OnCmdInitAfter)
buf.WriteString(":")

buf.WriteString(ctx.Cmd.Name + ";")
Expand Down
29 changes: 12 additions & 17 deletions cmd.go
Original file line number Diff line number Diff line change
Expand Up @@ -247,6 +247,7 @@ func (c *Command) initialize() {

// check command name
cName := c.goodName()
c.Fire(events.OnCmdInitBefore, nil)
Debugf("initialize the command '%s': init flags, run config func", cName)

c.initialized = true
Expand Down Expand Up @@ -274,12 +275,12 @@ func (c *Command) initialize() {
}
}

// call Config func
// call config func
if c.Config != nil {
c.Config(c)
}

c.Fire(events.OnCmdInit, nil)
c.Fire(events.OnCmdInitAfter, nil)
}

// init base, ctx
Expand All @@ -295,9 +296,9 @@ func (c *Command) initCommandBase(cName string) {
c.Context = gCtx
}

c.initHelpVars()

binWithPath := c.binName + " " + c.Path()

c.initHelpVars()
c.AddVars(map[string]string{
"cmd": cName,
// binName with command name
Expand Down Expand Up @@ -335,9 +336,6 @@ func (c *Command) Next() {
* standalone running
*************************************************************/

// var errCallRunOnApp = errors.New("c.Run() method can only be called in standalone mode")
// var errCallRunOnSub = errors.New("c.Run() cannot allow call at subcommand")

// MustRun Alone the current command, will panic on error
//
// Usage:
Expand Down Expand Up @@ -405,8 +403,7 @@ func (c *Command) innerDispatch(args []string) (err error) {
if err != nil {
if err == flag.ErrHelp {
Debugf("cmd: %s - parse opts return flag.ErrHelp, render command help", c.Name)
c.ShowHelp()
return nil
return c.ShowHelp()
}

Debugf("cmd: %s - command options parse error", c.Name)
Expand All @@ -418,8 +415,7 @@ func (c *Command) innerDispatch(args []string) (err error) {
if c.standalone {
if gOpts.ShowHelp {
Debugf("cmd: %s - gOpts.ShowHelp is True, render command help", c.Name)
c.ShowHelp()
return
return c.ShowHelp()
}

c.Fire(events.OnGlobalOptsParsed, map[string]any{"args": args})
Expand All @@ -438,17 +434,18 @@ func (c *Command) innerDispatch(args []string) (err error) {

// is valid sub command
if sub, has := c.Command(name); has {
// loop find sub...command and run it.
// TIP: loop find sub...command and run it.
return sub.innerDispatch(args[1:])
}

// is not a sub command and has no arguments -> error
if !c.HasArguments() {
// fire events
if stop := c.Fire(events.OnCmdSubNotFound, map[string]any{"name": name}); stop {
hookData := map[string]any{"name": name}
if c.Fire(events.OnCmdSubNotFound, hookData) {
return
}
if stop := c.Fire(events.OnCmdNotFound, map[string]any{"name": name}); stop {
if c.Fire(events.OnCmdNotFound, hookData) {
return
}

Expand All @@ -460,8 +457,7 @@ func (c *Command) innerDispatch(args []string) (err error) {
// not set command func and has sub commands.
if c.Func == nil && len(c.commands) > 0 {
Logf(VerbWarn, "cmd: %s - c.Func is empty, but has subcommands, render help", c.Name)
c.ShowHelp()
return err
return c.ShowHelp()
}

// do execute current command
Expand Down Expand Up @@ -528,7 +524,6 @@ func (c *Command) doExecute(args []string) (err error) {
if c.Func == nil {
Logf(VerbWarn, "the command '%s' no handler func to running", c.Name)
} else {
// err := c.Func.Run(c, args)
err = c.Func(c, args)
}

Expand Down
13 changes: 10 additions & 3 deletions events/events.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,30 +2,37 @@ package events

// constants for hooks event, there are default allowed event names
const (
// OnAppInitBefore On app init before
OnAppInitBefore = "app.init.before"
// OnAppInitAfter On app inited
// OnAppInitAfter On app init after
OnAppInitAfter = "app.init.after"
// OnAppInit event
// Deprecated: please use OnAppInitAfter
OnAppInit = OnAppInitAfter
// OnAppStop = "app.stopped"

// OnAppBindOptsBefore bind app options
OnAppBindOptsBefore = "app.bind.opts.before"
OnAppBindOptsAfter = "app.bind.opts.after"

OnAppPrepareAfter = "app.prepare.after"
// OnAppCmdAdded on app cmd added
OnAppCmdAdded = "app.cmd.added"

// OnAppOptsParsed event
//
// Data:
// {args: app-args}
OnAppOptsParsed = "app.opts.parsed"

// OnAppPrepared prepare for run
OnAppPrepared = "app.run.prepared"

OnAppRunBefore = "app.run.before"
OnAppRunAfter = "app.run.after"
OnAppRunError = "app.run.error"

OnCmdInit = "cmd.init"
OnCmdInitBefore = "cmd.init.before"
OnCmdInitAfter = "cmd.init.after"

// OnCmdNotFound app or sub command not found.
//
Expand Down
3 changes: 2 additions & 1 deletion help.go
Original file line number Diff line number Diff line change
Expand Up @@ -194,7 +194,7 @@ var CmdHelpTemplate = `{{.Desc}}
{{.Cmd.Help}}{{end}}`

// ShowHelp show command help info
func (c *Command) ShowHelp() {
func (c *Command) ShowHelp() (err error) {
Debugf("render the command '%s' help information", c.Name)

// custom help render func
Expand Down Expand Up @@ -239,4 +239,5 @@ func (c *Command) ShowHelp() {
// parse gcli help vars then print help
// fmt.Printf("%#v\n", s)
color.Print(c.ReplaceVars(str))
return
}
4 changes: 2 additions & 2 deletions hooks.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,13 +11,13 @@ import (
const (
EvtAppInit = events.OnAppInitAfter

EvtAppPrepareAfter = events.OnAppPrepareAfter
EvtAppPrepareAfter = events.OnAppPrepared

EvtAppRunBefore = events.OnAppRunBefore
EvtAppRunAfter = events.OnAppRunAfter
EvtAppRunError = events.OnAppRunError

EvtCmdInit = events.OnCmdInit
EvtCmdInit = events.OnCmdInitAfter

// EvtCmdNotFound app or sub command not found
EvtCmdNotFound = events.OnCmdNotFound
Expand Down

0 comments on commit 96aa894

Please sign in to comment.