Read RPC username/pass from correct config file for btcctl defaults.

If no existing btcctl.conf file exists, btcctl creates a default one
using the RPC username and password from the btcd.conf. If the
--wallet flag is passed, however, it should read from btcwallet.conf
instead.

https://github.com/btcsuite/btcd/issues/875.
This commit is contained in:
Jim Posen 2017-08-01 18:16:42 +01:00 committed by Dave Collins
parent 01f26a142b
commit e736ae125d

View File

@ -211,7 +211,15 @@ func loadConfig() (*config, []string, error) {
} }
if _, err := os.Stat(preCfg.ConfigFile); os.IsNotExist(err) { if _, err := os.Stat(preCfg.ConfigFile); os.IsNotExist(err) {
err := createDefaultConfigFile(preCfg.ConfigFile) // Use config file for RPC server to create default btcctl config
var serverConfigPath string
if preCfg.Wallet {
serverConfigPath = filepath.Join(btcwalletHomeDir, "btcwallet.conf")
} else {
serverConfigPath = filepath.Join(btcdHomeDir, "btcd.conf")
}
err := createDefaultConfigFile(preCfg.ConfigFile, serverConfigPath)
if err != nil { if err != nil {
fmt.Fprintf(os.Stderr, "Error creating a default config file: %v\n", err) fmt.Fprintf(os.Stderr, "Error creating a default config file: %v\n", err)
} }
@ -272,17 +280,16 @@ func loadConfig() (*config, []string, error) {
} }
// createDefaultConfig creates a basic config file at the given destination path. // createDefaultConfig creates a basic config file at the given destination path.
// For this it tries to read the btcd config file at its default path, and extract // For this it tries to read the config file for the RPC server (either btcd or
// the RPC user and password from it. // btcwallet), and extract the RPC user and password from it.
func createDefaultConfigFile(destinationPath string) error { func createDefaultConfigFile(destinationPath, serverConfigPath string) error {
// Read btcd.conf from its default path // Read the RPC server config
btcdConfigPath := filepath.Join(btcdHomeDir, "btcd.conf") serverConfigFile, err := os.Open(serverConfigPath)
btcdConfigFile, err := os.Open(btcdConfigPath)
if err != nil { if err != nil {
return err return err
} }
defer btcdConfigFile.Close() defer serverConfigFile.Close()
content, err := ioutil.ReadAll(btcdConfigFile) content, err := ioutil.ReadAll(serverConfigFile)
if err != nil { if err != nil {
return err return err
} }