Skip to content

Commit 4f3b193

Browse files
authored
Merge pull request #6 from ahmetalpbalkan/json
replication-status: add --json flag
2 parents d6a4863 + 40cd65c commit 4f3b193

File tree

2 files changed

+31
-2
lines changed

2 files changed

+31
-2
lines changed

main.go

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,9 @@ var (
6363
Name: "region-2",
6464
Usage: "Primary pilot location to roll out the extension (e.g. 'Brazil South')",
6565
EnvVar: "REGION2"}
66+
flJSON = cli.BoolFlag{
67+
Name: "json",
68+
Usage: "Print output as JSON"}
6669
)
6770

6871
func main() {
@@ -126,7 +129,7 @@ func main() {
126129
Action: listVersions},
127130
{Name: "replication-status",
128131
Usage: "Retrieves replication status for an uploaded extension package",
129-
Flags: []cli.Flag{flMgtURL, flSubsID, flSubsCert, flNamespace, flName, flVersion},
132+
Flags: []cli.Flag{flMgtURL, flSubsID, flSubsCert, flNamespace, flName, flVersion, flJSON},
130133
Action: replicationStatus},
131134
{Name: "unpublish-version",
132135
Usage: "Marks the specified version of the extension internal. Does not delete.",

replicationstatus.go

Lines changed: 27 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
package main
22

33
import (
4+
"encoding/json"
5+
"fmt"
46
"os"
57

68
log "github.com/Sirupsen/logrus"
@@ -11,17 +13,41 @@ import (
1113
func 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

Comments
 (0)