Merge remote-tracking branch 'origin/v2' into remove-global-lookups

This commit is contained in:
Dan Buch 2016-05-17 20:26:30 -04:00
commit 1925bb4e3b
No known key found for this signature in database
GPG Key ID: FAEF12936DD3E3EC
5 changed files with 11 additions and 173 deletions

View File

@ -23,6 +23,8 @@
arguments](https://github.com/codegangsta/cli/issues/355) when the user arguments](https://github.com/codegangsta/cli/issues/355) when the user
attempted to mix flags and arguments. Given the trade-offs we removed support attempted to mix flags and arguments. Given the trade-offs we removed support
for this reordering. for this reordering.
- adapter code for deprecated `Action` func signature
- deprecated `App.Author`, `App.Email`, and `Command.ShortName` fields
- All `Context.Global*` methods, as the non-global versions now traverse up - All `Context.Global*` methods, as the non-global versions now traverse up
the context lineage automatically. the context lineage automatically.

84
app.go
View File

@ -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
}

View File

@ -31,9 +31,7 @@ func ExampleApp_Run() {
return nil return nil
} }
app.UsageText = "app [first_arg] [second_arg]" app.UsageText = "app [first_arg] [second_arg]"
app.Author = "Harrison" app.Authors = []Author{{Name: "Oliver Allen", Email: "oliver@toyshop.example.com"}}
app.Email = "harrison@lolwut.com"
app.Authors = []Author{{Name: "Oliver Allen", Email: "oliver@toyshop.com"}}
app.Run(os.Args) app.Run(os.Args)
// Output: // Output:
// Hello Jeremy // Hello Jeremy
@ -1357,75 +1355,3 @@ func TestApp_OnUsageError_WithWrongFlagValue_ForSubcommand(t *testing.T) {
t.Errorf("Expect an intercepted error, but got \"%v\"", err) t.Errorf("Expect an intercepted error, but got \"%v\"", err)
} }
} }
func TestHandleAction_WithNonFuncAction(t *testing.T) {
app := NewApp()
app.Action = 42
err := HandleAction(app.Action, NewContext(app, flagSet(app.Name, app.Flags), nil))
if err == nil {
t.Fatalf("expected to receive error from Run, got none")
}
exitErr, ok := err.(*ExitError)
if !ok {
t.Fatalf("expected to receive a *ExitError")
}
if !strings.HasPrefix(exitErr.Error(), "ERROR invalid Action type") {
t.Fatalf("expected an unknown Action error, but got: %v", exitErr.Error())
}
if exitErr.ExitCode() != 2 {
t.Fatalf("expected error exit code to be 2, but got: %v", exitErr.ExitCode())
}
}
func TestHandleAction_WithInvalidFuncSignature(t *testing.T) {
app := NewApp()
app.Action = func() string { return "" }
err := HandleAction(app.Action, NewContext(app, flagSet(app.Name, app.Flags), nil))
if err == nil {
t.Fatalf("expected to receive error from Run, got none")
}
exitErr, ok := err.(*ExitError)
if !ok {
t.Fatalf("expected to receive a *ExitError")
}
if !strings.HasPrefix(exitErr.Error(), "ERROR unknown Action error") {
t.Fatalf("expected an unknown Action error, but got: %v", exitErr.Error())
}
if exitErr.ExitCode() != 2 {
t.Fatalf("expected error exit code to be 2, but got: %v", exitErr.ExitCode())
}
}
func TestHandleAction_WithInvalidFuncReturnSignature(t *testing.T) {
app := NewApp()
app.Action = func(_ *Context) (int, error) { return 0, nil }
err := HandleAction(app.Action, NewContext(app, flagSet(app.Name, app.Flags), nil))
if err == nil {
t.Fatalf("expected to receive error from Run, got none")
}
exitErr, ok := err.(*ExitError)
if !ok {
t.Fatalf("expected to receive a *ExitError")
}
if !strings.HasPrefix(exitErr.Error(), "ERROR invalid Action signature") {
t.Fatalf("expected an invalid Action signature error, but got: %v", exitErr.Error())
}
if exitErr.ExitCode() != 2 {
t.Fatalf("expected error exit code to be 2, but got: %v", exitErr.ExitCode())
}
}

View File

@ -11,8 +11,6 @@ import (
type Command struct { type Command struct {
// The name of the command // The name of the command
Name string Name string
// short name of the command. Typically one character (deprecated, use `Aliases`)
ShortName string
// A list of aliases for the command // A list of aliases for the command
Aliases []string Aliases []string
// A short description of the usage of this command // A short description of the usage of this command
@ -34,10 +32,7 @@ type Command struct {
// It is run even if Action() panics // It is run even if Action() panics
After AfterFunc After AfterFunc
// The function to call when this command is invoked // The function to call when this command is invoked
Action interface{} Action ActionFunc
// TODO: replace `Action: interface{}` with `Action: ActionFunc` once some kind
// of deprecation period has passed, maybe?
// Execute this function if a usage error occurs. // Execute this function if a usage error occurs.
OnUsageError OnUsageErrorFunc OnUsageError OnUsageErrorFunc
// List of child commands // List of child commands
@ -151,7 +146,7 @@ func (c Command) Run(ctx *Context) (err error) {
} }
context.Command = c context.Command = c
err = HandleAction(c.Action, context) err = c.Action(context)
if err != nil { if err != nil {
HandleExitCoder(err) HandleExitCoder(err)
@ -162,15 +157,10 @@ func (c Command) Run(ctx *Context) (err error) {
// Names returns the names including short names and aliases. // Names returns the names including short names and aliases.
func (c Command) Names() []string { func (c Command) Names() []string {
names := []string{c.Name} names := []string{c.Name}
if c.ShortName != "" {
names = append(names, c.ShortName)
}
return append(names, c.Aliases...) return append(names, c.Aliases...)
} }
// HasName returns true if Command.Name or Command.ShortName matches given name // HasName returns true if Command.Name matches given name
func (c Command) HasName(name string) bool { func (c Command) HasName(name string) bool {
for _, n := range c.Names() { for _, n := range c.Names() {
if n == name { if n == name {
@ -208,8 +198,6 @@ func (c Command) startApp(ctx *Context) error {
app.Version = ctx.App.Version app.Version = ctx.App.Version
app.HideVersion = ctx.App.HideVersion app.HideVersion = ctx.App.HideVersion
app.Compiled = ctx.App.Compiled app.Compiled = ctx.App.Compiled
app.Author = ctx.App.Author
app.Email = ctx.App.Email
app.Writer = ctx.App.Writer app.Writer = ctx.App.Writer
app.categories = CommandCategories{} app.categories = CommandCategories{}

View File

@ -121,7 +121,7 @@ func Test_helpCommand_Action_ErrorIfNoTopic(t *testing.T) {
c := NewContext(app, set, nil) c := NewContext(app, set, nil)
err := helpCommand.Action.(func(*Context) error)(c) err := helpCommand.Action(c)
if err == nil { if err == nil {
t.Fatalf("expected error from helpCommand.Action(), but got nil") t.Fatalf("expected error from helpCommand.Action(), but got nil")
@ -149,7 +149,7 @@ func Test_helpSubcommand_Action_ErrorIfNoTopic(t *testing.T) {
c := NewContext(app, set, nil) c := NewContext(app, set, nil)
err := helpSubcommand.Action.(func(*Context) error)(c) err := helpSubcommand.Action(c)
if err == nil { if err == nil {
t.Fatalf("expected error from helpCommand.Action(), but got nil") t.Fatalf("expected error from helpCommand.Action(), but got nil")