Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Simplify cli.NewExitError #475

Closed
gravis opened this issue Jul 7, 2016 · 5 comments
Closed

Simplify cli.NewExitError #475

gravis opened this issue Jul 7, 2016 · 5 comments
Labels
kind/documentation describes a documentation update kind/question someone asking a question

Comments

@gravis
Copy link
Member

gravis commented Jul 7, 2016

Hi,

I need to update a CLI tool because we have some deprecation warnings in the output.
While I thought I could simple return errors from commands, I end up with things like:

 f, err := os.Create("config.yml")
 if err != nil {
     return cli.NewExitError(err.Error(), 1)
 }
 defer f.Close()
 err:= project.Configure(project.Slug, os.Stdin, f)
 if err != nil {
     return cli.NewExitError(err.Error(), 1)
 }
 [...]
 return nil

It would be so much easier to simply return err in case of error, and cli would display the error, and exit with code 1. Sounds like a very reasonable default, doesn't it?

@gravis
Copy link
Member Author

gravis commented Jul 7, 2016

ie, I would like to write:

 f, err := os.Create("config.yml")
 if err != nil {
     return err
 }
 defer f.Close()
 err:= project.Configure(project.Slug, os.Stdin, f)
 if err != nil {
     return err
 }
 [...]
 return nil

instead.
It would be the icing on the cake if we could define an Exiter func to change the behaviour when an action returns a non-nil error (like using red text).

@jszwedko
Copy link
Contributor

jszwedko commented Jul 8, 2016

Hi @gravis,

You can achieve this behavior yourself by doing something like:

// define and instantiate a `cli.App`

err := app.Run(os.Args)
if err != nil {
  fmt.Println(err)
  os.Exit(1)
}

I can see the argument that you are making though -- I think it makes sense, but would want to think through the drawbacks. Do you have thoughts about this @meatballhat (as the original implementer of this behavior 😄)?

(It would basically just mean adding a default behavior to

cli/errors.go

Lines 74 to 91 in 1d47add

func HandleExitCoder(err error) {
if err == nil {
return
}
if exitErr, ok := err.(ExitCoder); ok {
if err.Error() != "" {
fmt.Fprintln(ErrWriter, err)
}
OsExiter(exitErr.ExitCode())
return
}
if multiErr, ok := err.(MultiError); ok {
for _, merr := range multiErr.Errors {
HandleExitCoder(merr)
}
}
)

@gravis
Copy link
Member Author

gravis commented Jul 9, 2016

Exactly was I was looking for. I wasn't obvious to me that the error was forwarded to Run. It probably needs a small example somewhere in the doc?
Thanks :)

@meatballhat meatballhat added kind/question someone asking a question kind/documentation describes a documentation update labels Jul 10, 2016
meatballhat added a commit that referenced this issue Jul 24, 2016
@jszwedko
Copy link
Contributor

@gravis behavior was updated in #496 to match what you described (after discovering that that was the original intent). Thanks for the report!

@gravis
Copy link
Member Author

gravis commented Aug 2, 2016

Awesome!
Thanks :)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
kind/documentation describes a documentation update kind/question someone asking a question
Projects
None yet
Development

No branches or pull requests

3 participants