From 9bb2e02ee80ff50abca214fc1484f3305550c06d Mon Sep 17 00:00:00 2001 From: Bryan White Date: Fri, 7 Jul 2023 11:16:49 +0200 Subject: [PATCH] refactor: CLI config parsing --- app/client/cli/cmd.go | 18 ++++-------------- app/client/cli/flags/config.go | 19 +++++++++++++++++++ app/client/cli/helpers/setup.go | 5 +++++ app/client/cli/utils.go | 2 +- 4 files changed, 29 insertions(+), 15 deletions(-) create mode 100644 app/client/cli/flags/config.go diff --git a/app/client/cli/cmd.go b/app/client/cli/cmd.go index 8e3f975bb9..160360ac1f 100644 --- a/app/client/cli/cmd.go +++ b/app/client/cli/cmd.go @@ -8,7 +8,6 @@ import ( "github.com/spf13/viper" "github.com/pokt-network/pocket/app/client/cli/flags" - "github.com/pokt-network/pocket/runtime/configs" "github.com/pokt-network/pocket/runtime/defaults" ) @@ -17,8 +16,6 @@ const ( flagBindErrFormat = "could not bind flag %q: %v" ) -var cfg *configs.Config - func init() { rootCmd.PersistentFlags().StringVar(&flags.RemoteCLIURL, "remote_cli_url", defaults.DefaultRemoteCLIURL, "takes a remote endpoint in the form of ://: (uses RPC Port)") // ensure that this flag can be overridden by the respective viper-conventional environment variable (i.e. `POCKET_REMOTE_CLI_URL`) @@ -42,17 +39,10 @@ func init() { } var rootCmd = &cobra.Command{ - Use: cliExecutableName, - Short: "Pocket Network Command Line Interface (CLI)", - Long: "The CLI is meant to be an user but also a machine friendly way for interacting with Pocket Network.", - PersistentPreRunE: func(cmd *cobra.Command, args []string) error { - // by this time, the config path should be set - cfg = configs.ParseConfig(flags.ConfigPath) - - // set final `remote_cli_url` value; order of precedence: flag > env var > config > default - flags.RemoteCLIURL = viper.GetString("remote_cli_url") - return nil - }, + Use: cliExecutableName, + Short: "Pocket Network Command Line Interface (CLI)", + Long: "The CLI is meant to be an user but also a machine friendly way for interacting with Pocket Network.", + PersistentPreRunE: flags.ParseConfigAndFlags, } func ExecuteContext(ctx context.Context) error { diff --git a/app/client/cli/flags/config.go b/app/client/cli/flags/config.go new file mode 100644 index 0000000000..0de55a7aa1 --- /dev/null +++ b/app/client/cli/flags/config.go @@ -0,0 +1,19 @@ +package flags + +import ( + "github.com/spf13/cobra" + "github.com/spf13/viper" + + "github.com/pokt-network/pocket/runtime/configs" +) + +var Cfg *configs.Config + +func ParseConfigAndFlags(_ *cobra.Command, _ []string) error { + // by this time, the config path should be set + Cfg = configs.ParseConfig(ConfigPath) + + // set final `remote_cli_url` value; order of precedence: flag > env var > config > default + RemoteCLIURL = viper.GetString("remote_cli_url") + return nil +} diff --git a/app/client/cli/helpers/setup.go b/app/client/cli/helpers/setup.go index 956102a044..350bcbf238 100644 --- a/app/client/cli/helpers/setup.go +++ b/app/client/cli/helpers/setup.go @@ -17,6 +17,11 @@ import ( // P2PDependenciesPreRunE initializes peerstore & current height providers, and a // p2p module which consumes them. Everything is registered to the bus. func P2PDependenciesPreRunE(cmd *cobra.Command, _ []string) error { + if err := flags.ParseConfigAndFlags(nil, nil); err != nil { + return err + } + + // TODO_THIS_COMMIT: figure out what to do with this vvv // TECHDEBT: this is to keep backwards compatibility with localnet flags.ConfigPath = runtime.GetEnv("CONFIG_PATH", "build/config/config.validator1.json") diff --git a/app/client/cli/utils.go b/app/client/cli/utils.go index 2e3e0263ef..36a7229e67 100644 --- a/app/client/cli/utils.go +++ b/app/client/cli/utils.go @@ -311,7 +311,7 @@ func attachKeybaseFlagsToSubcommands() []cmdOption { } func keybaseForCLI() (keybase.Keybase, error) { - return keybase.NewKeybase(cfg.Keybase) + return keybase.NewKeybase(flags.Cfg.Keybase) } func unableToConnectToRpc(err error) error {