|
|
@ -6,9 +6,7 @@ import (
|
|
|
|
"io/ioutil"
|
|
|
|
"io/ioutil"
|
|
|
|
"os"
|
|
|
|
"os"
|
|
|
|
"path/filepath"
|
|
|
|
"path/filepath"
|
|
|
|
"reflect"
|
|
|
|
|
|
|
|
"sort"
|
|
|
|
"sort"
|
|
|
|
"strings"
|
|
|
|
|
|
|
|
"time"
|
|
|
|
"time"
|
|
|
|
)
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
|
@ -19,11 +17,8 @@ var (
|
|
|
|
|
|
|
|
|
|
|
|
contactSysadmin = "This is an error in the application. Please contact the distributor of this application if this is not you."
|
|
|
|
contactSysadmin = "This is an error in the application. Please contact the distributor of this application if this is not you."
|
|
|
|
|
|
|
|
|
|
|
|
errNonFuncAction = NewExitError("ERROR invalid Action type. "+
|
|
|
|
errInvalidActionType = NewExitError("ERROR invalid Action type. "+
|
|
|
|
fmt.Sprintf("Must be a func of type `cli.ActionFunc`. %s", contactSysadmin)+
|
|
|
|
fmt.Sprintf("Must be `func(*Context`)` or `func(*Context) error). %s", contactSysadmin)+
|
|
|
|
fmt.Sprintf("See %s", appActionDeprecationURL), 2)
|
|
|
|
|
|
|
|
errInvalidActionSignature = NewExitError("ERROR invalid Action signature. "+
|
|
|
|
|
|
|
|
fmt.Sprintf("Must be `cli.ActionFunc`. %s", contactSysadmin)+
|
|
|
|
|
|
|
|
fmt.Sprintf("See %s", appActionDeprecationURL), 2)
|
|
|
|
fmt.Sprintf("See %s", appActionDeprecationURL), 2)
|
|
|
|
)
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
|
@ -468,41 +463,16 @@ func (a Author) String() string {
|
|
|
|
return fmt.Sprintf("%v%v", a.Name, e)
|
|
|
|
return fmt.Sprintf("%v%v", a.Name, e)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// HandleAction uses ✧✧✧reflection✧✧✧ to figure out if the given Action is an
|
|
|
|
// HandleAction attempts to figure out which Action signature was used. If
|
|
|
|
// ActionFunc, a func with the legacy signature for Action, or some other
|
|
|
|
// it's an ActionFunc or a func with the legacy signature for Action, the func
|
|
|
|
// invalid thing. If it's an ActionFunc or a func with the legacy signature for
|
|
|
|
// is run!
|
|
|
|
// Action, the func is run!
|
|
|
|
|
|
|
|
func HandleAction(action interface{}, context *Context) (err error) {
|
|
|
|
func HandleAction(action interface{}, context *Context) (err error) {
|
|
|
|
defer func() {
|
|
|
|
if a, ok := action.(func(*Context) error); ok {
|
|
|
|
if r := recover(); r != nil {
|
|
|
|
return a(context)
|
|
|
|
// Try to detect a known reflection error from *this scope*, rather than
|
|
|
|
} else if a, ok := action.(func(*Context)); ok { // deprecated function signature
|
|
|
|
// swallowing all panics that may happen when calling an Action func.
|
|
|
|
a(context)
|
|
|
|
s := fmt.Sprintf("%v", r)
|
|
|
|
|
|
|
|
if strings.HasPrefix(s, "reflect: ") && strings.Contains(s, "too many input arguments") {
|
|
|
|
|
|
|
|
err = NewExitError(fmt.Sprintf("ERROR unknown Action error: %v.", r), 2)
|
|
|
|
|
|
|
|
} else {
|
|
|
|
|
|
|
|
panic(r)
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
}()
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
if reflect.TypeOf(action).Kind() != reflect.Func {
|
|
|
|
|
|
|
|
return errNonFuncAction
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
vals := reflect.ValueOf(action).Call([]reflect.Value{reflect.ValueOf(context)})
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
if len(vals) == 0 {
|
|
|
|
|
|
|
|
return nil
|
|
|
|
return nil
|
|
|
|
|
|
|
|
} else {
|
|
|
|
|
|
|
|
return errInvalidActionType
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
if len(vals) > 1 {
|
|
|
|
|
|
|
|
return errInvalidActionSignature
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
if retErr, ok := vals[0].Interface().(error); vals[0].IsValid() && ok {
|
|
|
|
|
|
|
|
return retErr
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
return err
|
|
|
|
|
|
|
|
}
|
|
|
|
}
|
|
|
|