Skip to content

Commit

Permalink
lbcctl: support --timed, --quiet options
Browse files Browse the repository at this point in the history
  • Loading branch information
roylee17 committed Sep 29, 2022
1 parent 987a533 commit cbc4d48
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 3 deletions.
2 changes: 2 additions & 0 deletions cmd/lbcctl/config.go
Expand Up @@ -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
Expand Down
19 changes: 16 additions & 3 deletions cmd/lbcctl/lbcctl.go
Expand Up @@ -9,6 +9,7 @@ import (
"os"
"path/filepath"
"strings"
"time"

"github.com/lbryio/lbcd/btcjson"
)
Expand Down Expand Up @@ -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)
Expand All @@ -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, "[") {
Expand All @@ -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
Expand All @@ -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)
}
}

0 comments on commit cbc4d48

Please sign in to comment.