From 4be9e4347aae23923c546c2b027089dc80db7ad1 Mon Sep 17 00:00:00 2001 From: Anna Shaleva Date: Fri, 12 May 2023 16:53:57 +0300 Subject: [PATCH] cli: allow to provide network-specific node config file Close #2978. Signed-off-by: Anna Shaleva --- cli/options/options.go | 21 ++++++++++++++++----- cli/server/server.go | 10 +++++----- cli/server/server_test.go | 26 +++++++++++++++++++------- cli/vm/vm.go | 2 +- docs/cli.md | 4 ++++ 5 files changed, 45 insertions(+), 18 deletions(-) diff --git a/cli/options/options.go b/cli/options/options.go index e5e5f45b3..09bb08572 100644 --- a/cli/options/options.go +++ b/cli/options/options.go @@ -35,9 +35,9 @@ const RPCEndpointFlag = "rpc-endpoint" // 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", Usage: "use private network configuration"}, - cli.BoolFlag{Name: "mainnet, m", Usage: "use mainnet network configuration"}, - cli.BoolFlag{Name: "testnet, t", Usage: "use testnet network configuration"}, + cli.BoolFlag{Name: "privnet, p", Usage: "use private network configuration (if --config-file option is not specified)"}, + cli.BoolFlag{Name: "mainnet, m", Usage: "use mainnet network configuration (if --config-file option is not specified)"}, + cli.BoolFlag{Name: "testnet, t", Usage: "use testnet network configuration (if --config-file option is not specified)"}, cli.BoolFlag{Name: "unittest", Hidden: true}, } @@ -63,7 +63,14 @@ var Historic = cli.StringFlag{ // Config is a flag for commands that use node configuration. var Config = cli.StringFlag{ Name: "config-path", - Usage: "path to directory with configuration files", + Usage: "path to directory with per-network configuration files (may be overridden by --config-file option for the configuration file)", +} + +// ConfigFile is a flag for commands that use node configuration and provide +// path to the specific config file instead of config path. +var ConfigFile = cli.StringFlag{ + Name: "config-file", + Usage: "path to the node configuration file (overrides --config-path option)", } // Debug is a flag for commands that allow node in debug mode usage. @@ -152,7 +159,11 @@ func GetRPCWithInvoker(gctx context.Context, ctx *cli.Context, signers []transac // GetConfigFromContext looks at the path and the mode flags in the given config and // returns an appropriate config. func GetConfigFromContext(ctx *cli.Context) (config.Config, error) { - configPath := "./config" + var configFile = ctx.String("config-file") + if len(configFile) != 0 { + return config.LoadFile(configFile) + } + var configPath = "./config" if argCp := ctx.String("config-path"); argCp != "" { configPath = argCp } diff --git a/cli/server/server.go b/cli/server/server.go index 1caa4f099..8dd586e9a 100644 --- a/cli/server/server.go +++ b/cli/server/server.go @@ -34,7 +34,7 @@ import ( // NewCommands returns 'node' command. func NewCommands() []cli.Command { - cfgFlags := []cli.Flag{options.Config} + cfgFlags := []cli.Flag{options.Config, options.ConfigFile} cfgFlags = append(cfgFlags, options.Network...) var cfgWithCountFlags = make([]cli.Flag, len(cfgFlags)) copy(cfgWithCountFlags, cfgFlags) @@ -85,7 +85,7 @@ func NewCommands() []cli.Command { { Name: "node", Usage: "start a NeoGo node", - UsageText: "neo-go node [--config-path path] [-d] [-p/-m/-t]", + UsageText: "neo-go node [--config-path path] [-d] [-p/-m/-t] [--config-file file]", Action: startServer, Flags: cfgFlags, }, @@ -96,21 +96,21 @@ func NewCommands() []cli.Command { { Name: "dump", Usage: "dump blocks (starting with block #1) to the file", - UsageText: "neo-go db dump -o file [-s start] [-c count] [--config-path path] [-p/-m/-t]", + UsageText: "neo-go db dump -o file [-s start] [-c count] [--config-path path] [-p/-m/-t] [--config-file file]", Action: dumpDB, Flags: cfgCountOutFlags, }, { Name: "restore", Usage: "restore blocks from the file", - UsageText: "neo-go db restore -i file [--dump] [-n] [-c count] [--config-path path] [-p/-m/-t]", + UsageText: "neo-go db restore -i file [--dump] [-n] [-c count] [--config-path path] [-p/-m/-t] [--config-file file]", Action: restoreDB, Flags: cfgCountInFlags, }, { Name: "reset", Usage: "reset database to the previous state", - UsageText: "neo-go db reset --height height [--config-path path] [-p/-m/-t]", + UsageText: "neo-go db reset --height height [--config-path path] [-p/-m/-t] [--config-file file]", Action: resetDB, Flags: cfgHeightFlags, }, diff --git a/cli/server/server_test.go b/cli/server/server_test.go index 5a7a36948..62ff9b11b 100644 --- a/cli/server/server_test.go +++ b/cli/server/server_test.go @@ -30,13 +30,25 @@ func init() { } func TestGetConfigFromContext(t *testing.T) { - set := flag.NewFlagSet("flagSet", flag.ExitOnError) - set.String("config-path", "../../config", "") - set.Bool("testnet", true, "") - ctx := cli.NewContext(cli.NewApp(), set, nil) - cfg, err := options.GetConfigFromContext(ctx) - require.NoError(t, err) - require.Equal(t, netmode.TestNet, cfg.ProtocolConfiguration.Magic) + t.Run("config-path", func(t *testing.T) { + set := flag.NewFlagSet("flagSet", flag.ExitOnError) + set.String("config-path", "../../config", "") + set.Bool("testnet", true, "") + ctx := cli.NewContext(cli.NewApp(), set, nil) + cfg, err := options.GetConfigFromContext(ctx) + require.NoError(t, err) + require.Equal(t, netmode.TestNet, cfg.ProtocolConfiguration.Magic) + }) + t.Run("config-file", func(t *testing.T) { + set := flag.NewFlagSet("flagSet", flag.ExitOnError) + set.String("config-path", "../../config", "") + set.Bool("testnet", true, "") + set.String("config-file", "../../config/protocol.testnet.yml", "") + ctx := cli.NewContext(cli.NewApp(), set, nil) + cfg, err := options.GetConfigFromContext(ctx) + require.NoError(t, err) + require.Equal(t, netmode.TestNet, cfg.ProtocolConfiguration.Magic) + }) } func TestHandleLoggingParams(t *testing.T) { diff --git a/cli/vm/vm.go b/cli/vm/vm.go index 23b9938de..89dd351bb 100644 --- a/cli/vm/vm.go +++ b/cli/vm/vm.go @@ -13,7 +13,7 @@ import ( // NewCommands returns 'vm' command. func NewCommands() []cli.Command { - cfgFlags := []cli.Flag{options.Config} + cfgFlags := []cli.Flag{options.Config, options.ConfigFile} cfgFlags = append(cfgFlags, options.Network...) return []cli.Command{{ Name: "vm", diff --git a/docs/cli.md b/docs/cli.md index 310ad9562..1f734ad07 100644 --- a/docs/cli.md +++ b/docs/cli.md @@ -35,6 +35,10 @@ If you want to use some non-default configuration directory path, specify The file loaded is chosen automatically depending on network mode flag. +Or just provide path to the configuration file using `--config-file` flag: + +`./bin/neo-go node --config-file /user/yourConfigPath/yourConfigFile.yml` + Refer to the [node configuration documentation](./node-configuration.md) for detailed configuration file description.