Skip to content

Commit e99d389

Browse files
authored
Merge pull request #635 from oasisprotocol/ptrus/feature/rofl-show-json
rofl/show: Support json format output
2 parents 8d241dc + a70f74b commit e99d389

File tree

2 files changed

+61
-4
lines changed

2 files changed

+61
-4
lines changed

cmd/common/json.go

Lines changed: 38 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,21 +2,24 @@ package common
22

33
import (
44
"context"
5+
"encoding/base64"
56
"encoding/json"
67
"fmt"
78
"strings"
89
"unicode/utf8"
910

11+
"github.com/spf13/cobra"
12+
1013
"github.com/oasisprotocol/oasis-core/go/common"
1114
coreSignature "github.com/oasisprotocol/oasis-core/go/common/crypto/signature"
1215
consensusPretty "github.com/oasisprotocol/oasis-core/go/common/prettyprint"
1316
"github.com/oasisprotocol/oasis-sdk/client-sdk/go/config"
1417
"github.com/oasisprotocol/oasis-sdk/client-sdk/go/crypto/signature"
1518
"github.com/oasisprotocol/oasis-sdk/client-sdk/go/types"
16-
"github.com/spf13/cobra"
1719

1820
"github.com/oasisprotocol/oasis-core/go/common/cbor"
1921
"github.com/oasisprotocol/oasis-sdk/client-sdk/go/modules/contracts"
22+
"github.com/oasisprotocol/oasis-sdk/client-sdk/go/modules/rofl"
2023
)
2124

2225
// PrettyJSONMarshal returns pretty-printed JSON encoding of v.
@@ -46,6 +49,40 @@ func JSONMarshalKey(k interface{}) (keyJSON []byte, err error) {
4649
return
4750
}
4851

52+
// JSONPrettyPrintRoflAppConfig is a wrapper around rofl.AppConfig that implements custom JSON marshaling.
53+
type JSONPrettyPrintRoflAppConfig rofl.AppConfig
54+
55+
// MarshalJSON implements custom JSON marshaling.
56+
func (a JSONPrettyPrintRoflAppConfig) MarshalJSON() ([]byte, error) {
57+
type alias JSONPrettyPrintRoflAppConfig
58+
out := struct {
59+
alias
60+
// Overrides alias field so JSON marshaler uses this base64 string.
61+
SEK string `json:"sek"`
62+
}{
63+
alias: alias(a),
64+
SEK: base64.StdEncoding.EncodeToString(a.SEK[:]),
65+
}
66+
return json.Marshal(out)
67+
}
68+
69+
// JSONPrettyPrintRoflRegistration is a wrapper around rofl.Registration that implements custom JSON marshaling.
70+
type JSONPrettyPrintRoflRegistration rofl.Registration
71+
72+
// MarshalJSON implements custom JSON marshaling.
73+
func (r JSONPrettyPrintRoflRegistration) MarshalJSON() ([]byte, error) {
74+
type alias JSONPrettyPrintRoflRegistration
75+
out := struct {
76+
alias
77+
// Overrides alias field so JSON marshaler uses this base64 string.
78+
REK string `json:"rek"`
79+
}{
80+
alias: alias(r),
81+
REK: base64.StdEncoding.EncodeToString(r.REK[:]),
82+
}
83+
return json.Marshal(out)
84+
}
85+
4986
// JSONPrintKeyValueTuple traverses potentially large number of items and prints JSON representation
5087
// of them.
5188
//

cmd/rofl/mgmt.go

Lines changed: 23 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -447,6 +447,28 @@ var (
447447
appCfg, err := conn.Runtime(npa.ParaTime).ROFL.App(ctx, client.RoundLatest, appID)
448448
cobra.CheckErr(err)
449449

450+
replicas, err := conn.Runtime(npa.ParaTime).ROFL.AppInstances(ctx, client.RoundLatest, appID)
451+
cobra.CheckErr(err)
452+
453+
if common.OutputFormat() == common.FormatJSON {
454+
output := struct {
455+
App common.JSONPrettyPrintRoflAppConfig `json:"app"`
456+
Replicas []common.JSONPrettyPrintRoflRegistration `json:"replicas"`
457+
}{
458+
App: common.JSONPrettyPrintRoflAppConfig(*appCfg),
459+
Replicas: make([]common.JSONPrettyPrintRoflRegistration, 0, len(replicas)),
460+
}
461+
// Convert replicas to pretty-printable format.
462+
for _, ai := range replicas {
463+
output.Replicas = append(output.Replicas, common.JSONPrettyPrintRoflRegistration(*ai))
464+
}
465+
466+
jsonOutput, err := common.PrettyJSONMarshal(output)
467+
cobra.CheckErr(err)
468+
fmt.Println(string(jsonOutput))
469+
return
470+
}
471+
450472
fmt.Printf("App ID: %s\n", appCfg.ID)
451473
fmt.Printf("Admin: ")
452474
switch appCfg.Admin {
@@ -479,9 +501,6 @@ var (
479501
fmt.Println()
480502
fmt.Printf("=== Replicas ===\n")
481503

482-
replicas, err := conn.Runtime(npa.ParaTime).ROFL.AppInstances(ctx, client.RoundLatest, appID)
483-
cobra.CheckErr(err)
484-
485504
if len(replicas) > 0 {
486505
for _, ai := range replicas {
487506
fmt.Printf("- RAK: %s\n", ai.RAK)
@@ -826,6 +845,7 @@ func init() {
826845

827846
showCmd.Flags().AddFlagSet(common.SelectorFlags)
828847
showCmd.Flags().AddFlagSet(roflCommon.DeploymentFlags)
848+
showCmd.Flags().AddFlagSet(common.FormatFlag)
829849

830850
secretSetCmd.Flags().AddFlagSet(roflCommon.DeploymentFlags)
831851
secretSetCmd.Flags().StringVar(&pubName, "public-name", "", "public secret name")

0 commit comments

Comments
 (0)