diff --git a/cli/options/options.go b/cli/options/options.go new file mode 100644 index 000000000..34cd31777 --- /dev/null +++ b/cli/options/options.go @@ -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 +} diff --git a/cli/server/server.go b/cli/server/server.go index e73c0c239..061dc0734 100644 --- a/cli/server/server.go +++ b/cli/server/server.go @@ -6,8 +6,8 @@ import ( "os" "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/netmode" "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/storage" @@ -25,11 +25,9 @@ import ( func NewCommands() []cli.Command { var cfgFlags = []cli.Flag{ 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"}, } + cfgFlags = append(cfgFlags, options.Network...) var cfgWithCountFlags = make([]cli.Flag, len(cfgFlags)) copy(cfgWithCountFlags, cfgFlags) cfgWithCountFlags = append(cfgWithCountFlags, @@ -108,18 +106,11 @@ func newGraceContext() context.Context { // getConfigFromContext looks at path and mode flags in the given config and // returns appropriate config. 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" if argCp := ctx.String("config-path"); argCp != "" { configPath = argCp } - return config.Load(configPath, net) + return config.Load(configPath, options.GetNetwork(ctx)) } // handleLoggingParams reads logging parameters.