From ae89f7aa824c438ecde450a2db87b64f40dd81dd Mon Sep 17 00:00:00 2001 From: Dave Collins Date: Wed, 19 Mar 2014 15:22:57 -0500 Subject: [PATCH] Expand btcctl RPC cert path to from config file. This commit allows paths in the btcctl config file to use environment variables and ~ as a shortcut for the home directory. Closes #113. --- util/btcctl/config.go | 17 +++++++++++++++++ 1 file changed, 17 insertions(+) diff --git a/util/btcctl/config.go b/util/btcctl/config.go index b85a5e86d..b8c266102 100644 --- a/util/btcctl/config.go +++ b/util/btcctl/config.go @@ -30,6 +30,20 @@ type config struct { TlsSkipVerify bool `long:"skipverify" description:"Do not verify tls certificates (not recommended!)"` } +// cleanAndExpandPath expands environement variables and leading ~ in the +// passed path, cleans the result, and returns it. +func cleanAndExpandPath(path string) string { + // Expand initial ~ to OS specific home directory. + if strings.HasPrefix(path, "~") { + homeDir := filepath.Dir(btcctlHomeDir) + path = strings.Replace(path, "~", homeDir, 1) + } + + // NOTE: The os.ExpandEnv doesn't work with Windows-style %VARIABLE%, + // but they variables can still be expanded via POSIX-style $VARIABLE. + return filepath.Clean(os.ExpandEnv(path)) +} + // loadConfig initializes and parses the config using a config file and command // line options. // @@ -88,5 +102,8 @@ func loadConfig() (*flags.Parser, *config, []string, error) { return parser, nil, nil, err } + // Handle environment variable expansion in the RPC certificate path. + cfg.RPCCert = cleanAndExpandPath(cfg.RPCCert) + return parser, &cfg, remainingArgs, nil }