-
Notifications
You must be signed in to change notification settings - Fork 224
/
list.go
123 lines (104 loc) · 3.37 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
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
package checks
import (
"context"
"fmt"
"sort"
"strings"
"github.com/superfly/flyctl/api"
"github.com/superfly/flyctl/client"
"github.com/superfly/flyctl/flaps"
"github.com/superfly/flyctl/helpers"
"github.com/superfly/flyctl/internal/appconfig"
"github.com/superfly/flyctl/internal/config"
"github.com/superfly/flyctl/internal/flag"
"github.com/superfly/flyctl/internal/format"
"github.com/superfly/flyctl/internal/render"
"github.com/superfly/flyctl/iostreams"
)
func runAppCheckList(ctx context.Context) error {
web := client.FromContext(ctx).API()
appName := appconfig.NameFromContext(ctx)
app, err := web.GetAppCompact(ctx, appName)
if err != nil {
return fmt.Errorf("failed to get app: %w", err)
}
if app.PlatformVersion == "machines" {
return runMachinesAppCheckList(ctx, app)
}
return runNomadAppCheckList(ctx)
}
func runMachinesAppCheckList(ctx context.Context, app *api.AppCompact) error {
out := iostreams.FromContext(ctx).Out
nameFilter := flag.GetString(ctx, "check-name")
flapsClient, err := flaps.New(ctx, app)
if err != nil {
return err
}
machines, err := flapsClient.ListActive(ctx)
if err != nil {
return err
}
sort.Slice(machines, func(i, j int) bool {
return machines[i].ID < machines[j].ID
})
if config.FromContext(ctx).JSONOutput {
checks := map[string][]api.MachineCheckStatus{}
for _, machine := range machines {
checks[machine.ID] = make([]api.MachineCheckStatus, len(machine.Checks))
for i, check := range machine.Checks {
checks[machine.ID][i] = *check
}
}
return render.JSON(out, checks)
}
fmt.Fprintf(out, "Health Checks for %s\n", app.Name)
table := helpers.MakeSimpleTable(out, []string{"Name", "Status", "Machine", "Last Updated", "Output"})
table.SetRowLine(true)
for _, machine := range machines {
sort.Slice(machine.Checks, func(i, j int) bool {
return machine.Checks[i].Name < machine.Checks[j].Name
})
for _, check := range machine.Checks {
if nameFilter != "" && nameFilter != check.Name {
continue
}
table.Append([]string{check.Name, string(check.Status), machine.ID, format.RelativeTime(*check.UpdatedAt), check.Output})
}
}
table.Render()
return nil
}
func runNomadAppCheckList(ctx context.Context) error {
appName := appconfig.NameFromContext(ctx)
out := iostreams.FromContext(ctx).Out
web := client.FromContext(ctx).API()
var nameFilter *string
if val := flag.GetString(ctx, "check-name"); val != "" {
nameFilter = api.StringPointer(val)
}
checks, err := web.GetAppHealthChecks(ctx, appName, nameFilter, nil, api.BoolPointer(false))
if err != nil {
return err
}
if config.FromContext(ctx).JSONOutput {
return render.JSON(out, checks)
}
fmt.Fprintf(out, "Health Checks for %s\n", appName)
table := helpers.MakeSimpleTable(out, []string{"Name", "Status", "Allocation", "Region", "Type", "Last Updated", "Output"})
for _, check := range checks {
formattedOutput := formatOutput(check.Output)
table.Append([]string{check.Name, check.Status, check.Allocation.IDShort, check.Allocation.Region, check.Type, format.RelativeTime(check.UpdatedAt), formattedOutput})
}
table.Render()
return nil
}
func formatOutput(output string) string {
var newstr string
output = strings.ReplaceAll(output, "\n", "")
output = strings.ReplaceAll(output, "] ", "]")
v := strings.Split(output, "[✓]")
for _, attr := range v {
newstr += fmt.Sprintf("%s[✓]\n\n", attr)
}
return newstr
}