11package main
22
33import (
4+ "encoding/json"
5+ "fmt"
46 "os"
57
68 log "github.com/Sirupsen/logrus"
@@ -11,17 +13,41 @@ import (
1113func replicationStatus (c * cli.Context ) {
1214 cl := mkClient (checkFlag (c , flMgtURL .Name ), checkFlag (c , flSubsID .Name ), checkFlag (c , flSubsCert .Name ))
1315 ns , name , version := checkFlag (c , flNamespace .Name ), checkFlag (c , flName .Name ), checkFlag (c , flVersion .Name )
16+ json := c .Bool (flJSON .Name )
1417 log .Debug ("Requesting replication status." )
1518 rs , err := cl .GetReplicationStatus (ns , name , version )
1619 if err != nil {
1720 log .Fatalf ("Cannot fetch replication status: %v" , err )
1821 }
22+
23+ var f func (_ ReplicationStatusResponse ) error
24+ if json {
25+ f = printAsJson
26+ } else {
27+ f = printAsTable
28+ }
29+ if err := f (rs ); err != nil {
30+ log .Fatal (err )
31+ }
32+ }
33+
34+ func printAsJson (r ReplicationStatusResponse ) error {
35+ b , err := json .MarshalIndent (r .Statuses , "" , " " )
36+ if err != nil {
37+ return fmt .Errorf ("failed to format as json: %+v" , err )
38+ }
39+ fmt .Fprintf (os .Stdout , "%s" , string (b ))
40+ return nil
41+ }
42+
43+ func printAsTable (r ReplicationStatusResponse ) error {
1944 table := tablewriter .NewWriter (os .Stdout )
2045 table .SetHeader ([]string {"Location" , "Status" })
2146 data := [][]string {}
22- for _ , s := range rs .Statuses {
47+ for _ , s := range r .Statuses {
2348 data = append (data , []string {s .Location , s .Status })
2449 }
2550 table .AppendBulk (data )
2651 table .Render ()
52+ return nil
2753}
0 commit comments