/
extensionList.go
111 lines (91 loc) · 2.85 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
102
103
104
105
106
107
108
109
110
111
package extension
import (
"context"
"fmt"
"strings"
"github.com/clintjedwards/gofer/internal/cli/cl"
cliFmt "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 cmdPipelineExtensionList = &cobra.Command{
Use: "list <pipeline_id>",
Short: "List pipeline extensions",
Long: `List pipeline extensions.`,
Example: `$ gofer pipeline extension list simple`,
RunE: pipelineExtensionList,
Args: cobra.ExactArgs(1),
}
func init() {
CmdPipelineExtension.AddCommand(cmdPipelineExtensionList)
}
func pipelineExtensionList(_ *cobra.Command, args []string) error {
pipelineID := args[0]
cl.State.Fmt.Print("Retrieving pipeline extension subscriptions")
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.ListPipelineExtensionSubscriptions(ctx, &proto.ListPipelineExtensionSubscriptionsRequest{
NamespaceId: cl.State.Config.Namespace,
PipelineId: pipelineID,
})
if err != nil {
cl.State.Fmt.PrintErr(fmt.Sprintf("could not get pipeline extension subscriptions: %v", err))
cl.State.Fmt.Finish()
return err
}
if len(resp.Subscriptions) == 0 {
cl.State.Fmt.Println(fmt.Sprintf("No pipeline extension subscriptions found for pipeline %s", pipelineID))
cl.State.Fmt.Finish()
return err
}
data := [][]string{}
for _, subscription := range resp.Subscriptions {
data = append(data, []string{
subscription.Name,
subscription.Label,
cliFmt.ColorizePipelineExtensionSubscriptionStatus(cliFmt.NormalizeEnumValue(subscription.Status.String(), "Unknown")),
})
}
table := formatTable(data, !cl.State.Config.NoColor)
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", "Label", "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),
)
table.SetColumnColor(
tablewriter.Color(tablewriter.FgYellowColor),
tablewriter.Color(0),
tablewriter.Color(0),
)
}
table.AppendBulk(data)
table.Render()
return tableString.String()
}