Skip to content

Commit

Permalink
👔 up(app): update app and cmd run logic, app.RunCmd now will return e…
Browse files Browse the repository at this point in the history
…rror
  • Loading branch information
inhere committed Feb 19, 2023
1 parent 2804f64 commit bc43572
Show file tree
Hide file tree
Showing 5 changed files with 43 additions and 44 deletions.
26 changes: 17 additions & 9 deletions app.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import (
"github.com/gookit/gcli/v3/helper"
"github.com/gookit/goutil/cflag"
"github.com/gookit/goutil/cliutil"
"github.com/gookit/goutil/errorx"
)

/*************************************************************
Expand Down Expand Up @@ -483,10 +484,16 @@ func (app *App) Run(args []string) (code int) {
app.Fire(events.OnAppPrepared, map[string]any{"name": name})

// do run input command
code = app.doRunCmd(name, app.args)
var exCode int
err := app.doRunCmd(name, app.args)
if err != nil {
if ec, ok := err.(errorx.ErrorCoder); ok {
exCode = ec.Code()
}
}

Debugf("command '%s' run complete, exit with code: %d", name, code)
return app.exitOnEnd(code)
Debugf("command '%s' run complete, exit with code: %d", name, exCode)
return app.exitOnEnd(exCode)
}

// RunLine manual run a command by command line string.
Expand All @@ -505,21 +512,22 @@ func (app *App) RunLine(argsLine string) int {
// app.Exec("top", []string{"-a", "val0", "arg0"})
// // can add sub command on args
// app.Exec("top", []string{"sub", "-o", "abc"})
func (app *App) RunCmd(name string, args []string) int {
if app.HasCommand(name) {
return ERR
func (app *App) RunCmd(name string, args []string) error {
if !app.HasCommand(name) {
return errorx.Failf(ERR, "command %q not exists", name)
}

return app.doRunCmd(name, args)
}

func (app *App) doRunCmd(name string, args []string) (code int) {
func (app *App) doRunCmd(name string, args []string) (err error) {
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)

// do execute command
if err := cmd.innerDispatch(args); err != nil {
code = ERR
if err = cmd.innerDispatch(args); err != nil {
err = newRunErr(ERR, err)
app.Fire(events.OnAppRunError, map[string]any{"err": err})
} else {
app.Fire(events.OnAppRunAfter, nil)
Expand Down
2 changes: 1 addition & 1 deletion cmd.go
Original file line number Diff line number Diff line change
Expand Up @@ -703,7 +703,7 @@ func (c *Command) App() *App {
return c.app
}

// ID get command ID string
// ID get command ID string. return like: git:branch:create
func (c *Command) ID() string {
return strings.Join(c.pathNames, CommandSep)
}
Expand Down
22 changes: 22 additions & 0 deletions ext.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import (
"strings"

"github.com/gookit/gcli/v3/events"
"github.com/gookit/goutil/errorx"
"github.com/gookit/goutil/maputil"
)

Expand Down Expand Up @@ -43,6 +44,27 @@ const (
EvtGOptionsParsed = events.OnGlobalOptsParsed
)

// runErr struct
type runErr struct {
code int
err error
}

// newRunErr instance
func newRunErr(code int, err error) errorx.ErrorCoder {
return &runErr{code: code, err: err}
}

// Code for error
func (e *runErr) Code() int {
return e.code
}

// Error string
func (e *runErr) Error() string {
return fmt.Sprintf("%v with code %d", e.err, e.code)
}

// HookFunc definition.
//
// Returns:
Expand Down
3 changes: 3 additions & 0 deletions gcli.go
Original file line number Diff line number Diff line change
Expand Up @@ -255,6 +255,9 @@ type Booleans = cflag.Booleans
// EnumString The string flag list, implemented flag.Value interface
type EnumString = cflag.EnumString

// ConfString The config-string flag, INI format, like nginx-config
type ConfString = cflag.ConfString

// String type, a special string
//
// Usage:
Expand Down
34 changes: 0 additions & 34 deletions helper/utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,9 @@ import (
"fmt"
"regexp"
"strings"
"syscall"
"text/template"

"github.com/gookit/goutil/strutil"
"golang.org/x/term"
)

const (
Expand Down Expand Up @@ -47,38 +45,6 @@ func IsGoodCmdName(name string) bool {
return goodCmdName.MatchString(name)
}

// exec: `stty -a 2>&1`
// const (
// mac: speed 9600 baud; 97 rows; 362 columns;
// macSttyMsgPattern = `(\d+)\s+rows;\s*(\d+)\s+columns;`
// linux: speed 38400 baud; rows 97; columns 362; line = 0;
// linuxSttyMsgPattern = `rows\s+(\d+);\s*columns\s+(\d+);`
// )

var (
terminalWidth, terminalHeight int

// macSttyMsgMatch = regexp.MustCompile(macSttyMsgPattern)
// linuxSttyMsgMatch = regexp.MustCompile(linuxSttyMsgPattern)
)

// GetTerminalSize for current console terminal.
func GetTerminalSize(refresh ...bool) (w int, h int) {
if terminalWidth > 0 && len(refresh) > 0 && !refresh[0] {
return terminalWidth, terminalHeight
}

var err error
w, h, err = term.GetSize(syscall.Stdin)
if err != nil {
return
}

// cache result
terminalWidth, terminalHeight = w, h
return
}

// Panicf message
func Panicf(format string, v ...any) {
panic(fmt.Sprintf("GCli: "+format, v...))
Expand Down

0 comments on commit bc43572

Please sign in to comment.