lotus/cli/cmd.go

71 lines
1.4 KiB
Go
Raw Normal View History

package cli
import (
"context"
"os"
"os/signal"
"syscall"
2019-07-10 17:28:49 +00:00
manet "github.com/multiformats/go-multiaddr-net"
"gopkg.in/urfave/cli.v2"
2019-07-10 17:28:49 +00:00
"github.com/filecoin-project/go-lotus/api"
"github.com/filecoin-project/go-lotus/api/client"
"github.com/filecoin-project/go-lotus/node/repo"
)
const (
metadataContext = "context"
)
// ApiConnector returns API instance
2019-07-08 19:07:16 +00:00
type ApiConnector func() api.API
2019-07-11 11:52:07 +00:00
func getAPI(ctx *cli.Context) (api.API, error) {
2019-07-10 17:28:49 +00:00
r, err := repo.NewFS(ctx.String("repo"))
if err != nil {
return nil, err
}
ma, err := r.APIEndpoint()
if err != nil {
return nil, err
}
_, addr, err := manet.DialArgs(ma)
2019-07-11 11:52:07 +00:00
if err != nil {
return nil, err
}
2019-07-12 15:29:41 +00:00
return client.NewRPC("ws://" + addr + "/rpc/v0")
}
// 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 {
// unchecked cast as if something else is in there
// 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
}
var Commands = []*cli.Command{
2019-07-12 10:17:44 +00:00
clientCmd,
2019-07-09 15:19:27 +00:00
chainCmd,
2019-07-08 19:07:16 +00:00
netCmd,
versionCmd,
2019-07-09 22:58:51 +00:00
mpoolCmd,
2019-07-11 02:36:43 +00:00
minerCmd,
2019-07-13 00:41:32 +00:00
walletCmd,
createMinerCmd,
}