@ -6,9 +6,7 @@ import (
"io/ioutil"
"os"
"path/filepath"
"reflect"
"sort"
"strings"
"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."
errNonFuncAction = NewExitError ( "ERROR invalid Action type. " +
fmt . Sprintf ( "Must be a func of type `cli.ActionFunc`. %s" , contactSysadmin ) +
fmt . Sprintf ( "See %s" , appActionDeprecationURL ) , 2 )
errInvalidActionSignature = NewExitError ( "ERROR invalid Action signature. " +
fmt . Sprintf ( "Must be `cli.ActionFunc`. %s" , contactSysadmin ) +
errInvalidActionType = NewExitError ( "ERROR invalid Action type. " +
fmt . Sprintf ( "Must be `func(*Context`)` or `func(*Context) error). %s" , contactSysadmin ) +
fmt . Sprintf ( "See %s" , appActionDeprecationURL ) , 2 )
)
@ -468,41 +463,16 @@ func (a Author) String() string {
return fmt . Sprintf ( "%v%v" , a . Name , e )
}
// HandleAction uses ✧✧✧reflection✧✧✧ to figure out if the given Action is an
// ActionFunc, a func with the legacy signature for Action, or some other
// invalid thing. If it's an ActionFunc or a func with the legacy signature for
// Action, the func is run!
// HandleAction attempts to figure out which Action signature was used. If
// it's an ActionFunc or a func with the legacy signature for Action, the func
// is run!
func HandleAction ( action interface { } , context * Context ) ( err error ) {
defer func ( ) {
if r := recover ( ) ; r != nil {
// Try to detect a known reflection error from *this scope*, rather than
// swallowing all panics that may happen when calling an Action func.
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 {
if a , ok := action . ( func ( * Context ) error ) ; ok {
return a ( context )
} else if a , ok := action . ( func ( * Context ) ) ; ok { // deprecated function signature
a ( context )
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
}