Merge pull request #2551 from filecoin-project/feat/helptext-helpers

add a helper to make printing helptext on errors easier
This commit is contained in:
Łukasz Magiera 2020-07-23 21:26:01 +02:00 committed by GitHub
commit a4d85a88fe
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 51 additions and 8 deletions

46
cli/helper.go Normal file
View File

@ -0,0 +1,46 @@
package cli
import (
"fmt"
"os"
"github.com/urfave/cli/v2"
"golang.org/x/xerrors"
)
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)
}
var phe *PrintHelpErr
if xerrors.As(err, &phe) {
cli.ShowCommandHelp(phe.Ctx, phe.Ctx.Command.Name)
}
os.Exit(1)
}
}

View File

@ -3,7 +3,6 @@ package main
import (
"context"
"fmt"
"os"
logging "github.com/ipfs/go-log/v2"
"github.com/urfave/cli/v2"
@ -96,10 +95,7 @@ func main() {
app.Setup()
app.Metadata["repoType"] = repo.StorageMiner
if err := app.Run(os.Args); err != nil {
log.Warnf("%+v", err)
os.Exit(1)
}
lcli.RunApp(app)
}
func getActorAddress(ctx context.Context, nodeAPI api.StorageMiner, overrideMaddr string) (maddr address.Address, err error) {

View File

@ -251,15 +251,16 @@ var sectorsMarkForUpgradeCmd = &cli.Command{
Usage: "Mark a committed capacity sector for replacement by a sector with deals",
ArgsUsage: "<sectorNum>",
Action: func(cctx *cli.Context) error {
if cctx.Args().Len() != 1 {
return lcli.ShowHelp(cctx, xerrors.Errorf("must pass sector number"))
}
nodeApi, closer, err := lcli.GetStorageMinerAPI(cctx)
if err != nil {
return err
}
defer closer()
ctx := lcli.ReqContext(cctx)
if cctx.Args().Len() != 1 {
return xerrors.Errorf("must pass sector number")
}
id, err := strconv.ParseUint(cctx.Args().Get(0), 10, 64)
if err != nil {