Skip to content

Commit

Permalink
👔 up(cmd): add panics recover handle on run cmd.Func
Browse files Browse the repository at this point in the history
  • Loading branch information
inhere committed Feb 7, 2023
1 parent 76bdb02 commit 1c0ad15
Show file tree
Hide file tree
Showing 4 changed files with 49 additions and 17 deletions.
2 changes: 1 addition & 1 deletion app.go
Original file line number Diff line number Diff line change
Expand Up @@ -351,7 +351,7 @@ func (app *App) prepareRun() (code int, name string) {
// name is not empty, but is not command.
if app.inputName == "" {
Logf(VerbDebug, "input the command is not an registered: %s", name)
hookData := map[string]any{"name": name}
hookData := map[string]any{"name": name, "args": app.args}

// fire events
if stop := app.Fire(events.OnAppCmdNotFound, hookData); stop {
Expand Down
40 changes: 34 additions & 6 deletions cmd.go
Original file line number Diff line number Diff line change
Expand Up @@ -441,15 +441,15 @@ func (c *Command) innerDispatch(args []string) (err error) {
// is not a sub command and has no arguments -> error
if !c.HasArguments() {
// fire events
hookData := map[string]any{"name": name}
hookData := map[string]any{"name": name, "args": args[1:]}
if c.Fire(events.OnCmdSubNotFound, hookData) {
return
}
if c.Fire(events.OnCmdNotFound, hookData) {
return
}

color.Error.Tips("subcommand '%s' - not found on the command", name)
color.Error.Tips("%s - subcommand '%s' is not found", c.Name, name)
}
}
}
Expand Down Expand Up @@ -508,6 +508,15 @@ func (c *Command) prepare(_ []string) (status int, err error) {
return
}

type panicErr struct {
val any
}

// Error string
func (p panicErr) Error() string {
return fmt.Sprint(p.val)
}

// do execute the command
func (c *Command) doExecute(args []string) (err error) {
// collect and binding named argument
Expand All @@ -520,21 +529,40 @@ func (c *Command) doExecute(args []string) (err error) {

fnArgs := c.ExtraArgs()
c.Fire(events.OnCmdRunBefore, map[string]any{"args": fnArgs})
Debugf("cmd: %s - run command func with extra-args %v", c.Name, fnArgs)

// do call command handler func
if c.Func == nil {
Logf(VerbWarn, "the command '%s' no handler func to running", c.Name)
} else {
err = c.Func(c, fnArgs)
c.Fire(events.OnCmdRunAfter, nil)
return
}

Debugf("cmd: %s - run command func with extra-args %v", c.Name, fnArgs)

// recover panics
if re := recover(); re != nil {
var ok bool
err, ok = re.(error)
if !ok {
err = panicErr{val: re}
}

c.fireAfterExec(err)
return
}

// do run func
err = c.Func(c, fnArgs)
c.fireAfterExec(err)
return
}

func (c *Command) fireAfterExec(err error) {
if err != nil {
c.Fire(events.OnCmdRunError, map[string]any{"err": err})
} else {
c.Fire(events.OnCmdRunAfter, nil)
}
return
}

/*************************************************************
Expand Down
12 changes: 7 additions & 5 deletions events/events.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,12 +34,13 @@ const (
OnCmdInitBefore = "cmd.init.before"
OnCmdInitAfter = "cmd.init.after"

// OnCmdNotFound on app-command or subcommand not found.
// OnCmdNotFound on top-command or subcommand not found.
//
// Data:
// {name: command-name}
OnCmdNotFound = "cmd.not.found"
// OnAppCmdNotFound on app command not found

// OnAppCmdNotFound on top command not found
OnAppCmdNotFound = "app.cmd.not.found"
// OnCmdSubNotFound on subcommand not found
OnCmdSubNotFound = "cmd.sub.not.found"
Expand All @@ -52,15 +53,16 @@ const (

// OnCmdRunBefore cmd run
OnCmdRunBefore = "cmd.run.before"
OnCmdRunAfter = "cmd.run.after"
OnCmdRunError = "cmd.run.error"
// OnCmdRunAfter after cmd success run
OnCmdRunAfter = "cmd.run.after"
OnCmdRunError = "cmd.run.error"

// OnCmdExecBefore cmd exec
OnCmdExecBefore = "cmd.exec.before"
OnCmdExecAfter = "cmd.exec.after"
OnCmdExecError = "cmd.exec.error"

// OnGlobalOptsParsed event
// OnGlobalOptsParsed app or cmd parsed the global options
//
// Data:
// {args: remain-args}
Expand Down
12 changes: 7 additions & 5 deletions gflag/args.go
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ func (ags *CliArgs) SetValidateNum(validateNum bool) {

// ParseArgs for CliArgs
func (ags *CliArgs) ParseArgs(args []string) (err error) {
var num int
var num int // parsed num
inNum := len(args)

for i, arg := range ags.args {
Expand All @@ -73,6 +73,7 @@ func (ags *CliArgs) ParseArgs(args []string) (err error) {
if arg.Required {
return errorx.Rawf("must set value for the argument: %s(position#%d)", arg.ShowName, arg.index)
}
num = i
break
}

Expand All @@ -89,11 +90,12 @@ func (ags *CliArgs) ParseArgs(args []string) (err error) {
}
}

if ags.validateNum && inNum > num {
return errorx.Rawf("entered too many arguments: %v", args[num:])
if inNum > num {
if ags.validateNum {
return errorx.Rawf("entered too many arguments: %v", args[num:])
}
ags.remainArgs = args[num:]
}

ags.remainArgs = args[num:]
return
}

Expand Down

0 comments on commit 1c0ad15

Please sign in to comment.