2013-11-19 21:38:58 +01:00
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"github.com/conformal/btcutil"
|
|
|
|
"github.com/conformal/go-flags"
|
2014-03-20 02:12:14 +01:00
|
|
|
"net"
|
2013-11-19 21:38:58 +01:00
|
|
|
"os"
|
|
|
|
"path/filepath"
|
2013-11-20 02:17:34 +01:00
|
|
|
"strings"
|
2013-11-19 21:38:58 +01:00
|
|
|
)
|
|
|
|
|
|
|
|
var (
|
2014-03-20 02:12:14 +01:00
|
|
|
btcdHomeDir = btcutil.AppDataDir("btcd", false)
|
|
|
|
btcctlHomeDir = btcutil.AppDataDir("btcctl", false)
|
|
|
|
btcwalletHomeDir = btcutil.AppDataDir("btcwallet", false)
|
|
|
|
defaultConfigFile = filepath.Join(btcctlHomeDir, "btcctl.conf")
|
2014-03-20 05:41:01 +01:00
|
|
|
defaultRPCServer = "localhost"
|
2014-03-20 02:12:14 +01:00
|
|
|
defaultRPCCertFile = filepath.Join(btcdHomeDir, "rpc.cert")
|
|
|
|
defaultWalletCertFile = filepath.Join(btcwalletHomeDir, "rpc.cert")
|
2013-11-19 21:38:58 +01:00
|
|
|
)
|
|
|
|
|
|
|
|
// config defines the configuration options for btcctl.
|
|
|
|
//
|
|
|
|
// See loadConfig for details on the configuration load process.
|
|
|
|
type config struct {
|
2013-11-20 02:17:34 +01:00
|
|
|
ShowVersion bool `short:"V" long:"version" description:"Display version information and exit"`
|
2013-11-19 21:38:58 +01:00
|
|
|
ConfigFile string `short:"C" long:"configfile" description:"Path to configuration file"`
|
2013-11-20 03:35:49 +01:00
|
|
|
RPCUser string `short:"u" long:"rpcuser" description:"RPC username"`
|
|
|
|
RPCPassword string `short:"P" long:"rpcpass" default-mask:"-" description:"RPC password"`
|
|
|
|
RPCServer string `short:"s" long:"rpcserver" description:"RPC server to connect to"`
|
|
|
|
RPCCert string `short:"c" long:"rpccert" description:"RPC server certificate chain for validation"`
|
2013-11-27 02:13:31 +01:00
|
|
|
NoTls bool `long:"notls" description:"Disable TLS"`
|
2014-03-20 02:12:14 +01:00
|
|
|
TestNet3 bool `long:"testnet" description:"Connect to testnet"`
|
2013-11-19 21:38:58 +01:00
|
|
|
TlsSkipVerify bool `long:"skipverify" description:"Do not verify tls certificates (not recommended!)"`
|
2014-03-20 02:12:14 +01:00
|
|
|
Wallet bool `long:"wallet" description:"Connect to wallet"`
|
|
|
|
}
|
|
|
|
|
|
|
|
// normalizeAddress returns addr with the passed default port appended if
|
|
|
|
// there is not already a port specified.
|
|
|
|
func normalizeAddress(addr string, useTestNet3, useWallet bool) string {
|
|
|
|
_, _, err := net.SplitHostPort(addr)
|
|
|
|
if err != nil {
|
|
|
|
var defaultPort string
|
|
|
|
if useTestNet3 {
|
|
|
|
if useWallet {
|
|
|
|
defaultPort = "18332"
|
|
|
|
} else {
|
|
|
|
defaultPort = "18334"
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
if useWallet {
|
|
|
|
defaultPort = "8332"
|
|
|
|
} else {
|
|
|
|
defaultPort = "8334"
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return net.JoinHostPort(addr, defaultPort)
|
|
|
|
}
|
|
|
|
return addr
|
2013-11-19 21:38:58 +01:00
|
|
|
}
|
|
|
|
|
2014-03-19 21:22:57 +01:00
|
|
|
// 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))
|
|
|
|
}
|
|
|
|
|
2013-11-19 21:38:58 +01:00
|
|
|
// loadConfig initializes and parses the config using a config file and command
|
|
|
|
// line options.
|
|
|
|
//
|
|
|
|
// The configuration proceeds as follows:
|
|
|
|
// 1) Start with a default config with sane settings
|
|
|
|
// 2) Pre-parse the command line to check for an alternative config file
|
|
|
|
// 3) Load configuration file overwriting defaults with any specified options
|
|
|
|
// 4) Parse CLI options and overwrite/add any specified options
|
|
|
|
//
|
|
|
|
// The above results in functioning properly without any config settings
|
|
|
|
// while still allowing the user to override settings with config files and
|
|
|
|
// command line options. Command line options always take precedence.
|
|
|
|
func loadConfig() (*flags.Parser, *config, []string, error) {
|
|
|
|
// Default config.
|
|
|
|
cfg := config{
|
|
|
|
ConfigFile: defaultConfigFile,
|
2014-03-20 05:41:01 +01:00
|
|
|
RPCServer: defaultRPCServer,
|
|
|
|
RPCCert: defaultRPCCertFile,
|
2013-11-19 21:38:58 +01:00
|
|
|
}
|
|
|
|
|
2013-11-20 04:21:50 +01:00
|
|
|
// Create the home directory if it doesn't already exist.
|
|
|
|
err := os.MkdirAll(btcdHomeDir, 0700)
|
|
|
|
if err != nil {
|
|
|
|
fmt.Fprintf(os.Stderr, "%v\n", err)
|
|
|
|
os.Exit(-1)
|
|
|
|
}
|
|
|
|
|
2013-11-19 21:38:58 +01:00
|
|
|
// Pre-parse the command line options to see if an alternative config
|
2013-11-20 02:17:34 +01:00
|
|
|
// file or the version flag was specified. Any errors can be ignored
|
|
|
|
// here since they will be caught be the final parse below.
|
2013-11-19 21:38:58 +01:00
|
|
|
preCfg := cfg
|
|
|
|
preParser := flags.NewParser(&preCfg, flags.None)
|
|
|
|
preParser.Parse()
|
|
|
|
|
2013-11-20 02:17:34 +01:00
|
|
|
// Show the version and exit if the version flag was specified.
|
|
|
|
if preCfg.ShowVersion {
|
|
|
|
appName := filepath.Base(os.Args[0])
|
|
|
|
appName = strings.TrimSuffix(appName, filepath.Ext(appName))
|
|
|
|
fmt.Println(appName, "version", version())
|
|
|
|
os.Exit(0)
|
|
|
|
}
|
|
|
|
|
2013-11-19 21:38:58 +01:00
|
|
|
// Load additional config from file.
|
|
|
|
parser := flags.NewParser(&cfg, flags.PassDoubleDash|flags.HelpFlag)
|
2013-11-24 19:33:36 +01:00
|
|
|
err = flags.NewIniParser(parser).ParseFile(preCfg.ConfigFile)
|
2013-11-19 21:38:58 +01:00
|
|
|
if err != nil {
|
|
|
|
if _, ok := err.(*os.PathError); !ok {
|
|
|
|
fmt.Fprintln(os.Stderr, err)
|
|
|
|
return parser, nil, nil, err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Parse command line options again to ensure they take precedence.
|
|
|
|
remainingArgs, err := parser.Parse()
|
|
|
|
if err != nil {
|
|
|
|
return parser, nil, nil, err
|
|
|
|
}
|
|
|
|
|
2014-03-20 05:41:01 +01:00
|
|
|
// Override the RPC certificate if the --wallet flag was specified and
|
|
|
|
// the user did not specify one.
|
|
|
|
if cfg.Wallet && cfg.RPCCert == defaultRPCCertFile {
|
|
|
|
cfg.RPCCert = defaultWalletCertFile
|
2014-03-20 02:12:14 +01:00
|
|
|
}
|
|
|
|
|
2014-03-19 21:22:57 +01:00
|
|
|
// Handle environment variable expansion in the RPC certificate path.
|
|
|
|
cfg.RPCCert = cleanAndExpandPath(cfg.RPCCert)
|
|
|
|
|
2014-03-20 05:41:01 +01:00
|
|
|
// Add default port to RPC server based on --testnet and --wallet flags
|
|
|
|
// if needed.
|
2014-03-20 02:12:14 +01:00
|
|
|
cfg.RPCServer = normalizeAddress(cfg.RPCServer, cfg.TestNet3, cfg.Wallet)
|
|
|
|
|
2013-11-19 21:38:58 +01:00
|
|
|
return parser, &cfg, remainingArgs, nil
|
|
|
|
}
|