diff --git a/cmd/btcctl/config.go b/cmd/btcctl/config.go index f6ca8846..e9c4d342 100644 --- a/cmd/btcctl/config.go +++ b/cmd/btcctl/config.go @@ -110,6 +110,7 @@ type config struct { SigNet bool `long:"signet" description:"Connect to signet"` ShowVersion bool `short:"V" long:"version" description:"Display version information and exit"` Wallet bool `long:"wallet" description:"Connect to wallet"` + GlobalHomeRoot string `long:"globalhomeroot" description:"Global btcctl root directory"` } // normalizeAddress returns addr with the passed default port appended if @@ -211,6 +212,29 @@ func loadConfig() (*config, []string, error) { } } + // Check if global home root path is set and reassigns the config, datadir, + // rpc & rpccert paths to the global root + if len(preCfg.GlobalHomeRoot) > 0 { + btcdHomeDir = filepath.Join(preCfg.GlobalHomeRoot, "Btcd") + btcctlHomeDir = filepath.Join(preCfg.GlobalHomeRoot, "Btcctl") + btcwalletHomeDir = filepath.Join(preCfg.GlobalHomeRoot, "Btcwallet") + + if _, err := os.Stat(btcctlHomeDir); os.IsNotExist(err) { + perm := os.FileMode(0700) + err = os.Mkdir(btcctlHomeDir, perm) + if err != nil { + fmt.Fprintf(os.Stderr, "Error creating Btcctl directory: %v\n", + err) + } + + // os.Create(filepath.Join(btcctlHomeDir, "btcctl.conf")) + } + + preCfg.ConfigFile = filepath.Join(btcctlHomeDir, "btcctl.conf") + cfg.RPCCert = filepath.Join(btcdHomeDir, "rpc.cert") + + } + // Show the version and exit if the version flag was specified. appName := filepath.Base(os.Args[0]) appName = strings.TrimSuffix(appName, filepath.Ext(appName)) @@ -245,6 +269,7 @@ func loadConfig() (*config, []string, error) { // Load additional config from file. parser := flags.NewParser(&cfg, flags.Default) err = flags.NewIniParser(parser).ParseFile(preCfg.ConfigFile) + if err != nil { if _, ok := err.(*os.PathError); !ok { fmt.Fprintf(os.Stderr, "Error parsing config file: %v\n", diff --git a/config.go b/config.go index 8e88fd59..c522b37a 100644 --- a/config.go +++ b/config.go @@ -125,6 +125,7 @@ type config struct { ExternalIPs []string `long:"externalip" description:"Add an ip to the list of local addresses we claim to listen on to peers"` Generate bool `long:"generate" description:"Generate (mine) bitcoins using the CPU"` FreeTxRelayLimit float64 `long:"limitfreerelay" description:"Limit relay of transactions with no transaction fee to the given amount in thousands of bytes per minute"` + GlobalHomeRoot string `short:"g" long:"globalhomeroot" description:"Global root home directory"` Listeners []string `long:"listen" description:"Add an interface/port to listen for connections (default all interfaces port: 8333, testnet: 18333)"` LogDir string `long:"logdir" description:"Directory to log output."` MaxOrphanTxs int `long:"maxorphantx" description:"Max number of orphan transactions to keep in memory"` @@ -480,6 +481,26 @@ func loadConfig() (*config, []string, error) { os.Exit(0) } + // Check if global home root path is set and reassigns the config, datadir, + // rpc & rpccert paths to the global root + if len(preCfg.GlobalHomeRoot) > 0 && preCfg.GlobalHomeRoot != defaultHomeDir{ + defaultHomeDir = filepath.Join(preCfg.GlobalHomeRoot, "Btcd") + + if _, err := os.Stat(defaultHomeDir); os.IsNotExist(err) { + perm := os.FileMode(0700) + err = os.Mkdir(defaultHomeDir, perm) + if err != nil { + fmt.Fprintf(os.Stderr, "Error creating Btcd directory: %v\n", + err) + } + } + + preCfg.ConfigFile = filepath.Join(defaultHomeDir, defaultConfigFilename) + cfg.DataDir = filepath.Join(defaultHomeDir, defaultDataDirname) + cfg.RPCKey = filepath.Join(defaultHomeDir, "rpc.key") + cfg.RPCCert = filepath.Join(defaultHomeDir, "rpc.cert") + } + // Load additional config from file. var configFileError error parser := newConfigParser(&cfg, &serviceOpts, flags.Default) @@ -1186,6 +1207,7 @@ func createDefaultConfigFile(destinationPath string) error { if err != nil { return err } + sampleConfigPath := filepath.Join(path, sampleConfigFilename) // We generate a random user and password