-
Notifications
You must be signed in to change notification settings - Fork 1
/
extensionList.go
101 lines (83 loc) · 2.49 KB
/
extensionList.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
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
package extension
import (
"context"
"fmt"
"strings"
"github.com/clintjedwards/gofer/internal/cli/cl"
cliformat "github.com/clintjedwards/gofer/internal/cli/format"
proto "github.com/clintjedwards/gofer/proto/go"
"github.com/olekukonko/tablewriter"
"github.com/spf13/cobra"
"google.golang.org/grpc/metadata"
)
var cmdExtensionList = &cobra.Command{
Use: "list",
Short: "List all extensions",
Example: `$ gofer extension list`,
RunE: extensionList,
}
func init() {
CmdExtension.AddCommand(cmdExtensionList)
}
func extensionList(_ *cobra.Command, _ []string) error {
cl.State.Fmt.Print("Retrieving extensions")
conn, err := cl.State.Connect()
if err != nil {
cl.State.Fmt.PrintErr(err)
cl.State.Fmt.Finish()
return err
}
client := proto.NewGoferClient(conn)
md := metadata.Pairs("Authorization", "Bearer "+cl.State.Config.Token)
ctx := metadata.NewOutgoingContext(context.Background(), md)
resp, err := client.ListExtensions(ctx, &proto.ListExtensionsRequest{})
if err != nil {
cl.State.Fmt.PrintErr(fmt.Sprintf("could not list extensions: %v", err))
cl.State.Fmt.Finish()
return err
}
data := [][]string{}
for _, extension := range resp.Extensions {
data = append(data, []string{
extension.Name,
extension.Url,
cliformat.ColorizeExtensionState(cliformat.NormalizeEnumValue(extension.State.String(), "Unknown")),
cliformat.ColorizeExtensionStatus(cliformat.NormalizeEnumValue(extension.Status.String(), "Unknown")),
})
}
table := formatTable(data, true)
cl.State.Fmt.Println(table)
cl.State.Fmt.Finish()
return nil
}
func formatTable(data [][]string, color bool) string {
tableString := &strings.Builder{}
table := tablewriter.NewWriter(tableString)
table.SetHeader([]string{"Name", "URL", "State", "Status"})
table.SetAlignment(tablewriter.ALIGN_LEFT)
table.SetHeaderAlignment(tablewriter.ALIGN_LEFT)
table.SetHeaderLine(true)
table.SetBorder(false)
table.SetAutoFormatHeaders(false)
table.SetRowSeparator("―")
table.SetRowLine(false)
table.SetColumnSeparator("")
table.SetCenterSeparator("")
if color {
table.SetHeaderColor(
tablewriter.Color(tablewriter.FgBlueColor),
tablewriter.Color(tablewriter.FgBlueColor),
tablewriter.Color(tablewriter.FgBlueColor),
tablewriter.Color(tablewriter.FgBlueColor),
)
table.SetColumnColor(
tablewriter.Color(tablewriter.FgYellowColor),
tablewriter.Color(0),
tablewriter.Color(0),
tablewriter.Color(0),
)
}
table.AppendBulk(data)
table.Render()
return tableString.String()
}