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.
This commit is contained in:
Dave Collins 2014-03-19 15:22:57 -05:00
parent 083f5e007c
commit ae89f7aa82

View file

@ -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
}