2020-07-23 18:46:22 +00:00
|
|
|
package cli
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"os"
|
|
|
|
|
|
|
|
"github.com/urfave/cli/v2"
|
2020-07-23 18:48:48 +00:00
|
|
|
"golang.org/x/xerrors"
|
2020-07-23 18:46:22 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
type PrintHelpErr struct {
|
|
|
|
Err error
|
|
|
|
Ctx *cli.Context
|
|
|
|
}
|
|
|
|
|
|
|
|
func (e *PrintHelpErr) Error() string {
|
|
|
|
return e.Err.Error()
|
|
|
|
}
|
|
|
|
|
|
|
|
func (e *PrintHelpErr) Unwrap() error {
|
|
|
|
return e.Err
|
|
|
|
}
|
|
|
|
|
|
|
|
func (e *PrintHelpErr) Is(o error) bool {
|
|
|
|
_, ok := o.(*PrintHelpErr)
|
|
|
|
return ok
|
|
|
|
}
|
|
|
|
|
|
|
|
func ShowHelp(cctx *cli.Context, err error) error {
|
|
|
|
return &PrintHelpErr{Err: err, Ctx: cctx}
|
|
|
|
}
|
|
|
|
|
|
|
|
func RunApp(app *cli.App) {
|
|
|
|
if err := app.Run(os.Args); err != nil {
|
|
|
|
if os.Getenv("LOTUS_DEV") != "" {
|
|
|
|
log.Warnf("%+v", err)
|
|
|
|
} else {
|
2020-10-10 13:38:14 +00:00
|
|
|
fmt.Fprintf(os.Stderr, "ERROR: %s\n\n", err) // nolint:errcheck
|
2020-07-23 18:46:22 +00:00
|
|
|
}
|
2020-07-23 18:48:48 +00:00
|
|
|
var phe *PrintHelpErr
|
|
|
|
if xerrors.As(err, &phe) {
|
2020-08-20 04:49:10 +00:00
|
|
|
_ = cli.ShowCommandHelp(phe.Ctx, phe.Ctx.Command.Name)
|
2020-07-23 18:46:22 +00:00
|
|
|
}
|
|
|
|
os.Exit(1)
|
|
|
|
}
|
|
|
|
}
|