From 37571162a0f5e85b859bf4cd13b7187b9e262113 Mon Sep 17 00:00:00 2001 From: Anna Shaleva Date: Mon, 3 Oct 2022 15:05:34 +0300 Subject: [PATCH] cli: move config path flag to options package --- cli/options/options.go | 17 +++++++++++++++++ cli/server/server.go | 22 +++++----------------- cli/server/server_test.go | 5 +++-- 3 files changed, 25 insertions(+), 19 deletions(-) diff --git a/cli/options/options.go b/cli/options/options.go index cb23cb72c..8d6b5a4eb 100644 --- a/cli/options/options.go +++ b/cli/options/options.go @@ -9,6 +9,7 @@ import ( "strconv" "time" + "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/transaction" "github.com/nspcc-dev/neo-go/pkg/rpcclient" @@ -52,6 +53,12 @@ var Historic = cli.StringFlag{ Usage: "Use historic state (height, block hash or state root hash)", } +// Config is a flag for commands that use node configuration. +var Config = cli.StringFlag{ + Name: "config-path", + Usage: "path to directory with configuration files", +} + var errNoEndpoint = errors.New("no RPC endpoint specified, use option '--" + RPCEndpointFlag + "' or '-r'") var errInvalidHistoric = errors.New("invalid 'historic' parameter, neither a block number, nor a block/state hash") @@ -128,3 +135,13 @@ func GetRPCWithInvoker(gctx context.Context, ctx *cli.Context, signers []transac } return c, inv, err } + +// 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" + if argCp := ctx.String("config-path"); argCp != "" { + configPath = argCp + } + return config.Load(configPath, GetNetwork(ctx)) +} diff --git a/cli/server/server.go b/cli/server/server.go index 827b853b7..8ae5b8136 100644 --- a/cli/server/server.go +++ b/cli/server/server.go @@ -43,9 +43,7 @@ var ( // NewCommands returns 'node' command. func NewCommands() []cli.Command { - var cfgFlags = []cli.Flag{ - cli.StringFlag{Name: "config-path", Usage: "path to directory with configuration files"}, - } + cfgFlags := []cli.Flag{options.Config} cfgFlags = append(cfgFlags, options.Network...) var cfgWithCountFlags = make([]cli.Flag, len(cfgFlags)) copy(cfgWithCountFlags, cfgFlags) @@ -128,16 +126,6 @@ func newGraceContext() context.Context { return ctx } -// 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" - if argCp := ctx.String("config-path"); argCp != "" { - configPath = argCp - } - return config.Load(configPath, options.GetNetwork(ctx)) -} - // handleLoggingParams reads logging parameters. // If a user selected debug level -- function enables it. // If logPath is configured -- function creates a dir and a file for logging. @@ -233,7 +221,7 @@ func dumpDB(ctx *cli.Context) error { if err := cmdargs.EnsureNone(ctx); err != nil { return err } - cfg, err := getConfigFromContext(ctx) + cfg, err := options.GetConfigFromContext(ctx) if err != nil { return cli.NewExitError(err, 1) } @@ -286,7 +274,7 @@ func restoreDB(ctx *cli.Context) error { if err := cmdargs.EnsureNone(ctx); err != nil { return err } - cfg, err := getConfigFromContext(ctx) + cfg, err := options.GetConfigFromContext(ctx) if err != nil { return err } @@ -472,7 +460,7 @@ func startServer(ctx *cli.Context) error { return err } - cfg, err := getConfigFromContext(ctx) + cfg, err := options.GetConfigFromContext(ctx) if err != nil { return cli.NewExitError(err, 1) } @@ -549,7 +537,7 @@ Main: cancel() case sig := <-sigCh: log.Info("signal received", zap.Stringer("name", sig)) - cfgnew, err := getConfigFromContext(ctx) + cfgnew, err := options.GetConfigFromContext(ctx) if err != nil { log.Warn("can't reread the config file, signal ignored", zap.Error(err)) break // Continue working. diff --git a/cli/server/server_test.go b/cli/server/server_test.go index f3f8c107c..ff51b431b 100644 --- a/cli/server/server_test.go +++ b/cli/server/server_test.go @@ -7,6 +7,7 @@ import ( "path/filepath" "testing" + "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/storage/dbconfig" @@ -32,7 +33,7 @@ func TestGetConfigFromContext(t *testing.T) { set.String("config-path", "../../config", "") set.Bool("testnet", true, "") ctx := cli.NewContext(cli.NewApp(), set, nil) - cfg, err := getConfigFromContext(ctx) + cfg, err := options.GetConfigFromContext(ctx) require.NoError(t, err) require.Equal(t, netmode.TestNet, cfg.ProtocolConfiguration.Magic) } @@ -101,7 +102,7 @@ func TestInitBCWithMetrics(t *testing.T) { set.Bool("testnet", true, "") set.Bool("debug", true, "") ctx := cli.NewContext(cli.NewApp(), set, nil) - cfg, err := getConfigFromContext(ctx) + cfg, err := options.GetConfigFromContext(ctx) require.NoError(t, err) logger, closer, err := handleLoggingParams(ctx, cfg.ApplicationConfiguration) require.NoError(t, err)