From 96aa8944e6e53df31927daa61061ca58d4495e19 Mon Sep 17 00:00:00 2001 From: Inhere Date: Tue, 4 Oct 2022 12:57:07 +0800 Subject: [PATCH] up: update some app and cmd event fire logic --- app.go | 16 ++++------------ base.go | 2 -- base_test.go | 4 ++-- cmd.go | 29 ++++++++++++----------------- events/events.go | 13 ++++++++++--- help.go | 3 ++- hooks.go | 4 ++-- 7 files changed, 32 insertions(+), 39 deletions(-) diff --git a/app.go b/app.go index e462577..5bb27ca 100644 --- a/app.go +++ b/app.go @@ -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() @@ -165,7 +166,6 @@ func (app *App) initialize() { } app.Fire(events.OnAppInitAfter, nil) - app.initialized = true } // binding app options @@ -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) @@ -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) - // } } /************************************************************* @@ -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 @@ -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) @@ -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) diff --git a/base.go b/base.go index 2831870..45a9483 100644 --- a/base.go +++ b/base.go @@ -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 } diff --git a/base_test.go b/base_test.go index 8b6c149..b708f33 100644 --- a/base_test.go +++ b/base_test.go @@ -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 + ";") diff --git a/cmd.go b/cmd.go index 1776a8b..bd035bf 100644 --- a/cmd.go +++ b/cmd.go @@ -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 @@ -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 @@ -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 @@ -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: @@ -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) @@ -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}) @@ -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 } @@ -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 @@ -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) } diff --git a/events/events.go b/events/events.go index 4c87cf8..4c390e3 100644 --- a/events/events.go +++ b/events/events.go @@ -2,18 +2,21 @@ 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 // @@ -21,11 +24,15 @@ const ( // {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. // diff --git a/help.go b/help.go index f3b15c5..3acc7ec 100644 --- a/help.go +++ b/help.go @@ -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 @@ -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 } diff --git a/hooks.go b/hooks.go index e07bc79..51a6667 100644 --- a/hooks.go +++ b/hooks.go @@ -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