cli icon indicating copy to clipboard operation
cli copied to clipboard

Different Call/CallContext which returns error to main.

Open abraithwaite opened this issue 5 years ago • 3 comments

Using this library I was trying to figure out how to get access to the error returned from the command function.

Along the way, I discovered that the error is caught in CallContext, handled and not returned.

It would be nice to instead have a function which returns both, or modify this one to return both. The motivation is this: While it's nice to be able to do os.Exit(cli.CallContext(...)), it prevents me from being able to do cleanup of anything started outside of cli.CallContext (due to the nature of os.Exit terminating the program immediately).

https://github.com/segmentio/cli/blob/39f4fd8f18431e6f7eb9dbe5740b1dbb1dad1ae6/command.go#L309-L328 https://github.com/segmentio/cli/blob/39f4fd8f18431e6f7eb9dbe5740b1dbb1dad1ae6/cli.go#L81-L95

My current workaround is to simply copy the code in cli.CallContext and modify it to meet the needs, but I think it would be useful generally.

abraithwaite avatar Apr 10 '20 14:04 abraithwaite

We're still in version 0.x so we can make API breaking changes, it seems like a good opportunity to improve the API here.

Should we have Call and CallContext return the error? And expose a function that performs the error handling they're currently doing?

achille-roussel avatar Apr 10 '20 18:04 achille-roussel

Should we have Call and CallContext return the error? And expose a function that performs the error handling they're currently doing?

I think that would be cleanest, but I'm not sure how that would look.

Maybe something like:

if err != nil && !cli.IsUsageError(err) {
   // Handle other errors
}

Doesn't seem the cleanest either, but is pretty straightforward and to the point.

abraithwaite avatar Apr 10 '20 18:04 abraithwaite

Yep, this looks good.

Maybe cli.IsUsage(err error) to follow the pattern of functions like os.IsNotExist?

achille-roussel avatar Apr 10 '20 19:04 achille-roussel