mirror of
https://github.com/kaspanet/kaspad.git
synced 2025-03-30 15:08:33 +00:00
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:
parent
0561347ff1
commit
3916534a7e
@ -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)
|
||||
|
Loading…
x
Reference in New Issue
Block a user