lbcd/cmd/lbcctl/config.go

373 lines
12 KiB
Go
Raw Normal View History

// Copyright (c) 2013-2015 The btcsuite developers
Rewrite btcctl to use the new features of btcjson. This commit contains what is essentially a complete rewrite of the btcctl utility to make use of the new features provided by the latest version btcjson and improve several things along the way. The following summarizes the changes: - The supported commands and handling now come directly from btcjson, so it is no longer necessary to manually add new commands. Once a command has been registered with btcjson, it will automatically become usable by btcctl complete with full error handling (once it is re-compiled of course) - Rather than dumping the entire list of commands on every error, the user now must specifically request the list of command via the -l option - The list of commands is now categorized by chain and wallet and alphabetized - The help flag now only shows the help options instead of also dumping all of the commands - The error display on valid commands with invalid parameters has been greatly improved to show the specific parameter number, reason, and error code - When a valid command is specified with invalid parameter, only the usage for that specific command is shown now - It is now possible to use a SOCKS5 proxy for connection - The output of commands has been improved in the following ways: - Strings on commands such as getbestblockhash no longer have quotes wrapped around them - Fields that are integers no longer show in scientific notation when they are large (timestamps for example) This closes #305 as a side effect.
2015-01-18 21:45:20 +01:00
// Use of this source code is governed by an ISC
// license that can be found in the LICENSE file.
package main
import (
"fmt"
2016-06-04 04:14:15 +02:00
"io/ioutil"
"net"
"os"
"path/filepath"
2016-06-04 04:14:15 +02:00
"regexp"
"strings"
2014-07-02 15:50:08 +02:00
flags "github.com/jessevdk/go-flags"
"github.com/lbryio/lbcd/btcjson"
"github.com/lbryio/lbcd/chaincfg"
"github.com/lbryio/lbcd/version"
btcutil "github.com/lbryio/lbcutil"
)
Rewrite btcctl to use the new features of btcjson. This commit contains what is essentially a complete rewrite of the btcctl utility to make use of the new features provided by the latest version btcjson and improve several things along the way. The following summarizes the changes: - The supported commands and handling now come directly from btcjson, so it is no longer necessary to manually add new commands. Once a command has been registered with btcjson, it will automatically become usable by btcctl complete with full error handling (once it is re-compiled of course) - Rather than dumping the entire list of commands on every error, the user now must specifically request the list of command via the -l option - The list of commands is now categorized by chain and wallet and alphabetized - The help flag now only shows the help options instead of also dumping all of the commands - The error display on valid commands with invalid parameters has been greatly improved to show the specific parameter number, reason, and error code - When a valid command is specified with invalid parameter, only the usage for that specific command is shown now - It is now possible to use a SOCKS5 proxy for connection - The output of commands has been improved in the following ways: - Strings on commands such as getbestblockhash no longer have quotes wrapped around them - Fields that are integers no longer show in scientific notation when they are large (timestamps for example) This closes #305 as a side effect.
2015-01-18 21:45:20 +01:00
const (
// unusableFlags are the command usage flags which this utility are not
// able to use. In particular it doesn't support websockets and
// consequently notifications.
unusableFlags = btcjson.UFWebsocketOnly | btcjson.UFNotification
)
var (
btcdHomeDir = btcutil.AppDataDir("lbcd", false)
btcctlHomeDir = btcutil.AppDataDir("lbcctl", false)
btcwalletHomeDir = btcutil.AppDataDir("lbcwallet", false)
defaultConfigFile = filepath.Join(btcctlHomeDir, "lbcctl.conf")
defaultRPCServer = "localhost"
defaultRPCCertFile = filepath.Join(btcdHomeDir, "rpc.cert")
defaultWalletCertFile = filepath.Join(btcwalletHomeDir, "rpc.cert")
)
Rewrite btcctl to use the new features of btcjson. This commit contains what is essentially a complete rewrite of the btcctl utility to make use of the new features provided by the latest version btcjson and improve several things along the way. The following summarizes the changes: - The supported commands and handling now come directly from btcjson, so it is no longer necessary to manually add new commands. Once a command has been registered with btcjson, it will automatically become usable by btcctl complete with full error handling (once it is re-compiled of course) - Rather than dumping the entire list of commands on every error, the user now must specifically request the list of command via the -l option - The list of commands is now categorized by chain and wallet and alphabetized - The help flag now only shows the help options instead of also dumping all of the commands - The error display on valid commands with invalid parameters has been greatly improved to show the specific parameter number, reason, and error code - When a valid command is specified with invalid parameter, only the usage for that specific command is shown now - It is now possible to use a SOCKS5 proxy for connection - The output of commands has been improved in the following ways: - Strings on commands such as getbestblockhash no longer have quotes wrapped around them - Fields that are integers no longer show in scientific notation when they are large (timestamps for example) This closes #305 as a side effect.
2015-01-18 21:45:20 +01:00
// listCommands categorizes and lists all of the usable commands along with
// their one-line usage.
func listCommands() {
const (
categoryChain uint8 = iota
categoryWallet
numCategories
)
// Get a list of registered commands and categorize and filter them.
cmdMethods := btcjson.RegisteredCmdMethods()
categorized := make([][]string, numCategories)
for _, method := range cmdMethods {
flags, err := btcjson.MethodUsageFlags(method)
if err != nil {
// This should never happen since the method was just
// returned from the package, but be safe.
continue
}
// Skip the commands that aren't usable from this utility.
if flags&unusableFlags != 0 {
continue
}
usage, err := btcjson.MethodUsageText(method)
if err != nil {
// This should never happen since the method was just
// returned from the package, but be safe.
continue
}
// Categorize the command based on the usage flags.
category := categoryChain
if flags&btcjson.UFWalletOnly != 0 {
category = categoryWallet
}
categorized[category] = append(categorized[category], usage)
}
// Display the command according to their categories.
categoryTitles := make([]string, numCategories)
categoryTitles[categoryChain] = "Chain Server Commands:"
categoryTitles[categoryWallet] = "Wallet Server Commands (--wallet):"
for category := uint8(0); category < numCategories; category++ {
fmt.Println(categoryTitles[category])
for _, usage := range categorized[category] {
fmt.Println(usage)
}
fmt.Println()
}
}
// config defines the configuration options for btcctl.
//
// See loadConfig for details on the configuration load process.
type config struct {
2020-04-14 13:06:39 +02:00
ConfigFile string `short:"C" long:"configfile" description:"Path to configuration file"`
ListCommands bool `short:"l" long:"listcommands" description:"List all of the supported commands and exit"`
NoTLS bool `long:"notls" description:"Disable TLS"`
TLSSkipVerify bool `long:"skipverify" description:"Do not verify tls certificates (not recommended!)"`
2020-04-14 13:06:39 +02:00
Proxy string `long:"proxy" description:"Connect via SOCKS5 proxy (eg. 127.0.0.1:9050)"`
ProxyPass string `long:"proxypass" default-mask:"-" description:"Password for proxy server"`
ProxyUser string `long:"proxyuser" description:"Username for proxy server"`
RPCCert string `short:"c" long:"rpccert" description:"RPC server certificate chain for validation"`
RPCPassword string `short:"P" long:"rpcpass" default-mask:"-" description:"RPC password"`
RPCServer string `short:"s" long:"rpcserver" description:"RPC server to connect to"`
RPCUser string `short:"u" long:"rpcuser" description:"RPC username"`
TestNet3 bool `long:"testnet" description:"Connect to testnet (default RPC server: localhost:19245)"`
RegressionTest bool `long:"regtest" description:"Connect to the regression test network (default RPC server: localhost:29245)"`
SimNet bool `long:"simnet" description:"Connect to the simulation test network (default RPC server: localhost:39245)"`
SigNet bool `long:"signet" description:"Connect to signet (default RPC server: localhost:49245)"`
Wallet bool `long:"wallet" description:"Connect to wallet RPC server instead (default: localhost:9244, testnet: localhost:19244, regtest: localhost:29244)"`
2020-04-14 13:06:39 +02:00
ShowVersion bool `short:"V" long:"version" description:"Display version information and exit"`
}
// normalizeAddress returns addr with the passed default port appended if
// there is not already a port specified.
2020-04-14 13:06:39 +02:00
func normalizeAddress(addr string, chain *chaincfg.Params, useWallet bool) (string, error) {
_, _, err := net.SplitHostPort(addr)
if err != nil {
var defaultPort string
2020-04-14 13:06:39 +02:00
switch chain {
case &chaincfg.TestNet3Params:
if useWallet {
defaultPort = "19244"
} else {
defaultPort = "19245"
}
case &chaincfg.RegressionNetParams:
if useWallet {
defaultPort = "29244"
} else {
defaultPort = "29245"
}
case &chaincfg.SimNetParams:
2020-04-14 13:06:39 +02:00
if useWallet {
defaultPort = "39244"
2020-04-14 13:06:39 +02:00
} else {
defaultPort = "39245"
2020-04-14 13:06:39 +02:00
}
case &chaincfg.SigNetParams:
if useWallet {
defaultPort = "49244"
} else {
defaultPort = "49245"
}
default:
if useWallet {
defaultPort = "9244"
} else {
defaultPort = "9245"
}
}
2020-04-14 13:06:39 +02:00
return net.JoinHostPort(addr, defaultPort), nil
}
2020-04-14 13:06:39 +02:00
return addr, nil
}
// 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.
//
// 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.
Rewrite btcctl to use the new features of btcjson. This commit contains what is essentially a complete rewrite of the btcctl utility to make use of the new features provided by the latest version btcjson and improve several things along the way. The following summarizes the changes: - The supported commands and handling now come directly from btcjson, so it is no longer necessary to manually add new commands. Once a command has been registered with btcjson, it will automatically become usable by btcctl complete with full error handling (once it is re-compiled of course) - Rather than dumping the entire list of commands on every error, the user now must specifically request the list of command via the -l option - The list of commands is now categorized by chain and wallet and alphabetized - The help flag now only shows the help options instead of also dumping all of the commands - The error display on valid commands with invalid parameters has been greatly improved to show the specific parameter number, reason, and error code - When a valid command is specified with invalid parameter, only the usage for that specific command is shown now - It is now possible to use a SOCKS5 proxy for connection - The output of commands has been improved in the following ways: - Strings on commands such as getbestblockhash no longer have quotes wrapped around them - Fields that are integers no longer show in scientific notation when they are large (timestamps for example) This closes #305 as a side effect.
2015-01-18 21:45:20 +01:00
func loadConfig() (*config, []string, error) {
// Default config.
cfg := config{
ConfigFile: defaultConfigFile,
RPCServer: defaultRPCServer,
RPCCert: defaultRPCCertFile,
}
// Pre-parse the command line options to see if an alternative config
Rewrite btcctl to use the new features of btcjson. This commit contains what is essentially a complete rewrite of the btcctl utility to make use of the new features provided by the latest version btcjson and improve several things along the way. The following summarizes the changes: - The supported commands and handling now come directly from btcjson, so it is no longer necessary to manually add new commands. Once a command has been registered with btcjson, it will automatically become usable by btcctl complete with full error handling (once it is re-compiled of course) - Rather than dumping the entire list of commands on every error, the user now must specifically request the list of command via the -l option - The list of commands is now categorized by chain and wallet and alphabetized - The help flag now only shows the help options instead of also dumping all of the commands - The error display on valid commands with invalid parameters has been greatly improved to show the specific parameter number, reason, and error code - When a valid command is specified with invalid parameter, only the usage for that specific command is shown now - It is now possible to use a SOCKS5 proxy for connection - The output of commands has been improved in the following ways: - Strings on commands such as getbestblockhash no longer have quotes wrapped around them - Fields that are integers no longer show in scientific notation when they are large (timestamps for example) This closes #305 as a side effect.
2015-01-18 21:45:20 +01:00
// file, the version flag, or the list commands flag was specified. Any
// errors aside from the help message error can be ignored here since
// they will be caught by the final parse below.
preCfg := cfg
Rewrite btcctl to use the new features of btcjson. This commit contains what is essentially a complete rewrite of the btcctl utility to make use of the new features provided by the latest version btcjson and improve several things along the way. The following summarizes the changes: - The supported commands and handling now come directly from btcjson, so it is no longer necessary to manually add new commands. Once a command has been registered with btcjson, it will automatically become usable by btcctl complete with full error handling (once it is re-compiled of course) - Rather than dumping the entire list of commands on every error, the user now must specifically request the list of command via the -l option - The list of commands is now categorized by chain and wallet and alphabetized - The help flag now only shows the help options instead of also dumping all of the commands - The error display on valid commands with invalid parameters has been greatly improved to show the specific parameter number, reason, and error code - When a valid command is specified with invalid parameter, only the usage for that specific command is shown now - It is now possible to use a SOCKS5 proxy for connection - The output of commands has been improved in the following ways: - Strings on commands such as getbestblockhash no longer have quotes wrapped around them - Fields that are integers no longer show in scientific notation when they are large (timestamps for example) This closes #305 as a side effect.
2015-01-18 21:45:20 +01:00
preParser := flags.NewParser(&preCfg, flags.HelpFlag)
_, err := preParser.Parse()
Rewrite btcctl to use the new features of btcjson. This commit contains what is essentially a complete rewrite of the btcctl utility to make use of the new features provided by the latest version btcjson and improve several things along the way. The following summarizes the changes: - The supported commands and handling now come directly from btcjson, so it is no longer necessary to manually add new commands. Once a command has been registered with btcjson, it will automatically become usable by btcctl complete with full error handling (once it is re-compiled of course) - Rather than dumping the entire list of commands on every error, the user now must specifically request the list of command via the -l option - The list of commands is now categorized by chain and wallet and alphabetized - The help flag now only shows the help options instead of also dumping all of the commands - The error display on valid commands with invalid parameters has been greatly improved to show the specific parameter number, reason, and error code - When a valid command is specified with invalid parameter, only the usage for that specific command is shown now - It is now possible to use a SOCKS5 proxy for connection - The output of commands has been improved in the following ways: - Strings on commands such as getbestblockhash no longer have quotes wrapped around them - Fields that are integers no longer show in scientific notation when they are large (timestamps for example) This closes #305 as a side effect.
2015-01-18 21:45:20 +01:00
if err != nil {
if e, ok := err.(*flags.Error); ok && e.Type == flags.ErrHelp {
fmt.Fprintln(os.Stderr, err)
fmt.Fprintln(os.Stderr, "")
fmt.Fprintln(os.Stderr, "The special parameter `-` "+
"indicates that a parameter should be read "+
"from the\nnext unread line from standard "+
"input.")
Rewrite btcctl to use the new features of btcjson. This commit contains what is essentially a complete rewrite of the btcctl utility to make use of the new features provided by the latest version btcjson and improve several things along the way. The following summarizes the changes: - The supported commands and handling now come directly from btcjson, so it is no longer necessary to manually add new commands. Once a command has been registered with btcjson, it will automatically become usable by btcctl complete with full error handling (once it is re-compiled of course) - Rather than dumping the entire list of commands on every error, the user now must specifically request the list of command via the -l option - The list of commands is now categorized by chain and wallet and alphabetized - The help flag now only shows the help options instead of also dumping all of the commands - The error display on valid commands with invalid parameters has been greatly improved to show the specific parameter number, reason, and error code - When a valid command is specified with invalid parameter, only the usage for that specific command is shown now - It is now possible to use a SOCKS5 proxy for connection - The output of commands has been improved in the following ways: - Strings on commands such as getbestblockhash no longer have quotes wrapped around them - Fields that are integers no longer show in scientific notation when they are large (timestamps for example) This closes #305 as a side effect.
2015-01-18 21:45:20 +01:00
return nil, nil, err
}
}
// Show the version and exit if the version flag was specified.
Rewrite btcctl to use the new features of btcjson. This commit contains what is essentially a complete rewrite of the btcctl utility to make use of the new features provided by the latest version btcjson and improve several things along the way. The following summarizes the changes: - The supported commands and handling now come directly from btcjson, so it is no longer necessary to manually add new commands. Once a command has been registered with btcjson, it will automatically become usable by btcctl complete with full error handling (once it is re-compiled of course) - Rather than dumping the entire list of commands on every error, the user now must specifically request the list of command via the -l option - The list of commands is now categorized by chain and wallet and alphabetized - The help flag now only shows the help options instead of also dumping all of the commands - The error display on valid commands with invalid parameters has been greatly improved to show the specific parameter number, reason, and error code - When a valid command is specified with invalid parameter, only the usage for that specific command is shown now - It is now possible to use a SOCKS5 proxy for connection - The output of commands has been improved in the following ways: - Strings on commands such as getbestblockhash no longer have quotes wrapped around them - Fields that are integers no longer show in scientific notation when they are large (timestamps for example) This closes #305 as a side effect.
2015-01-18 21:45:20 +01:00
appName := filepath.Base(os.Args[0])
appName = strings.TrimSuffix(appName, filepath.Ext(appName))
usageMessage := fmt.Sprintf("Use %s -h to show options", appName)
if preCfg.ShowVersion {
fmt.Println(appName, "version", version.Full())
os.Exit(0)
}
Rewrite btcctl to use the new features of btcjson. This commit contains what is essentially a complete rewrite of the btcctl utility to make use of the new features provided by the latest version btcjson and improve several things along the way. The following summarizes the changes: - The supported commands and handling now come directly from btcjson, so it is no longer necessary to manually add new commands. Once a command has been registered with btcjson, it will automatically become usable by btcctl complete with full error handling (once it is re-compiled of course) - Rather than dumping the entire list of commands on every error, the user now must specifically request the list of command via the -l option - The list of commands is now categorized by chain and wallet and alphabetized - The help flag now only shows the help options instead of also dumping all of the commands - The error display on valid commands with invalid parameters has been greatly improved to show the specific parameter number, reason, and error code - When a valid command is specified with invalid parameter, only the usage for that specific command is shown now - It is now possible to use a SOCKS5 proxy for connection - The output of commands has been improved in the following ways: - Strings on commands such as getbestblockhash no longer have quotes wrapped around them - Fields that are integers no longer show in scientific notation when they are large (timestamps for example) This closes #305 as a side effect.
2015-01-18 21:45:20 +01:00
// Show the available commands and exit if the associated flag was
// specified.
if preCfg.ListCommands {
listCommands()
os.Exit(0)
}
2016-06-04 04:14:15 +02:00
if _, err := os.Stat(preCfg.ConfigFile); os.IsNotExist(err) {
// Use config file for RPC server to create default btcctl config
var serverConfigPath string
if preCfg.Wallet {
serverConfigPath = filepath.Join(btcwalletHomeDir, "lbcwallet.conf")
} else {
serverConfigPath = filepath.Join(btcdHomeDir, "lbcd.conf")
}
err := createDefaultConfigFile(preCfg.ConfigFile, serverConfigPath)
2016-06-04 04:14:15 +02:00
if err != nil {
fmt.Fprintf(os.Stderr, "Error creating a default config file: %v\n", err)
}
}
// Load additional config from file.
Rewrite btcctl to use the new features of btcjson. This commit contains what is essentially a complete rewrite of the btcctl utility to make use of the new features provided by the latest version btcjson and improve several things along the way. The following summarizes the changes: - The supported commands and handling now come directly from btcjson, so it is no longer necessary to manually add new commands. Once a command has been registered with btcjson, it will automatically become usable by btcctl complete with full error handling (once it is re-compiled of course) - Rather than dumping the entire list of commands on every error, the user now must specifically request the list of command via the -l option - The list of commands is now categorized by chain and wallet and alphabetized - The help flag now only shows the help options instead of also dumping all of the commands - The error display on valid commands with invalid parameters has been greatly improved to show the specific parameter number, reason, and error code - When a valid command is specified with invalid parameter, only the usage for that specific command is shown now - It is now possible to use a SOCKS5 proxy for connection - The output of commands has been improved in the following ways: - Strings on commands such as getbestblockhash no longer have quotes wrapped around them - Fields that are integers no longer show in scientific notation when they are large (timestamps for example) This closes #305 as a side effect.
2015-01-18 21:45:20 +01:00
parser := flags.NewParser(&cfg, flags.Default)
err = flags.NewIniParser(parser).ParseFile(preCfg.ConfigFile)
if err != nil {
if _, ok := err.(*os.PathError); !ok {
Rewrite btcctl to use the new features of btcjson. This commit contains what is essentially a complete rewrite of the btcctl utility to make use of the new features provided by the latest version btcjson and improve several things along the way. The following summarizes the changes: - The supported commands and handling now come directly from btcjson, so it is no longer necessary to manually add new commands. Once a command has been registered with btcjson, it will automatically become usable by btcctl complete with full error handling (once it is re-compiled of course) - Rather than dumping the entire list of commands on every error, the user now must specifically request the list of command via the -l option - The list of commands is now categorized by chain and wallet and alphabetized - The help flag now only shows the help options instead of also dumping all of the commands - The error display on valid commands with invalid parameters has been greatly improved to show the specific parameter number, reason, and error code - When a valid command is specified with invalid parameter, only the usage for that specific command is shown now - It is now possible to use a SOCKS5 proxy for connection - The output of commands has been improved in the following ways: - Strings on commands such as getbestblockhash no longer have quotes wrapped around them - Fields that are integers no longer show in scientific notation when they are large (timestamps for example) This closes #305 as a side effect.
2015-01-18 21:45:20 +01:00
fmt.Fprintf(os.Stderr, "Error parsing config file: %v\n",
err)
fmt.Fprintln(os.Stderr, usageMessage)
return nil, nil, err
}
}
// Parse command line options again to ensure they take precedence.
remainingArgs, err := parser.Parse()
if err != nil {
Rewrite btcctl to use the new features of btcjson. This commit contains what is essentially a complete rewrite of the btcctl utility to make use of the new features provided by the latest version btcjson and improve several things along the way. The following summarizes the changes: - The supported commands and handling now come directly from btcjson, so it is no longer necessary to manually add new commands. Once a command has been registered with btcjson, it will automatically become usable by btcctl complete with full error handling (once it is re-compiled of course) - Rather than dumping the entire list of commands on every error, the user now must specifically request the list of command via the -l option - The list of commands is now categorized by chain and wallet and alphabetized - The help flag now only shows the help options instead of also dumping all of the commands - The error display on valid commands with invalid parameters has been greatly improved to show the specific parameter number, reason, and error code - When a valid command is specified with invalid parameter, only the usage for that specific command is shown now - It is now possible to use a SOCKS5 proxy for connection - The output of commands has been improved in the following ways: - Strings on commands such as getbestblockhash no longer have quotes wrapped around them - Fields that are integers no longer show in scientific notation when they are large (timestamps for example) This closes #305 as a side effect.
2015-01-18 21:45:20 +01:00
if e, ok := err.(*flags.Error); !ok || e.Type != flags.ErrHelp {
fmt.Fprintln(os.Stderr, usageMessage)
}
return nil, nil, err
}
2020-04-14 13:06:39 +02:00
// default network is mainnet
network := &chaincfg.MainNetParams
// Multiple networks can't be selected simultaneously.
numNets := 0
if cfg.TestNet3 {
numNets++
2020-04-14 13:06:39 +02:00
network = &chaincfg.TestNet3Params
}
if cfg.SimNet {
numNets++
2020-04-14 13:06:39 +02:00
network = &chaincfg.SimNetParams
}
2020-04-14 13:06:39 +02:00
if cfg.RegressionTest {
numNets++
network = &chaincfg.RegressionNetParams
}
if cfg.SigNet {
numNets++
network = &chaincfg.SigNetParams
}
2020-04-14 13:06:39 +02:00
if numNets > 1 {
2020-04-14 13:06:39 +02:00
str := "%s: Multiple network params can't be used " +
"together -- choose one"
err := fmt.Errorf(str, "loadConfig")
fmt.Fprintln(os.Stderr, err)
Rewrite btcctl to use the new features of btcjson. This commit contains what is essentially a complete rewrite of the btcctl utility to make use of the new features provided by the latest version btcjson and improve several things along the way. The following summarizes the changes: - The supported commands and handling now come directly from btcjson, so it is no longer necessary to manually add new commands. Once a command has been registered with btcjson, it will automatically become usable by btcctl complete with full error handling (once it is re-compiled of course) - Rather than dumping the entire list of commands on every error, the user now must specifically request the list of command via the -l option - The list of commands is now categorized by chain and wallet and alphabetized - The help flag now only shows the help options instead of also dumping all of the commands - The error display on valid commands with invalid parameters has been greatly improved to show the specific parameter number, reason, and error code - When a valid command is specified with invalid parameter, only the usage for that specific command is shown now - It is now possible to use a SOCKS5 proxy for connection - The output of commands has been improved in the following ways: - Strings on commands such as getbestblockhash no longer have quotes wrapped around them - Fields that are integers no longer show in scientific notation when they are large (timestamps for example) This closes #305 as a side effect.
2015-01-18 21:45:20 +01:00
return nil, nil, err
}
// 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
}
// Handle environment variable expansion in the RPC certificate path.
cfg.RPCCert = cleanAndExpandPath(cfg.RPCCert)
// Add default port to RPC server based on --testnet and --wallet flags
// if needed.
2020-04-14 13:06:39 +02:00
cfg.RPCServer, err = normalizeAddress(cfg.RPCServer, network, cfg.Wallet)
if err != nil {
return nil, nil, err
}
Rewrite btcctl to use the new features of btcjson. This commit contains what is essentially a complete rewrite of the btcctl utility to make use of the new features provided by the latest version btcjson and improve several things along the way. The following summarizes the changes: - The supported commands and handling now come directly from btcjson, so it is no longer necessary to manually add new commands. Once a command has been registered with btcjson, it will automatically become usable by btcctl complete with full error handling (once it is re-compiled of course) - Rather than dumping the entire list of commands on every error, the user now must specifically request the list of command via the -l option - The list of commands is now categorized by chain and wallet and alphabetized - The help flag now only shows the help options instead of also dumping all of the commands - The error display on valid commands with invalid parameters has been greatly improved to show the specific parameter number, reason, and error code - When a valid command is specified with invalid parameter, only the usage for that specific command is shown now - It is now possible to use a SOCKS5 proxy for connection - The output of commands has been improved in the following ways: - Strings on commands such as getbestblockhash no longer have quotes wrapped around them - Fields that are integers no longer show in scientific notation when they are large (timestamps for example) This closes #305 as a side effect.
2015-01-18 21:45:20 +01:00
return &cfg, remainingArgs, nil
}
2016-06-04 04:14:15 +02:00
// createDefaultConfig creates a basic config file at the given destination path.
// 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)
2016-06-04 04:14:15 +02:00
if err != nil {
return err
}
defer serverConfigFile.Close()
content, err := ioutil.ReadAll(serverConfigFile)
2016-06-04 04:14:15 +02:00
if err != nil {
return err
}
// Extract the rpcuser
rpcUserRegexp := regexp.MustCompile(`(?m)^\s*rpcuser=([^\s]+)`)
2016-06-04 04:14:15 +02:00
userSubmatches := rpcUserRegexp.FindSubmatch(content)
if userSubmatches == nil {
// No user found, nothing to do
return nil
}
// Extract the rpcpass
rpcPassRegexp := regexp.MustCompile(`(?m)^\s*rpcpass=([^\s]+)`)
2016-06-04 04:14:15 +02:00
passSubmatches := rpcPassRegexp.FindSubmatch(content)
if passSubmatches == nil {
// No password found, nothing to do
return nil
}
// Extract the notls
noTLSRegexp := regexp.MustCompile(`(?m)^\s*notls=(0|1)(?:\s|$)`)
noTLSSubmatches := noTLSRegexp.FindSubmatch(content)
// Create the destination directory if it does not exists
err = os.MkdirAll(filepath.Dir(destinationPath), 0700)
if err != nil {
return err
}
2016-06-04 04:14:15 +02:00
// Create the destination file and write the rpcuser and rpcpass to it
dest, err := os.OpenFile(destinationPath,
os.O_RDWR|os.O_CREATE|os.O_TRUNC, 0600)
2016-06-04 04:14:15 +02:00
if err != nil {
return err
}
defer dest.Close()
destString := fmt.Sprintf("rpcuser=%s\nrpcpass=%s\n",
string(userSubmatches[1]), string(passSubmatches[1]))
if noTLSSubmatches != nil {
destString += fmt.Sprintf("notls=%s\n", noTLSSubmatches[1])
}
dest.WriteString(destString)
2016-06-04 04:14:15 +02:00
return nil
}