mirror of
https://github.com/btcsuite/btcd.git
synced 2025-01-19 14:45:34 +01:00
btcctl: Support - argument to read from stdin.
This commit modifies the argument handling for btcctl to treat a parameter that is a single dash as an indicator to read that paramter from stdin instead. This change allows commands, such as the submitblock, to accept data piped from stdin for any parameter. This, in turn, allows large arguments, such as blocks, which can often be too big for a single argument due to Operating System limitations to be submitted by putting them into a file and redirecting stdin. For example: btcctl submitblock - <block.hex cat block.hex | btcctl submitblock - btcctl sendrawtransaction - <tx.hex cat tx.hex | btcctl sendrawtransaction -
This commit is contained in:
parent
279308288c
commit
d3aebcaed3
@ -1,9 +1,11 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"bufio"
|
||||
"bytes"
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"io"
|
||||
"os"
|
||||
"path/filepath"
|
||||
"strings"
|
||||
@ -72,8 +74,31 @@ func main() {
|
||||
|
||||
// Convert remaining command line args to a slice of interface values
|
||||
// to be passed along as parameters to new command creation function.
|
||||
//
|
||||
// Since some commands, such as submitblock, can involve data which is
|
||||
// too large for the Operating System to allow as a normal command line
|
||||
// parameter, support using '-' as an argument to allow the argument
|
||||
// to be read from a stdin pipe.
|
||||
bio := bufio.NewReader(os.Stdin)
|
||||
params := make([]interface{}, 0, len(args[1:]))
|
||||
for _, arg := range args[1:] {
|
||||
if arg == "-" {
|
||||
param, err := bio.ReadString('\n')
|
||||
if err != nil && err != io.EOF {
|
||||
fmt.Fprintf(os.Stderr, "Failed to read data "+
|
||||
"from stdin: %v\n", err)
|
||||
os.Exit(1)
|
||||
}
|
||||
if err == io.EOF && len(param) == 0 {
|
||||
fmt.Fprintln(os.Stderr, "Not enough lines "+
|
||||
"provided on stdin")
|
||||
os.Exit(1)
|
||||
}
|
||||
param = strings.TrimRight(param, "\r\n")
|
||||
params = append(params, param)
|
||||
continue
|
||||
}
|
||||
|
||||
params = append(params, arg)
|
||||
}
|
||||
|
||||
|
@ -190,6 +190,11 @@ func loadConfig() (*config, []string, error) {
|
||||
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.")
|
||||
return nil, nil, err
|
||||
}
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user