Introduce reqContext for cli
License: MIT Signed-off-by: Jakub Sztandera <kubuxu@protonmail.ch>
This commit is contained in:
parent
d852b3f7ef
commit
40d9fbd952
34
cli/cmd.go
34
cli/cmd.go
@ -1,17 +1,47 @@
|
||||
package cli
|
||||
|
||||
import (
|
||||
"context"
|
||||
"os"
|
||||
"os/signal"
|
||||
"syscall"
|
||||
|
||||
"github.com/filecoin-project/go-lotus/api"
|
||||
"gopkg.in/urfave/cli.v2"
|
||||
)
|
||||
|
||||
const (
|
||||
metadataContext = "context"
|
||||
metadataAPI = "api"
|
||||
)
|
||||
|
||||
// ApiConnector returns API instance
|
||||
type ApiConnector func() api.API
|
||||
|
||||
func getApi(ctx *cli.Context) api.API {
|
||||
return ctx.App.Metadata["api"].(ApiConnector)()
|
||||
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 {
|
||||
// unchecked cast as if somethign 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
|
||||
}
|
||||
|
||||
// Commands is the root group of CLI commands
|
||||
var Commands = []*cli.Command{
|
||||
netCmd,
|
||||
versionCmd,
|
||||
|
Loading…
Reference in New Issue
Block a user