-
Notifications
You must be signed in to change notification settings - Fork 40
/
list.go
88 lines (78 loc) · 2.4 KB
/
list.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
package instance
import (
"context"
"strings"
"connectrpc.com/connect"
"github.com/fatih/color"
"github.com/rigdev/rig-go-api/api/v1/capsule"
"github.com/rigdev/rig-go-api/api/v1/capsule/instance"
"github.com/rigdev/rig-go-api/model"
"github.com/rigdev/rig/cmd/common"
cmd_capsule "github.com/rigdev/rig/cmd/rig/cmd/capsule"
"github.com/rigdev/rig/cmd/rig/cmd/flags"
table2 "github.com/rodaine/table"
"github.com/spf13/cobra"
)
func (c *Cmd) list(ctx context.Context, _ *cobra.Command, _ []string) error {
resp, err := c.Rig.Capsule().ListInstanceStatuses(ctx, connect.NewRequest(&capsule.ListInstanceStatusesRequest{
CapsuleId: cmd_capsule.CapsuleID,
Pagination: &model.Pagination{
Offset: uint32(offset),
Limit: uint32(limit),
Descending: true,
},
ProjectId: flags.GetProject(c.Scope),
EnvironmentId: flags.GetEnvironment(c.Scope),
ExcludeExisting: excludeExisting,
IncludeDeleted: includeDeleted,
}))
if err != nil {
return err
}
instances := resp.Msg.GetInstances()
if flags.Flags.OutputType != common.OutputTypePretty {
return common.FormatPrint(instances, flags.Flags.OutputType)
}
headerFmt := color.New(color.FgBlue, color.Underline).SprintfFunc()
columnFmt := color.New(color.FgYellow).SprintfFunc()
tbl := table2.New("ID", "Created", "Deleted", "Scheduling", "Preparing", "Running", "Deleted")
tbl.WithHeaderFormatter(headerFmt).WithFirstColumnFormatter(columnFmt)
for _, i := range instances {
tbl.AddRow(instanceStatusToTableRow(i)...)
}
tbl.Print()
return nil
}
func instanceStatusToTableRow(instance *instance.Status) []any {
stages := instance.GetStages()
d := stages.GetDeleted().GetInfo().GetUpdatedAt()
ds := "-"
if d != nil {
ds = common.FormatTime(d.AsTime())
}
return []any{
instance.GetInstanceId(),
common.FormatTime(instance.CreatedAt.AsTime()),
ds,
formatRow(stages.GetSchedule()),
formatRow(stages.GetPreparing()),
formatRow(stages.GetRunning()),
formatRow(stages.GetDeleted()),
}
}
type stage interface {
GetInfo() *instance.StageInfo
}
func formatRow(stage stage) string {
info := stage.GetInfo()
if info.GetState() == instance.StageState_STAGE_STATE_UNSPECIFIED {
return ""
}
return formatStageState(info.GetState())
}
func formatStageState(s instance.StageState) string {
if s == instance.StageState_STAGE_STATE_UNSPECIFIED {
return ""
}
return strings.ToLower(strings.TrimPrefix(s.String(), "STAGE_STATE_"))
}