cli: move network options to their own package

They will be reused.
This commit is contained in:
Roman Khimov 2020-06-17 21:13:37 +03:00
parent 1c2318eed4
commit f2c4b9b1d9
2 changed files with 33 additions and 12 deletions

30
cli/options/options.go Normal file
View file

@ -0,0 +1,30 @@
/*
Package options contains a set of common CLI options and helper functions to use them.
*/
package options
import (
"github.com/nspcc-dev/neo-go/pkg/config/netmode"
"github.com/urfave/cli"
)
// Network is a set of flags for choosing the network to operate on
// (privnet/mainnet/testnet).
var Network = []cli.Flag{
cli.BoolFlag{Name: "privnet, p"},
cli.BoolFlag{Name: "mainnet, m"},
cli.BoolFlag{Name: "testnet, t"},
}
// 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
}
return net
}

View file

@ -6,8 +6,8 @@ import (
"os" "os"
"os/signal" "os/signal"
"github.com/nspcc-dev/neo-go/cli/options"
"github.com/nspcc-dev/neo-go/pkg/config" "github.com/nspcc-dev/neo-go/pkg/config"
"github.com/nspcc-dev/neo-go/pkg/config/netmode"
"github.com/nspcc-dev/neo-go/pkg/core" "github.com/nspcc-dev/neo-go/pkg/core"
"github.com/nspcc-dev/neo-go/pkg/core/block" "github.com/nspcc-dev/neo-go/pkg/core/block"
"github.com/nspcc-dev/neo-go/pkg/core/storage" "github.com/nspcc-dev/neo-go/pkg/core/storage"
@ -25,11 +25,9 @@ import (
func NewCommands() []cli.Command { func NewCommands() []cli.Command {
var cfgFlags = []cli.Flag{ var cfgFlags = []cli.Flag{
cli.StringFlag{Name: "config-path"}, cli.StringFlag{Name: "config-path"},
cli.BoolFlag{Name: "privnet, p"},
cli.BoolFlag{Name: "mainnet, m"},
cli.BoolFlag{Name: "testnet, t"},
cli.BoolFlag{Name: "debug, d"}, cli.BoolFlag{Name: "debug, d"},
} }
cfgFlags = append(cfgFlags, options.Network...)
var cfgWithCountFlags = make([]cli.Flag, len(cfgFlags)) var cfgWithCountFlags = make([]cli.Flag, len(cfgFlags))
copy(cfgWithCountFlags, cfgFlags) copy(cfgWithCountFlags, cfgFlags)
cfgWithCountFlags = append(cfgWithCountFlags, cfgWithCountFlags = append(cfgWithCountFlags,
@ -108,18 +106,11 @@ func newGraceContext() context.Context {
// getConfigFromContext looks at path and mode flags in the given config and // getConfigFromContext looks at path and mode flags in the given config and
// returns appropriate config. // returns appropriate config.
func getConfigFromContext(ctx *cli.Context) (config.Config, error) { func getConfigFromContext(ctx *cli.Context) (config.Config, error) {
var net = netmode.PrivNet
if ctx.Bool("testnet") {
net = netmode.TestNet
}
if ctx.Bool("mainnet") {
net = netmode.MainNet
}
configPath := "./config" configPath := "./config"
if argCp := ctx.String("config-path"); argCp != "" { if argCp := ctx.String("config-path"); argCp != "" {
configPath = argCp configPath = argCp
} }
return config.Load(configPath, net) return config.Load(configPath, options.GetNetwork(ctx))
} }
// handleLoggingParams reads logging parameters. // handleLoggingParams reads logging parameters.