From 9f1382f03c1df0732384dbbc006a112b07787f65 Mon Sep 17 00:00:00 2001 From: Dave Collins Date: Wed, 19 Mar 2014 23:41:01 -0500 Subject: [PATCH] Use defaults in config struct for btcctl. Rather than doing a lot of if/else in the configuration load for btcctl, set the default for the RPC server to localhost and the RPC cert to the btcd cert, then override them as needed depending on the --wallet and --testnet flags. --- util/btcctl/config.go | 22 +++++++++------------- 1 file changed, 9 insertions(+), 13 deletions(-) diff --git a/util/btcctl/config.go b/util/btcctl/config.go index 88e06d53..14689e20 100644 --- a/util/btcctl/config.go +++ b/util/btcctl/config.go @@ -15,6 +15,7 @@ var ( btcctlHomeDir = btcutil.AppDataDir("btcctl", false) btcwalletHomeDir = btcutil.AppDataDir("btcwallet", false) defaultConfigFile = filepath.Join(btcctlHomeDir, "btcctl.conf") + defaultRPCServer = "localhost" defaultRPCCertFile = filepath.Join(btcdHomeDir, "rpc.cert") defaultWalletCertFile = filepath.Join(btcwalletHomeDir, "rpc.cert") ) @@ -90,6 +91,8 @@ func loadConfig() (*flags.Parser, *config, []string, error) { // Default config. cfg := config{ ConfigFile: defaultConfigFile, + RPCServer: defaultRPCServer, + RPCCert: defaultRPCCertFile, } // Create the home directory if it doesn't already exist. @@ -130,24 +133,17 @@ func loadConfig() (*flags.Parser, *config, []string, error) { return parser, nil, nil, err } - // Choose a default RPC certificate file if the user did not - // specify one. - if cfg.RPCCert == "" { - if cfg.Wallet { - cfg.RPCCert = defaultWalletCertFile - } else { - cfg.RPCCert = defaultRPCCertFile - } + // Override the RPC certificate if the --wallet flag was specified and + // the user did not specify one. + if cfg.Wallet && cfg.RPCCert == defaultRPCCertFile { + cfg.RPCCert = defaultWalletCertFile } // Handle environment variable expansion in the RPC certificate path. cfg.RPCCert = cleanAndExpandPath(cfg.RPCCert) - // Connect to localhost if the user did not specify a server. - if cfg.RPCServer == "" { - cfg.RPCServer = "localhost" - } - + // Add default port to RPC server based on --testnet and --wallet flags + // if needed. cfg.RPCServer = normalizeAddress(cfg.RPCServer, cfg.TestNet3, cfg.Wallet) return parser, &cfg, remainingArgs, nil