From 72fe8cb4794069b8662395dc7e5b8c42b207b956 Mon Sep 17 00:00:00 2001 From: whyrusleeping Date: Thu, 23 Jul 2020 11:46:22 -0700 Subject: [PATCH] add the actual code --- cli/helper.go | 45 +++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 45 insertions(+) create mode 100644 cli/helper.go diff --git a/cli/helper.go b/cli/helper.go new file mode 100644 index 000000000..5626488e0 --- /dev/null +++ b/cli/helper.go @@ -0,0 +1,45 @@ +package cli + +import ( + "fmt" + "os" + + "github.com/urfave/cli/v2" +) + +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 { + fmt.Printf("ERROR: %s\n\n", err) + } + phe, ok := err.(*PrintHelpErr) + if ok { + cli.ShowCommandHelp(phe.Ctx, phe.Ctx.Command.Name) + } + os.Exit(1) + } +}