Skip to content

Commit 13a7609

Browse files
authored
Merge pull request #619 from oasisprotocol/amela/fix-format-json
fix(cmd/network/show): Fix --format json output
2 parents 164bb7c + 0fe0d6a commit 13a7609

File tree

2 files changed

+19
-3
lines changed

2 files changed

+19
-3
lines changed

cmd/network/show.go

Lines changed: 17 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@ const (
4343
selParameters
4444
)
4545

46-
func printEntityNodes(ctx context.Context, npa *common.NPASelection, stakingConn staking.Backend, registryConn registry.Backend, beaconConn api.Backend, entity *entity.Entity, height int64) error {
46+
func prettyPrintEntityNodes(ctx context.Context, npa *common.NPASelection, stakingConn staking.Backend, registryConn registry.Backend, beaconConn api.Backend, entity *entity.Entity, height int64) error {
4747
epoch, err := beaconConn.GetEpoch(ctx, height)
4848
if err != nil {
4949
return err
@@ -177,7 +177,14 @@ var showCmd = &cobra.Command{
177177
}
178178

179179
if entity, err := registryConn.GetEntity(ctx, idQuery); err == nil {
180-
err = printEntityNodes(ctx, npa, stakingConn, registryConn, beaconConn, entity, height)
180+
switch common.OutputFormat() {
181+
case common.FormatJSON:
182+
var jsonBytes []byte
183+
jsonBytes, err = json.MarshalIndent(&entity, "", " ")
184+
fmt.Println(string(jsonBytes))
185+
default:
186+
err = prettyPrintEntityNodes(ctx, npa, stakingConn, registryConn, beaconConn, entity, height)
187+
}
181188
cobra.CheckErr(err)
182189
return
183190
}
@@ -210,7 +217,14 @@ var showCmd = &cobra.Command{
210217
cobra.CheckErr(err) // If this doesn't work the other large queries won't either.
211218
for _, entity := range entities {
212219
if staking.NewAddress(entity.ID).Equal(addr) {
213-
err = printEntityNodes(ctx, npa, stakingConn, registryConn, beaconConn, entity, height)
220+
switch common.OutputFormat() {
221+
case common.FormatJSON:
222+
var jsonBytes []byte
223+
jsonBytes, err = json.MarshalIndent(&entity, "", " ")
224+
fmt.Println(string(jsonBytes))
225+
default:
226+
err = prettyPrintEntityNodes(ctx, npa, stakingConn, registryConn, beaconConn, entity, height)
227+
}
214228
cobra.CheckErr(err)
215229
return
216230
}

docs/network.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -414,6 +414,8 @@ The provided ID can be one of the following:
414414

415415
![code json](../examples/network-show/id-entity.out.static)
416416

417+
By passing `--format json`, the output is formatted as JSON.
418+
417419
- If the node ID is provided, Oasis CLI shows detailed information of the node
418420
such as the Oasis Core software version, the node's role, supported
419421
ParaTimes, trusted execution environment support and more. For example:

0 commit comments

Comments
 (0)