Skip to content

Commit bc43572

Browse files
committed
👔 up(app): update app and cmd run logic, app.RunCmd now will return error
1 parent 2804f64 commit bc43572

File tree

5 files changed

+43
-44
lines changed

5 files changed

+43
-44
lines changed

app.go

+17-9
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ import (
1111
"github.com/gookit/gcli/v3/helper"
1212
"github.com/gookit/goutil/cflag"
1313
"github.com/gookit/goutil/cliutil"
14+
"github.com/gookit/goutil/errorx"
1415
)
1516

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

485486
// do run input command
486-
code = app.doRunCmd(name, app.args)
487+
var exCode int
488+
err := app.doRunCmd(name, app.args)
489+
if err != nil {
490+
if ec, ok := err.(errorx.ErrorCoder); ok {
491+
exCode = ec.Code()
492+
}
493+
}
487494

488-
Debugf("command '%s' run complete, exit with code: %d", name, code)
489-
return app.exitOnEnd(code)
495+
Debugf("command '%s' run complete, exit with code: %d", name, exCode)
496+
return app.exitOnEnd(exCode)
490497
}
491498

492499
// RunLine manual run a command by command line string.
@@ -505,21 +512,22 @@ func (app *App) RunLine(argsLine string) int {
505512
// app.Exec("top", []string{"-a", "val0", "arg0"})
506513
// // can add sub command on args
507514
// app.Exec("top", []string{"sub", "-o", "abc"})
508-
func (app *App) RunCmd(name string, args []string) int {
509-
if app.HasCommand(name) {
510-
return ERR
515+
func (app *App) RunCmd(name string, args []string) error {
516+
if !app.HasCommand(name) {
517+
return errorx.Failf(ERR, "command %q not exists", name)
511518
}
519+
512520
return app.doRunCmd(name, args)
513521
}
514522

515-
func (app *App) doRunCmd(name string, args []string) (code int) {
523+
func (app *App) doRunCmd(name string, args []string) (err error) {
516524
cmd := app.GetCommand(name)
517525
app.fireWithCmd(events.OnAppRunBefore, cmd, map[string]any{"args": args})
518526
Debugf("will run app command '%s' with args: %v", name, args)
519527

520528
// do execute command
521-
if err := cmd.innerDispatch(args); err != nil {
522-
code = ERR
529+
if err = cmd.innerDispatch(args); err != nil {
530+
err = newRunErr(ERR, err)
523531
app.Fire(events.OnAppRunError, map[string]any{"err": err})
524532
} else {
525533
app.Fire(events.OnAppRunAfter, nil)

cmd.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -703,7 +703,7 @@ func (c *Command) App() *App {
703703
return c.app
704704
}
705705

706-
// ID get command ID string
706+
// ID get command ID string. return like: git:branch:create
707707
func (c *Command) ID() string {
708708
return strings.Join(c.pathNames, CommandSep)
709709
}

ext.go

+22
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ import (
66
"strings"
77

88
"github.com/gookit/gcli/v3/events"
9+
"github.com/gookit/goutil/errorx"
910
"github.com/gookit/goutil/maputil"
1011
)
1112

@@ -43,6 +44,27 @@ const (
4344
EvtGOptionsParsed = events.OnGlobalOptsParsed
4445
)
4546

47+
// runErr struct
48+
type runErr struct {
49+
code int
50+
err error
51+
}
52+
53+
// newRunErr instance
54+
func newRunErr(code int, err error) errorx.ErrorCoder {
55+
return &runErr{code: code, err: err}
56+
}
57+
58+
// Code for error
59+
func (e *runErr) Code() int {
60+
return e.code
61+
}
62+
63+
// Error string
64+
func (e *runErr) Error() string {
65+
return fmt.Sprintf("%v with code %d", e.err, e.code)
66+
}
67+
4668
// HookFunc definition.
4769
//
4870
// Returns:

gcli.go

+3
Original file line numberDiff line numberDiff line change
@@ -255,6 +255,9 @@ type Booleans = cflag.Booleans
255255
// EnumString The string flag list, implemented flag.Value interface
256256
type EnumString = cflag.EnumString
257257

258+
// ConfString The config-string flag, INI format, like nginx-config
259+
type ConfString = cflag.ConfString
260+
258261
// String type, a special string
259262
//
260263
// Usage:

helper/utils.go

-34
Original file line numberDiff line numberDiff line change
@@ -5,11 +5,9 @@ import (
55
"fmt"
66
"regexp"
77
"strings"
8-
"syscall"
98
"text/template"
109

1110
"github.com/gookit/goutil/strutil"
12-
"golang.org/x/term"
1311
)
1412

1513
const (
@@ -47,38 +45,6 @@ func IsGoodCmdName(name string) bool {
4745
return goodCmdName.MatchString(name)
4846
}
4947

50-
// exec: `stty -a 2>&1`
51-
// const (
52-
// mac: speed 9600 baud; 97 rows; 362 columns;
53-
// macSttyMsgPattern = `(\d+)\s+rows;\s*(\d+)\s+columns;`
54-
// linux: speed 38400 baud; rows 97; columns 362; line = 0;
55-
// linuxSttyMsgPattern = `rows\s+(\d+);\s*columns\s+(\d+);`
56-
// )
57-
58-
var (
59-
terminalWidth, terminalHeight int
60-
61-
// macSttyMsgMatch = regexp.MustCompile(macSttyMsgPattern)
62-
// linuxSttyMsgMatch = regexp.MustCompile(linuxSttyMsgPattern)
63-
)
64-
65-
// GetTerminalSize for current console terminal.
66-
func GetTerminalSize(refresh ...bool) (w int, h int) {
67-
if terminalWidth > 0 && len(refresh) > 0 && !refresh[0] {
68-
return terminalWidth, terminalHeight
69-
}
70-
71-
var err error
72-
w, h, err = term.GetSize(syscall.Stdin)
73-
if err != nil {
74-
return
75-
}
76-
77-
// cache result
78-
terminalWidth, terminalHeight = w, h
79-
return
80-
}
81-
8248
// Panicf message
8349
func Panicf(format string, v ...any) {
8450
panic(fmt.Sprintf("GCli: "+format, v...))

0 commit comments

Comments
 (0)