Skip to content

Commit

Permalink
Merge pull request #4266 from oasisprotocol/tjanez/cmd-pretty-json
Browse files Browse the repository at this point in the history
go/oasis-node/cmd: Use pretty-printed variant when outputting JSON
  • Loading branch information
tjanez authored Sep 22, 2021
2 parents b4e0209 + 9f45a50 commit ac71808
Show file tree
Hide file tree
Showing 16 changed files with 150 additions and 66 deletions.
1 change: 1 addition & 0 deletions .changelog/4266.feature.1.md
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
go/oasis-node/cmd: Use pretty-printed variant when outputting JSON
1 change: 1 addition & 0 deletions .changelog/4266.feature.2.md
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
go/common/entity: Use pretty-printed JSON when saving entity to file
1 change: 1 addition & 0 deletions .changelog/4266.internal.md
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
go/oasis-node/cmd/common: Add `PrettyJSONMarshal()` helper
2 changes: 1 addition & 1 deletion go/common/entity/entity.go
Original file line number Diff line number Diff line change
Expand Up @@ -129,7 +129,7 @@ func (e *Entity) Save(baseDir string) error {
entityPath := filepath.Join(baseDir, entityFilename)

// Write to disk.
b, err := json.Marshal(e)
b, err := json.MarshalIndent(e, "", " ")
if err != nil {
return err
}
Expand Down
9 changes: 4 additions & 5 deletions go/oasis-node/cmd/common/consensus/consensus.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ package consensus

import (
"context"
"encoding/json"
"fmt"
"io/ioutil"
"os"
Expand Down Expand Up @@ -142,15 +141,15 @@ func SignAndSaveTx(ctx context.Context, tx *transaction.Transaction, signer sign
os.Exit(1)
}

rawTx, err := json.Marshal(sigTx)
prettySigTx, err := cmdCommon.PrettyJSONMarshal(sigTx)
if err != nil {
logger.Error("failed to marshal transaction",
logger.Error("failed to get pretty JSON of signed transaction",
"err", err,
)
os.Exit(1)
}
if err = ioutil.WriteFile(viper.GetString(CfgTxFile), rawTx, 0o600); err != nil {
logger.Error("failed to save transaction",
if err = ioutil.WriteFile(viper.GetString(CfgTxFile), prettySigTx, 0o600); err != nil {
logger.Error("failed to save signed transaction",
"err", err,
)
os.Exit(1)
Expand Down
15 changes: 15 additions & 0 deletions go/oasis-node/cmd/common/json.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
package common

import (
"encoding/json"
"fmt"
)

// PrettyJSONMarshal returns pretty-printed JSON encoding of v.
func PrettyJSONMarshal(v interface{}) ([]byte, error) {
formatted, err := json.MarshalIndent(v, "", " ")
if err != nil {
return nil, fmt.Errorf("failed to marshal to pretty JSON: %w", err)
}
return formatted, nil
}
6 changes: 3 additions & 3 deletions go/oasis-node/cmd/control/control.go
Original file line number Diff line number Diff line change
Expand Up @@ -220,14 +220,14 @@ func doStatus(cmd *cobra.Command, args []string) {
)
os.Exit(128)
}
formatted, err := json.MarshalIndent(status, "", " ")
prettyStatus, err := cmdCommon.PrettyJSONMarshal(status)
if err != nil {
logger.Error("failed to format status",
logger.Error("failed to get pretty JSON of node status",
"err", err,
)
os.Exit(1)
}
fmt.Println(string(formatted))
fmt.Println(string(prettyStatus))
}

// Register registers the client sub-command and all of it's children.
Expand Down
7 changes: 3 additions & 4 deletions go/oasis-node/cmd/debug/beacon/beacon.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ package beacon

import (
"context"
"encoding/json"
"fmt"
"os"

Expand Down Expand Up @@ -84,14 +83,14 @@ func doBeaconStatus(cmd *cobra.Command, args []string) {
}
}

formatted, err := json.MarshalIndent(prettyOut, "", " ")
prettyJSON, err := cmdCommon.PrettyJSONMarshal(prettyOut)
if err != nil {
logger.Error("failed to format state",
logger.Error("failed to get pretty JSON of beacon state",
"err", err,
)
os.Exit(1)
}
fmt.Println(string(formatted))
fmt.Println(string(prettyJSON))
}

// Register registers the beacon sub-command and all of it's children.
Expand Down
5 changes: 2 additions & 3 deletions go/oasis-node/cmd/debug/dumpdb/dumpdb.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ package dumpdb

import (
"context"
"encoding/json"
"fmt"
"os"
"path/filepath"
Expand Down Expand Up @@ -254,14 +253,14 @@ func doDumpDB(cmd *cobra.Command, args []string) {
if shouldClose {
defer w.Close()
}
raw, err := json.Marshal(doc)
prettyDoc, err := cmdCommon.PrettyJSONMarshal(doc)
if err != nil {
logger.Error("failed to marshal state dump into JSON",
"err", err,
)
return
}
if _, err := w.Write(raw); err != nil {
if _, err := w.Write(prettyDoc); err != nil {
logger.Error("failed to write state dump file",
"err", err,
)
Expand Down
32 changes: 25 additions & 7 deletions go/oasis-node/cmd/governance/governance.go
Original file line number Diff line number Diff line change
Expand Up @@ -198,14 +198,20 @@ func doProposalInfo(cmd *cobra.Command, args []string) {
defer conn.Close()

ctx := context.Background()
p, err := client.Proposal(ctx, &governance.ProposalQuery{Height: consensus.HeightLatest, ProposalID: id})
proposal, err := client.Proposal(ctx, &governance.ProposalQuery{Height: consensus.HeightLatest, ProposalID: id})
if err != nil {
logger.Error("error querying proposal", "err", err)
os.Exit(1)
}

o, _ := json.Marshal(p)
fmt.Println(string(o))
prettyProposal, err := cmdCommon.PrettyJSONMarshal(proposal)
if err != nil {
logger.Error("failed to get pretty JSON of proposal",
"err", err,
)
os.Exit(1)
}
fmt.Println(string(prettyProposal))
}

func doProposalVotes(cmd *cobra.Command, args []string) {
Expand All @@ -229,8 +235,14 @@ func doProposalVotes(cmd *cobra.Command, args []string) {
os.Exit(1)
}

o, _ := json.Marshal(votes)
fmt.Println(string(o))
prettyVotes, err := cmdCommon.PrettyJSONMarshal(votes)
if err != nil {
logger.Error("failed to get pretty JSON of votes",
"err", err,
)
os.Exit(1)
}
fmt.Println(string(prettyVotes))
}

func doListProposals(cmd *cobra.Command, args []string) {
Expand All @@ -256,8 +268,14 @@ func doListProposals(cmd *cobra.Command, args []string) {
os.Exit(1)
}

o, _ := json.Marshal(proposals)
fmt.Println(string(o))
prettyProposals, err := cmdCommon.PrettyJSONMarshal(proposals)
if err != nil {
logger.Error("failed to get pretty JSON of proposals",
"err", err,
)
os.Exit(1)
}
fmt.Println(string(prettyProposals))
}

// Register registers the governance sub-command and all of it's children.
Expand Down
25 changes: 18 additions & 7 deletions go/oasis-node/cmd/keymanager/keymanager.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ package keymanager
import (
"bytes"
"encoding/hex"
"encoding/json"
"errors"
"fmt"
"io/ioutil"
Expand Down Expand Up @@ -316,8 +315,14 @@ func verifyPolicyFromFlags() error {

// Output policy content in JSON, if verbose switch given.
if cmdFlags.Verbose() {
c, _ := json.Marshal(policy)
fmt.Printf("%s\n", string(c))
prettyPolicy, err := cmdCommon.PrettyJSONMarshal(policy)
if err != nil {
logger.Error("failed to get pretty JSON of policy",
"err", err,
)
os.Exit(1)
}
fmt.Println(string(prettyPolicy))
}

// Check the signatures of the policy. Public key is taken from the PEM
Expand Down Expand Up @@ -364,16 +369,22 @@ func doInitStatus(cmd *cobra.Command, args []string) {
cmdCommon.EarlyLogAndExit(err)
}

s, err := statusFromFlags()
status, err := statusFromFlags()
if err != nil {
logger.Error("failed to generate status",
"err", err,
)
os.Exit(1)
}

c, _ := json.Marshal(s)
if err = ioutil.WriteFile(viper.GetString(CfgStatusFile), c, 0o644); err != nil { // nolint: gosec
prettyStatus, err := cmdCommon.PrettyJSONMarshal(status)
if err != nil {
logger.Error("failed to get pretty JSON of key manager status",
"err", err,
)
os.Exit(1)
}
if err = ioutil.WriteFile(viper.GetString(CfgStatusFile), prettyStatus, 0o644); err != nil { // nolint: gosec
logger.Error("failed to write key manager status json file",
"err", err,
"CfgStatusFile", viper.GetString(CfgStatusFile),
Expand All @@ -382,7 +393,7 @@ func doInitStatus(cmd *cobra.Command, args []string) {
}

logger.Info("generated key manager status file",
"Status.ID", s.ID,
"Status.ID", status.ID,
)
}

Expand Down
28 changes: 21 additions & 7 deletions go/oasis-node/cmd/registry/entity/entity.go
Original file line number Diff line number Diff line change
Expand Up @@ -264,8 +264,14 @@ func signAndWriteEntityGenesis(dataDir string, signer signature.Signer, ent *ent
}

// Write out the signed entity registration.
b, _ := json.Marshal(signed)
if err = ioutil.WriteFile(filepath.Join(dataDir, entityGenesisFilename), b, 0o600); err != nil {
prettySigned, err := cmdCommon.PrettyJSONMarshal(signed)
if err != nil {
logger.Error("failed to get pretty JSON of signed entity genesis registration",
"err", err,
)
os.Exit(1)
}
if err = ioutil.WriteFile(filepath.Join(dataDir, entityGenesisFilename), prettySigned, 0o600); err != nil {
logger.Error("failed to write signed entity genesis registration",
"err", err,
)
Expand Down Expand Up @@ -337,16 +343,24 @@ func doList(cmd *cobra.Command, args []string) {
}

for _, ent := range entities {
var s string
var entString string
switch cmdFlags.Verbose() {
case true:
b, _ := json.Marshal(ent)
s = string(b)
prettyEnt, err := cmdCommon.PrettyJSONMarshal(ent)
if err != nil {
logger.Error("failed to get pretty JSON of entity",
"err", err,
"entity ID", ent.ID.String(),
)
entString = fmt.Sprintf("[invalid pretty JSON for entity %s]", ent.ID)
} else {
entString = string(prettyEnt)
}
default:
s = ent.ID.String()
entString = ent.ID.String()
}

fmt.Printf("%v\n", s)
fmt.Println(entString)
}
}

Expand Down
29 changes: 21 additions & 8 deletions go/oasis-node/cmd/registry/node/node.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ package node

import (
"context"
"encoding/json"
"fmt"
"io/ioutil"
"os"
Expand Down Expand Up @@ -258,8 +257,14 @@ func doInit(cmd *cobra.Command, args []string) { // nolint: gocyclo
)
os.Exit(1)
}
b, _ := json.Marshal(signed)
if err = ioutil.WriteFile(filepath.Join(dataDir, NodeGenesisFilename), b, 0o600); err != nil {
prettySigned, err := cmdCommon.PrettyJSONMarshal(signed)
if err != nil {
logger.Error("failed to get pretty JSON of signed node genesis registration",
"err", err,
)
os.Exit(1)
}
if err = ioutil.WriteFile(filepath.Join(dataDir, NodeGenesisFilename), prettySigned, 0o600); err != nil {
logger.Error("failed to write signed node genesis registration",
"err", err,
)
Expand Down Expand Up @@ -304,16 +309,24 @@ func doList(cmd *cobra.Command, args []string) {
}

for _, node := range nodes {
var s string
var nodeString string
switch cmdFlags.Verbose() {
case true:
b, _ := json.Marshal(node)
s = string(b)
prettyNode, err := cmdCommon.PrettyJSONMarshal(node)
if err != nil {
logger.Error("failed to get pretty JSON of node",
"err", err,
"node ID", node.ID.String(),
)
nodeString = fmt.Sprintf("[invalid pretty JSON for node %s]", node.ID)
} else {
nodeString = string(prettyNode)
}
default:
s = node.ID.String()
nodeString = node.ID.String()
}

fmt.Printf("%v\n", s)
fmt.Println(nodeString)
}
}

Expand Down
18 changes: 13 additions & 5 deletions go/oasis-node/cmd/registry/runtime/runtime.go
Original file line number Diff line number Diff line change
Expand Up @@ -133,16 +133,24 @@ func doList(cmd *cobra.Command, args []string) {
}

for _, rt := range runtimes {
var s string
var rtString string
switch cmdFlags.Verbose() {
case true:
b, _ := json.Marshal(rt)
s = string(b)
prettyRt, err := cmdCommon.PrettyJSONMarshal(rt)
if err != nil {
logger.Error("failed to get pretty JSON of runtime",
"err", err,
"runtime ID", rt.ID.String(),
)
rtString = fmt.Sprintf("[invalid pretty JSON for runtime %s]", rt.ID)
} else {
rtString = string(prettyRt)
}
default:
s = rt.ID.String()
rtString = rt.ID.String()
}

fmt.Printf("%v\n", s)
fmt.Println(rtString)
}
}

Expand Down
Loading

0 comments on commit ac71808

Please sign in to comment.