Skip to content

Commit

Permalink
Read RPC username/pass from correct config file for btcctl defaults.
Browse files Browse the repository at this point in the history
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.

btcsuite#875.
  • Loading branch information
Jim Posen authored and davecgh committed Aug 14, 2017
1 parent 01f26a1 commit e736ae1
Showing 1 changed file with 16 additions and 9 deletions.
25 changes: 16 additions & 9 deletions cmd/btcctl/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -211,7 +211,15 @@ func loadConfig() (*config, []string, error) {
}

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 {
fmt.Fprintf(os.Stderr, "Error creating a default config file: %v\n", err)
}
Expand Down Expand Up @@ -272,17 +280,16 @@ func loadConfig() (*config, []string, error) {
}

// 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
// the RPC user and password from it.
func createDefaultConfigFile(destinationPath string) error {
// Read btcd.conf from its default path
btcdConfigPath := filepath.Join(btcdHomeDir, "btcd.conf")
btcdConfigFile, err := os.Open(btcdConfigPath)
// For this it tries to read the config file for the RPC server (either btcd or
// btcwallet), and extract the RPC user and password from it.
func createDefaultConfigFile(destinationPath, serverConfigPath string) error {
// Read the RPC server config
serverConfigFile, err := os.Open(serverConfigPath)
if err != nil {
return err
}
defer btcdConfigFile.Close()
content, err := ioutil.ReadAll(btcdConfigFile)
defer serverConfigFile.Close()
content, err := ioutil.ReadAll(serverConfigFile)
if err != nil {
return err
}
Expand Down

0 comments on commit e736ae1

Please sign in to comment.