forked from Azure/azure-extensions-cli
-
Notifications
You must be signed in to change notification settings - Fork 1
/
replicationstatus.go
53 lines (47 loc) · 1.3 KB
/
replicationstatus.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
package main
import (
"encoding/json"
"fmt"
"os"
log "github.com/Sirupsen/logrus"
"github.com/codegangsta/cli"
"github.com/olekukonko/tablewriter"
)
func replicationStatus(c *cli.Context) {
cl := mkClient(checkFlag(c, flMgtURL.Name), checkFlag(c, flSubsID.Name), checkFlag(c, flSubsCert.Name))
ns, name, version := checkFlag(c, flNamespace.Name), checkFlag(c, flName.Name), checkFlag(c, flVersion.Name)
json := c.Bool(flJSON.Name)
log.Debug("Requesting replication status.")
rs, err := cl.GetReplicationStatus(ns, name, version)
if err != nil {
log.Fatalf("Cannot fetch replication status: %v", err)
}
var f func(_ ReplicationStatusResponse) error
if json {
f = printAsJSON
} else {
f = printAsTable
}
if err := f(rs); err != nil {
log.Fatal(err)
}
}
func printAsJSON(r ReplicationStatusResponse) error {
b, err := json.MarshalIndent(r.Statuses, "", " ")
if err != nil {
return fmt.Errorf("failed to format as json: %+v", err)
}
fmt.Fprintf(os.Stdout, "%s", string(b))
return nil
}
func printAsTable(r ReplicationStatusResponse) error {
table := tablewriter.NewWriter(os.Stdout)
table.SetHeader([]string{"Location", "Status"})
data := [][]string{}
for _, s := range r.Statuses {
data = append(data, []string{s.Location, s.Status})
}
table.AppendBulk(data)
table.Render()
return nil
}