From cbc4d489e8d2fc1b15a6798960cee766ab45dd1f Mon Sep 17 00:00:00 2001 From: Roy Lee Date: Thu, 29 Sep 2022 16:45:21 -0700 Subject: [PATCH] lbcctl: support --timed, --quiet options --- cmd/lbcctl/config.go | 2 ++ cmd/lbcctl/lbcctl.go | 19 ++++++++++++++++--- 2 files changed, 18 insertions(+), 3 deletions(-) diff --git a/cmd/lbcctl/config.go b/cmd/lbcctl/config.go index 90289663..81b26103 100644 --- a/cmd/lbcctl/config.go +++ b/cmd/lbcctl/config.go @@ -111,6 +111,8 @@ type config struct { 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)"` ShowVersion bool `short:"V" long:"version" description:"Display version information and exit"` + Timed bool `short:"t" long:"timed" description:"Display RPC response time"` + Quiet bool `short:"q" long:"quiet" description:"Do not output results to stdout"` } // normalizeAddress returns addr with the passed default port appended if diff --git a/cmd/lbcctl/lbcctl.go b/cmd/lbcctl/lbcctl.go index 79349186..40132c65 100644 --- a/cmd/lbcctl/lbcctl.go +++ b/cmd/lbcctl/lbcctl.go @@ -9,6 +9,7 @@ import ( "os" "path/filepath" "strings" + "time" "github.com/lbryio/lbcd/btcjson" ) @@ -133,6 +134,8 @@ func main() { os.Exit(1) } + started := time.Now() + // Send the JSON-RPC request to the server using the user-specified // connection configuration. result, err := sendPostRequest(marshalledJSON, cfg) @@ -141,6 +144,16 @@ func main() { os.Exit(1) } + if cfg.Timed { + elapsed := time.Since(started) + defer fmt.Fprintf(os.Stderr, "%s\n", elapsed) + } + + var output io.Writer = os.Stdout + if cfg.Quiet { + output = io.Discard + } + // Choose how to display the result based on its type. strResult := string(result) if strings.HasPrefix(strResult, "{") || strings.HasPrefix(strResult, "[") { @@ -150,7 +163,7 @@ func main() { err) os.Exit(1) } - fmt.Println(dst.String()) + fmt.Fprintln(output, dst.String()) } else if strings.HasPrefix(strResult, `"`) { var str string @@ -159,9 +172,9 @@ func main() { err) os.Exit(1) } - fmt.Println(str) + fmt.Fprintln(output, str) } else if strResult != "null" { - fmt.Println(strResult) + fmt.Fprintln(output, strResult) } }