|
| 1 | +package lotus |
| 2 | + |
| 3 | +import ( |
| 4 | + "context" |
| 5 | + "os" |
| 6 | + |
| 7 | + "github.com/fatih/color" |
| 8 | + logging "github.com/ipfs/go-log/v2" |
| 9 | + "github.com/mattn/go-isatty" |
| 10 | + "github.com/urfave/cli/v2" |
| 11 | + "go.opencensus.io/trace" |
| 12 | + |
| 13 | + "github.com/filecoin-project/lotus/api" |
| 14 | + "github.com/filecoin-project/lotus/build" |
| 15 | + "github.com/filecoin-project/lotus/cli/clicommands" |
| 16 | + cliutil "github.com/filecoin-project/lotus/cli/util" |
| 17 | + "github.com/filecoin-project/lotus/lib/lotuslog" |
| 18 | + "github.com/filecoin-project/lotus/lib/tracing" |
| 19 | + "github.com/filecoin-project/lotus/node/repo" |
| 20 | +) |
| 21 | + |
| 22 | +var log = logging.Logger("lotus") |
| 23 | + |
| 24 | +var AdvanceBlockCmd *cli.Command |
| 25 | + |
| 26 | +func App() *cli.App { |
| 27 | + api.RunningNodeType = api.NodeFull |
| 28 | + |
| 29 | + lotuslog.SetupLogLevels() |
| 30 | + |
| 31 | + local := []*cli.Command{ |
| 32 | + DaemonCmd, |
| 33 | + backupCmd, |
| 34 | + configCmd, |
| 35 | + } |
| 36 | + if AdvanceBlockCmd != nil { |
| 37 | + local = append(local, AdvanceBlockCmd) |
| 38 | + } |
| 39 | + |
| 40 | + jaeger := tracing.SetupJaegerTracing("lotus") |
| 41 | + defer func() { |
| 42 | + if jaeger != nil { |
| 43 | + _ = jaeger.ForceFlush(context.Background()) |
| 44 | + } |
| 45 | + }() |
| 46 | + |
| 47 | + for _, cmd := range local { |
| 48 | + cmd := cmd |
| 49 | + originBefore := cmd.Before |
| 50 | + cmd.Before = func(cctx *cli.Context) error { |
| 51 | + if jaeger != nil { |
| 52 | + _ = jaeger.Shutdown(cctx.Context) |
| 53 | + } |
| 54 | + jaeger = tracing.SetupJaegerTracing("lotus/" + cmd.Name) |
| 55 | + |
| 56 | + if cctx.IsSet("color") { |
| 57 | + color.NoColor = !cctx.Bool("color") |
| 58 | + } |
| 59 | + |
| 60 | + if originBefore != nil { |
| 61 | + return originBefore(cctx) |
| 62 | + } |
| 63 | + return nil |
| 64 | + } |
| 65 | + } |
| 66 | + ctx, span := trace.StartSpan(context.Background(), "/cli") |
| 67 | + defer span.End() |
| 68 | + |
| 69 | + interactiveDef := isatty.IsTerminal(os.Stdout.Fd()) || isatty.IsCygwinTerminal(os.Stdout.Fd()) |
| 70 | + |
| 71 | + app := &cli.App{ |
| 72 | + Name: "lotus", |
| 73 | + Usage: "Filecoin decentralized storage network client", |
| 74 | + Version: string(build.NodeUserVersion()), |
| 75 | + EnableBashCompletion: true, |
| 76 | + Flags: []cli.Flag{ |
| 77 | + &cli.StringFlag{ |
| 78 | + Name: "panic-reports", |
| 79 | + EnvVars: []string{"LOTUS_PANIC_REPORT_PATH"}, |
| 80 | + Hidden: true, |
| 81 | + Value: "~/.lotus", // should follow --repo default |
| 82 | + }, |
| 83 | + &cli.BoolFlag{ |
| 84 | + // examined in the Before above |
| 85 | + Name: "color", |
| 86 | + Usage: "use color in display output", |
| 87 | + DefaultText: "depends on output being a TTY", |
| 88 | + }, |
| 89 | + &cli.StringFlag{ |
| 90 | + Name: "repo", |
| 91 | + EnvVars: []string{"LOTUS_PATH"}, |
| 92 | + Hidden: true, |
| 93 | + Value: "~/.lotus", // TODO: Consider XDG_DATA_HOME |
| 94 | + }, |
| 95 | + &cli.BoolFlag{ |
| 96 | + Name: "interactive", |
| 97 | + Usage: "setting to false will disable interactive functionality of commands", |
| 98 | + Value: interactiveDef, |
| 99 | + }, |
| 100 | + &cli.BoolFlag{ |
| 101 | + Name: "force-send", |
| 102 | + Usage: "if true, will ignore pre-send checks", |
| 103 | + }, |
| 104 | + cliutil.FlagVeryVerbose, |
| 105 | + }, |
| 106 | + After: func(c *cli.Context) error { |
| 107 | + if r := recover(); r != nil { |
| 108 | + // Generate report in LOTUS_PATH and re-raise panic |
| 109 | + build.GenerateNodePanicReport(c.String("panic-reports"), c.String("repo"), c.App.Name) |
| 110 | + panic(r) |
| 111 | + } |
| 112 | + return nil |
| 113 | + }, |
| 114 | + |
| 115 | + Commands: append(local, clicommands.Commands...), |
| 116 | + } |
| 117 | + |
| 118 | + app.Setup() |
| 119 | + app.Metadata["traceContext"] = ctx |
| 120 | + app.Metadata["repoType"] = repo.FullNode |
| 121 | + |
| 122 | + return app |
| 123 | +} |
0 commit comments