In kaspactl, prettify responses before printing them (#1453)

* In kaspactl, prettify responses before printing them.

* Indent with four spaces instead of a tab.

* Unwrap the response.

* Simplify unwrapping the response.

* Don't unwrap responses.

* Use protojson.MarshalOptions for prettification.
This commit is contained in:
stasatdaglabs 2021-01-27 09:13:48 +02:00 committed by GitHub
parent 0561347ff1
commit 3916534a7e
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -2,6 +2,7 @@ package main
import (
"fmt"
"github.com/kaspanet/kaspad/infrastructure/network/netadapter/server/grpcserver/protowire"
"os"
"time"
@ -42,7 +43,8 @@ func main() {
timeout := time.Duration(cfg.Timeout) * time.Second
select {
case responseString := <-responseChan:
fmt.Println(responseString)
prettyResponseString := prettifyResponse(responseString)
fmt.Println(prettyResponseString)
case <-time.After(timeout):
printErrorAndExit(fmt.Sprintf("timeout of %s has been exceeded", timeout))
}
@ -81,6 +83,18 @@ func postJSON(cfg *configFlags, client *grpcclient.GRPCClient, doneChan chan str
doneChan <- responseString
}
func prettifyResponse(response string) string {
kaspadMessage := &protowire.KaspadMessage{}
err := protojson.Unmarshal([]byte(response), kaspadMessage)
if err != nil {
printErrorAndExit(fmt.Sprintf("error parsing the response from the RPC server: %s", err))
}
marshalOptions := &protojson.MarshalOptions{}
marshalOptions.Indent = " "
return marshalOptions.Format(kaspadMessage)
}
func printErrorAndExit(message string) {
fmt.Fprintf(os.Stderr, fmt.Sprintf("%s\n", message))
os.Exit(1)