From ca4cf29e495e8d10a32902e43c2034a189d4aba8 Mon Sep 17 00:00:00 2001 From: Dave Collins Date: Wed, 25 Dec 2013 23:17:03 -0600 Subject: [PATCH] Add a new function to btcctl for JSON display. This commit adds a new function to btcctl that shows the results as properly indented JSON instead of relying on spew and changes all of the commands that used spew to the new function. The output of btcctl should be more user-facing than developer-facing. --- util/btcctl/btcctl.go | 34 ++++++++++++++++++++++++++-------- 1 file changed, 26 insertions(+), 8 deletions(-) diff --git a/util/btcctl/btcctl.go b/util/btcctl/btcctl.go index cccc6372..e575334b 100644 --- a/util/btcctl/btcctl.go +++ b/util/btcctl/btcctl.go @@ -1,6 +1,7 @@ package main import ( + "bytes" "encoding/json" "errors" "fmt" @@ -43,25 +44,25 @@ var ( // commandHandlers is a map of commands and associated handler data that is used // to validate correctness and perform the command. var commandHandlers = map[string]*handlerData{ - "addnode": &handlerData{2, 0, displaySpewDump, nil, makeAddNode, " "}, + "addnode": &handlerData{2, 0, displayJSONDump, nil, makeAddNode, " "}, "debuglevel": &handlerData{1, 0, displayGeneric, nil, makeDebugLevel, ""}, - "decoderawtransaction": &handlerData{1, 0, displaySpewDump, nil, makeDecodeRawTransaction, ""}, + "decoderawtransaction": &handlerData{1, 0, displayJSONDump, nil, makeDecodeRawTransaction, ""}, "dumpprivkey": &handlerData{1, 0, displayGeneric, nil, makeDumpPrivKey, ""}, "getbalance": &handlerData{0, 2, displayGeneric, []conversionHandler{nil, toInt}, makeGetBalance, "[account] [minconf=1]"}, "getbestblockhash": &handlerData{0, 0, displayGeneric, nil, makeGetBestBlockHash, ""}, - "getblock": &handlerData{1, 0, displaySpewDump, nil, makeGetBlock, ""}, + "getblock": &handlerData{1, 0, displayJSONDump, nil, makeGetBlock, ""}, "getblockcount": &handlerData{0, 0, displayFloat64, nil, makeGetBlockCount, ""}, "getblockhash": &handlerData{1, 0, displayGeneric, []conversionHandler{toInt64}, makeGetBlockHash, ""}, "getconnectioncount": &handlerData{0, 0, displayFloat64, nil, makeGetConnectionCount, ""}, "getdifficulty": &handlerData{0, 0, displayFloat64, nil, makeGetDifficulty, ""}, "getgenerate": &handlerData{0, 0, displayGeneric, nil, makeGetGenerate, ""}, "gethashespersec": &handlerData{0, 0, displayGeneric, nil, makeGetHashesPerSec, ""}, - "getpeerinfo": &handlerData{0, 0, displaySpewDump, nil, makeGetPeerInfo, ""}, - "getrawmempool": &handlerData{0, 1, displaySpewDump, []conversionHandler{toBool}, makeGetRawMempool, "[verbose=false]"}, - "getrawtransaction": &handlerData{1, 1, displaySpewDump, []conversionHandler{nil, toBool}, makeGetRawTransaction, " [verbose=false]"}, + "getpeerinfo": &handlerData{0, 0, displayJSONDump, nil, makeGetPeerInfo, ""}, + "getrawmempool": &handlerData{0, 1, displayJSONDump, []conversionHandler{toBool}, makeGetRawMempool, "[verbose=false]"}, + "getrawtransaction": &handlerData{1, 1, displayJSONDump, []conversionHandler{nil, toBool}, makeGetRawTransaction, " [verbose=false]"}, "importprivkey": &handlerData{1, 2, displayGeneric, []conversionHandler{nil, nil, toBool}, makeImportPrivKey, " [label] [rescan=true]"}, - "listtransactions": &handlerData{0, 3, displaySpewDump, []conversionHandler{nil, toInt, toInt}, makeListTransactions, "[account] [count=10] [from=0]"}, - "verifychain": &handlerData{0, 2, displaySpewDump, []conversionHandler{toInt, toInt}, makeVerifyChain, "[level] [depth]"}, + "listtransactions": &handlerData{0, 3, displayJSONDump, []conversionHandler{nil, toInt, toInt}, makeListTransactions, "[account] [count=10] [from=0]"}, + "verifychain": &handlerData{0, 2, displayJSONDump, []conversionHandler{toInt, toInt}, makeVerifyChain, "[level] [depth]"}, "stop": &handlerData{0, 0, displayGeneric, nil, makeStop, ""}, } @@ -125,6 +126,23 @@ func displaySpewDump(reply interface{}) error { return nil } +// displayJSONDump is a displayHandler that uses json.Indent to display the +// passed interface. +func displayJSONDump(reply interface{}) error { + marshaledBytes, err := json.Marshal(reply) + if err != nil { + return err + } + + var buf bytes.Buffer + err = json.Indent(&buf, marshaledBytes, "", "\t") + if err != nil { + return err + } + fmt.Println(buf.String()) + return nil +} + // makeAddNode generates the cmd structure for addnode comands. func makeAddNode(args []interface{}) (btcjson.Cmd, error) { return btcjson.NewAddNodeCmd("btcctl", args[0].(string),