2020-06-17 18:13:37 +00:00
|
|
|
/*
|
|
|
|
Package options contains a set of common CLI options and helper functions to use them.
|
|
|
|
*/
|
|
|
|
package options
|
|
|
|
|
|
|
|
import (
|
2020-06-17 21:15:13 +00:00
|
|
|
"context"
|
|
|
|
"errors"
|
|
|
|
"time"
|
|
|
|
|
2020-06-17 18:13:37 +00:00
|
|
|
"github.com/nspcc-dev/neo-go/pkg/config/netmode"
|
2022-07-21 19:39:53 +00:00
|
|
|
"github.com/nspcc-dev/neo-go/pkg/rpcclient"
|
2020-06-17 18:13:37 +00:00
|
|
|
"github.com/urfave/cli"
|
|
|
|
)
|
|
|
|
|
2020-06-17 21:15:13 +00:00
|
|
|
// DefaultTimeout is the default timeout used for RPC requests.
|
|
|
|
const DefaultTimeout = 10 * time.Second
|
|
|
|
|
2022-04-20 18:30:09 +00:00
|
|
|
// RPCEndpointFlag is a long flag name for an RPC endpoint. It can be used to
|
2020-06-17 21:15:13 +00:00
|
|
|
// check for flag presence in the context.
|
|
|
|
const RPCEndpointFlag = "rpc-endpoint"
|
|
|
|
|
2020-06-17 18:13:37 +00:00
|
|
|
// Network is a set of flags for choosing the network to operate on
|
|
|
|
// (privnet/mainnet/testnet).
|
2020-10-21 14:38:35 +00:00
|
|
|
var Network = []cli.Flag{
|
|
|
|
cli.BoolFlag{Name: "privnet, p"},
|
|
|
|
cli.BoolFlag{Name: "mainnet, m"},
|
|
|
|
cli.BoolFlag{Name: "testnet, t"},
|
|
|
|
cli.BoolFlag{Name: "unittest", Hidden: true},
|
|
|
|
}
|
2020-06-17 18:13:37 +00:00
|
|
|
|
2020-06-17 21:15:13 +00:00
|
|
|
// RPC is a set of flags used for RPC connections (endpoint and timeout).
|
|
|
|
var RPC = []cli.Flag{
|
|
|
|
cli.StringFlag{
|
|
|
|
Name: RPCEndpointFlag + ", r",
|
|
|
|
Usage: "RPC node address",
|
|
|
|
},
|
|
|
|
cli.DurationFlag{
|
2020-06-18 06:43:37 +00:00
|
|
|
Name: "timeout, s",
|
2022-03-25 08:50:55 +00:00
|
|
|
Value: DefaultTimeout,
|
|
|
|
Usage: "Timeout for the operation",
|
2020-06-17 21:15:13 +00:00
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
var errNoEndpoint = errors.New("no RPC endpoint specified, use option '--" + RPCEndpointFlag + "' or '-r'")
|
|
|
|
|
2020-06-17 18:13:37 +00:00
|
|
|
// GetNetwork examines Context's flags and returns the appropriate network. It
|
|
|
|
// defaults to PrivNet if no flags are given.
|
|
|
|
func GetNetwork(ctx *cli.Context) netmode.Magic {
|
|
|
|
var net = netmode.PrivNet
|
|
|
|
if ctx.Bool("testnet") {
|
|
|
|
net = netmode.TestNet
|
|
|
|
}
|
|
|
|
if ctx.Bool("mainnet") {
|
|
|
|
net = netmode.MainNet
|
|
|
|
}
|
2020-08-31 09:42:42 +00:00
|
|
|
if ctx.Bool("unittest") {
|
|
|
|
net = netmode.UnitTestNet
|
|
|
|
}
|
2020-06-17 18:13:37 +00:00
|
|
|
return net
|
|
|
|
}
|
2020-06-17 21:15:13 +00:00
|
|
|
|
2022-04-20 18:30:09 +00:00
|
|
|
// GetTimeoutContext returns a context.Context with the default or a user-set timeout.
|
2020-06-17 21:15:13 +00:00
|
|
|
func GetTimeoutContext(ctx *cli.Context) (context.Context, func()) {
|
|
|
|
dur := ctx.Duration("timeout")
|
|
|
|
if dur == 0 {
|
|
|
|
dur = DefaultTimeout
|
|
|
|
}
|
|
|
|
return context.WithTimeout(context.Background(), dur)
|
|
|
|
}
|
|
|
|
|
|
|
|
// GetRPCClient returns an RPC client instance for the given Context.
|
2022-07-21 19:39:53 +00:00
|
|
|
func GetRPCClient(gctx context.Context, ctx *cli.Context) (*rpcclient.Client, cli.ExitCoder) {
|
2020-06-17 21:15:13 +00:00
|
|
|
endpoint := ctx.String(RPCEndpointFlag)
|
|
|
|
if len(endpoint) == 0 {
|
|
|
|
return nil, cli.NewExitError(errNoEndpoint, 1)
|
|
|
|
}
|
2022-07-21 19:39:53 +00:00
|
|
|
c, err := rpcclient.New(gctx, endpoint, rpcclient.Options{})
|
2020-10-14 15:13:20 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, cli.NewExitError(err, 1)
|
|
|
|
}
|
|
|
|
err = c.Init()
|
2020-06-17 21:15:13 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, cli.NewExitError(err, 1)
|
|
|
|
}
|
|
|
|
return c, nil
|
|
|
|
}
|