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.
This commit is contained in:
Dave Collins 2014-03-19 23:41:01 -05:00
parent a94ca01cf4
commit 9f1382f03c

View file

@ -15,6 +15,7 @@ var (
btcctlHomeDir = btcutil.AppDataDir("btcctl", false) btcctlHomeDir = btcutil.AppDataDir("btcctl", false)
btcwalletHomeDir = btcutil.AppDataDir("btcwallet", false) btcwalletHomeDir = btcutil.AppDataDir("btcwallet", false)
defaultConfigFile = filepath.Join(btcctlHomeDir, "btcctl.conf") defaultConfigFile = filepath.Join(btcctlHomeDir, "btcctl.conf")
defaultRPCServer = "localhost"
defaultRPCCertFile = filepath.Join(btcdHomeDir, "rpc.cert") defaultRPCCertFile = filepath.Join(btcdHomeDir, "rpc.cert")
defaultWalletCertFile = filepath.Join(btcwalletHomeDir, "rpc.cert") defaultWalletCertFile = filepath.Join(btcwalletHomeDir, "rpc.cert")
) )
@ -90,6 +91,8 @@ func loadConfig() (*flags.Parser, *config, []string, error) {
// Default config. // Default config.
cfg := config{ cfg := config{
ConfigFile: defaultConfigFile, ConfigFile: defaultConfigFile,
RPCServer: defaultRPCServer,
RPCCert: defaultRPCCertFile,
} }
// Create the home directory if it doesn't already exist. // 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 return parser, nil, nil, err
} }
// Choose a default RPC certificate file if the user did not // Override the RPC certificate if the --wallet flag was specified and
// specify one. // the user did not specify one.
if cfg.RPCCert == "" { if cfg.Wallet && cfg.RPCCert == defaultRPCCertFile {
if cfg.Wallet { cfg.RPCCert = defaultWalletCertFile
cfg.RPCCert = defaultWalletCertFile
} else {
cfg.RPCCert = defaultRPCCertFile
}
} }
// Handle environment variable expansion in the RPC certificate path. // Handle environment variable expansion in the RPC certificate path.
cfg.RPCCert = cleanAndExpandPath(cfg.RPCCert) cfg.RPCCert = cleanAndExpandPath(cfg.RPCCert)
// Connect to localhost if the user did not specify a server. // Add default port to RPC server based on --testnet and --wallet flags
if cfg.RPCServer == "" { // if needed.
cfg.RPCServer = "localhost"
}
cfg.RPCServer = normalizeAddress(cfg.RPCServer, cfg.TestNet3, cfg.Wallet) cfg.RPCServer = normalizeAddress(cfg.RPCServer, cfg.TestNet3, cfg.Wallet)
return parser, &cfg, remainingArgs, nil return parser, &cfg, remainingArgs, nil