forked from Azure/azure-extensions-cli
-
Notifications
You must be signed in to change notification settings - Fork 0
/
versions.go
53 lines (46 loc) · 1.3 KB
/
versions.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 (
"fmt"
"os"
"encoding/json"
log "github.com/Sirupsen/logrus"
"github.com/codegangsta/cli"
"github.com/olekukonko/tablewriter"
)
func listVersions(c *cli.Context) {
cl := mkClient(checkFlag(c, flMgtURL.Name), checkFlag(c, flSubsID.Name), checkFlag(c, flSubsCert.Name))
v, err := cl.ListVersions()
if err != nil {
log.Fatalf("Request failed: %v", err)
}
json := c.Bool(flJSON.Name)
var f func(_ ListVersionsResponse) error
if json {
f = printListVersionsAsJSON
} else {
f = printListVersionsAsTable
}
if err := f(v); err != nil {
log.Fatal(err)
}
}
func printListVersionsAsJSON(r ListVersionsResponse) error {
b, err := json.MarshalIndent(r.Extensions, "", " ")
if err != nil {
return fmt.Errorf("failed to format as json: %+v", err)
}
fmt.Fprintf(os.Stdout, "%s", string(b))
return nil
}
func printListVersionsAsTable(v ListVersionsResponse) error {
table := tablewriter.NewWriter(os.Stdout)
table.SetColWidth(4000)
table.SetHeader([]string{"Namespace", "Type", "Version", "Replicated?", "Internal?", "Regions"})
data := [][]string{}
for _, e := range v.Extensions {
data = append(data, []string{e.Ns, e.Name, e.Version, fmt.Sprintf("%v", e.ReplicationCompleted), fmt.Sprintf("%v", e.IsInternal), e.Regions})
}
table.AppendBulk(data)
table.Render()
return nil
}