2019-06-25 11:42:17 +00:00
|
|
|
package cli
|
|
|
|
|
|
|
|
import (
|
2019-07-09 12:08:43 +00:00
|
|
|
"context"
|
|
|
|
"os"
|
|
|
|
"os/signal"
|
|
|
|
"syscall"
|
|
|
|
|
2019-07-08 19:07:16 +00:00
|
|
|
"github.com/filecoin-project/go-lotus/api"
|
2019-06-25 11:42:17 +00:00
|
|
|
"gopkg.in/urfave/cli.v2"
|
|
|
|
)
|
|
|
|
|
2019-07-09 12:08:43 +00:00
|
|
|
const (
|
|
|
|
metadataContext = "context"
|
|
|
|
metadataAPI = "api"
|
|
|
|
)
|
|
|
|
|
|
|
|
// ApiConnector returns API instance
|
2019-07-08 19:07:16 +00:00
|
|
|
type ApiConnector func() api.API
|
|
|
|
|
|
|
|
func getApi(ctx *cli.Context) api.API {
|
2019-07-09 12:08:43 +00:00
|
|
|
return ctx.App.Metadata[metadataAPI].(ApiConnector)()
|
|
|
|
}
|
|
|
|
|
|
|
|
// reqContext returns context for cli execution. Calling it for the first time
|
|
|
|
// installs SIGTERM handler that will close returned context.
|
|
|
|
// Not safe for concurrent execution.
|
|
|
|
func reqContext(cctx *cli.Context) context.Context {
|
|
|
|
if uctx, ok := cctx.App.Metadata[metadataContext]; ok {
|
2019-07-09 12:46:06 +00:00
|
|
|
// unchecked cast as if something else is in there
|
2019-07-09 12:08:43 +00:00
|
|
|
// it is crash worthy either way
|
|
|
|
return uctx.(context.Context)
|
|
|
|
}
|
|
|
|
ctx, done := context.WithCancel(context.Background())
|
|
|
|
sigChan := make(chan os.Signal, 2)
|
|
|
|
go func() {
|
|
|
|
<-sigChan
|
|
|
|
done()
|
|
|
|
}()
|
|
|
|
signal.Notify(sigChan, syscall.SIGTERM, syscall.SIGINT)
|
|
|
|
|
|
|
|
return ctx
|
2019-07-08 19:07:16 +00:00
|
|
|
}
|
|
|
|
|
2019-06-25 11:42:17 +00:00
|
|
|
var Commands = []*cli.Command{
|
2019-07-09 15:19:27 +00:00
|
|
|
chainCmd,
|
2019-07-08 19:07:16 +00:00
|
|
|
netCmd,
|
2019-06-25 11:42:17 +00:00
|
|
|
versionCmd,
|
2019-07-09 22:58:51 +00:00
|
|
|
mpoolCmd,
|
2019-07-11 02:36:43 +00:00
|
|
|
minerCmd,
|
2019-06-28 09:03:28 +00:00
|
|
|
}
|