|
|
@ -6,26 +6,10 @@ import (
|
|
|
|
"io/ioutil"
|
|
|
|
"io/ioutil"
|
|
|
|
"os"
|
|
|
|
"os"
|
|
|
|
"path/filepath"
|
|
|
|
"path/filepath"
|
|
|
|
"reflect"
|
|
|
|
|
|
|
|
"sort"
|
|
|
|
"sort"
|
|
|
|
"time"
|
|
|
|
"time"
|
|
|
|
)
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
|
|
var (
|
|
|
|
|
|
|
|
changeLogURL = "https://github.com/codegangsta/cli/blob/master/CHANGELOG.md"
|
|
|
|
|
|
|
|
appActionDeprecationURL = fmt.Sprintf("%s#deprecated-cli-app-action-signature", changeLogURL)
|
|
|
|
|
|
|
|
runAndExitOnErrorDeprecationURL = fmt.Sprintf("%s#deprecated-cli-app-runandexitonerror", changeLogURL)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
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)+
|
|
|
|
|
|
|
|
fmt.Sprintf("See %s", appActionDeprecationURL), 2)
|
|
|
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// App is the main structure of a cli application. It is recommended that
|
|
|
|
// App is the main structure of a cli application. It is recommended that
|
|
|
|
// an app be created with the cli.NewApp() function
|
|
|
|
// an app be created with the cli.NewApp() function
|
|
|
|
type App struct {
|
|
|
|
type App struct {
|
|
|
@ -62,10 +46,7 @@ type App struct {
|
|
|
|
// It is run even if Action() panics
|
|
|
|
// It is run even if Action() panics
|
|
|
|
After AfterFunc
|
|
|
|
After AfterFunc
|
|
|
|
// The action to execute when no subcommands are specified
|
|
|
|
// The action to execute when no subcommands are specified
|
|
|
|
Action interface{}
|
|
|
|
Action ActionFunc
|
|
|
|
// TODO: replace `Action: interface{}` with `Action: ActionFunc` once some kind
|
|
|
|
|
|
|
|
// of deprecation period has passed, maybe?
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// Execute this function if the proper command cannot be found
|
|
|
|
// Execute this function if the proper command cannot be found
|
|
|
|
CommandNotFound CommandNotFoundFunc
|
|
|
|
CommandNotFound CommandNotFoundFunc
|
|
|
|
// Execute this function if an usage error occurs
|
|
|
|
// Execute this function if an usage error occurs
|
|
|
@ -76,10 +57,6 @@ type App struct {
|
|
|
|
Authors []Author
|
|
|
|
Authors []Author
|
|
|
|
// Copyright of the binary if any
|
|
|
|
// Copyright of the binary if any
|
|
|
|
Copyright string
|
|
|
|
Copyright string
|
|
|
|
// Name of Author (Note: Use App.Authors, this is deprecated)
|
|
|
|
|
|
|
|
Author string
|
|
|
|
|
|
|
|
// Email of Author (Note: Use App.Authors, this is deprecated)
|
|
|
|
|
|
|
|
Email string
|
|
|
|
|
|
|
|
// Writer writer to write output to
|
|
|
|
// Writer writer to write output to
|
|
|
|
Writer io.Writer
|
|
|
|
Writer io.Writer
|
|
|
|
// ErrWriter writes error output
|
|
|
|
// ErrWriter writes error output
|
|
|
@ -126,10 +103,6 @@ func (a *App) Setup() {
|
|
|
|
|
|
|
|
|
|
|
|
a.didSetup = true
|
|
|
|
a.didSetup = true
|
|
|
|
|
|
|
|
|
|
|
|
if a.Author != "" || a.Email != "" {
|
|
|
|
|
|
|
|
a.Authors = append(a.Authors, Author{Name: a.Author, Email: a.Email})
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
newCmds := []Command{}
|
|
|
|
newCmds := []Command{}
|
|
|
|
for _, c := range a.Commands {
|
|
|
|
for _, c := range a.Commands {
|
|
|
|
if c.HelpName == "" {
|
|
|
|
if c.HelpName == "" {
|
|
|
@ -238,23 +211,12 @@ func (a *App) Run(arguments []string) (err error) {
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// Run default Action
|
|
|
|
// Run default Action
|
|
|
|
err = HandleAction(a.Action, context)
|
|
|
|
err = a.Action(context)
|
|
|
|
|
|
|
|
|
|
|
|
HandleExitCoder(err)
|
|
|
|
HandleExitCoder(err)
|
|
|
|
return err
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// DEPRECATED: Another entry point to the cli app, takes care of passing arguments and error handling
|
|
|
|
|
|
|
|
func (a *App) RunAndExitOnError() {
|
|
|
|
|
|
|
|
fmt.Fprintf(a.errWriter(),
|
|
|
|
|
|
|
|
"DEPRECATED cli.App.RunAndExitOnError. %s See %s\n",
|
|
|
|
|
|
|
|
contactSysadmin, runAndExitOnErrorDeprecationURL)
|
|
|
|
|
|
|
|
if err := a.Run(os.Args); err != nil {
|
|
|
|
|
|
|
|
fmt.Fprintln(a.errWriter(), err)
|
|
|
|
|
|
|
|
OsExiter(1)
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// RunAsSubcommand invokes the subcommand given the context, parses ctx.Args() to
|
|
|
|
// RunAsSubcommand invokes the subcommand given the context, parses ctx.Args() to
|
|
|
|
// generate command-specific flags
|
|
|
|
// generate command-specific flags
|
|
|
|
func (a *App) RunAsSubcommand(ctx *Context) (err error) {
|
|
|
|
func (a *App) RunAsSubcommand(ctx *Context) (err error) {
|
|
|
@ -358,7 +320,7 @@ func (a *App) RunAsSubcommand(ctx *Context) (err error) {
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// Run default Action
|
|
|
|
// Run default Action
|
|
|
|
err = HandleAction(a.Action, context)
|
|
|
|
err = a.Action(context)
|
|
|
|
|
|
|
|
|
|
|
|
HandleExitCoder(err)
|
|
|
|
HandleExitCoder(err)
|
|
|
|
return err
|
|
|
|
return err
|
|
|
@ -456,43 +418,3 @@ 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
|
|
|
|
|
|
|
|
// 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!
|
|
|
|
|
|
|
|
func HandleAction(action interface{}, context *Context) (err error) {
|
|
|
|
|
|
|
|
defer func() {
|
|
|
|
|
|
|
|
if r := recover(); r != nil {
|
|
|
|
|
|
|
|
switch r.(type) {
|
|
|
|
|
|
|
|
case error:
|
|
|
|
|
|
|
|
err = r.(error)
|
|
|
|
|
|
|
|
default:
|
|
|
|
|
|
|
|
err = NewExitError(fmt.Sprintf("ERROR unknown Action error: %v. See %s", r, appActionDeprecationURL), 2)
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
}()
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
if reflect.TypeOf(action).Kind() != reflect.Func {
|
|
|
|
|
|
|
|
return errNonFuncAction
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
vals := reflect.ValueOf(action).Call([]reflect.Value{reflect.ValueOf(context)})
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
if len(vals) == 0 {
|
|
|
|
|
|
|
|
fmt.Fprintf(ErrWriter,
|
|
|
|
|
|
|
|
"DEPRECATED Action signature. Must be `cli.ActionFunc`. %s See %s\n",
|
|
|
|
|
|
|
|
contactSysadmin, appActionDeprecationURL)
|
|
|
|
|
|
|
|
return nil
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
if len(vals) > 1 {
|
|
|
|
|
|
|
|
return errInvalidActionSignature
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
if retErr, ok := vals[0].Interface().(error); vals[0].IsValid() && ok {
|
|
|
|
|
|
|
|
return retErr
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
return err
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|