-
Notifications
You must be signed in to change notification settings - Fork 64
/
print.go
361 lines (305 loc) · 10.8 KB
/
print.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
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
// SPDX-License-Identifier: BSD-3-Clause
// Copyright (c) 2023, Unikraft GmbH and The KraftKit Authors.
// Licensed under the BSD-3-Clause License (the "License").
// You may not use this file except in compliance with the License.
package utils
import (
"context"
"fmt"
"strings"
"time"
"github.com/charmbracelet/lipgloss"
"github.com/dustin/go-humanize"
"kraftkit.sh/internal/tableprinter"
"kraftkit.sh/iostreams"
"kraftkit.sh/log"
kraftcloudinstances "sdk.kraft.cloud/instances"
kraftcloudservices "sdk.kraft.cloud/services"
kraftcloudusers "sdk.kraft.cloud/users"
kraftcloudvolumes "sdk.kraft.cloud/volumes"
)
var (
textRed = lipgloss.NewStyle().Foreground(lipgloss.Color("9")).Render
textGreen = lipgloss.NewStyle().Foreground(lipgloss.Color("10")).Render
textYellow = lipgloss.NewStyle().Foreground(lipgloss.Color("11")).Render
textGray = lipgloss.NewStyle().Foreground(lipgloss.Color("245")).Render
)
// PrintInstances pretty-prints the provided set of instances or returns
// an error if unable to send to stdout via the provided context.
func PrintInstances(ctx context.Context, format string, instances ...kraftcloudinstances.Instance) error {
err := iostreams.G(ctx).StartPager()
if err != nil {
log.G(ctx).Errorf("error starting pager: %v", err)
}
defer iostreams.G(ctx).StopPager()
cs := iostreams.G(ctx).ColorScheme()
table, err := tableprinter.NewTablePrinter(ctx,
tableprinter.WithMaxWidth(iostreams.G(ctx).TerminalWidth()),
tableprinter.WithOutputFormatFromString(format),
)
if err != nil {
return err
}
// Header row
if format != "table" {
table.AddField("UUID", cs.Bold)
}
table.AddField("NAME", cs.Bold)
table.AddField("FQDN", cs.Bold)
if format != "table" {
table.AddField("PRIVATE IP", cs.Bold)
}
table.AddField("STATE", cs.Bold)
table.AddField("CREATED AT", cs.Bold)
table.AddField("IMAGE", cs.Bold)
table.AddField("MEMORY", cs.Bold)
table.AddField("ARGS", cs.Bold)
if format != "table" {
table.AddField("VOLUMES", cs.Bold)
table.AddField("SERVICE GROUP", cs.Bold)
}
table.AddField("BOOT TIME", cs.Bold)
table.EndRow()
for _, instance := range instances {
var createdAt string
if len(instance.CreatedAt) > 0 {
createdTime, err := time.Parse(time.RFC3339, instance.CreatedAt)
if err != nil {
return fmt.Errorf("could not parse time for '%s': %w", instance.UUID, err)
}
createdAt = humanize.Time(createdTime)
}
if format != "table" {
table.AddField(instance.UUID, nil)
}
table.AddField(instance.Name, nil)
table.AddField(instance.FQDN, nil)
if format != "table" {
table.AddField(instance.PrivateIP, nil)
}
table.AddField(string(instance.State), nil)
table.AddField(createdAt, nil)
table.AddField(instance.Image, nil)
table.AddField(humanize.IBytes(uint64(instance.MemoryMB)*humanize.MiByte), nil)
table.AddField(strings.Join(instance.Args, " "), nil)
if format != "table" {
vols := make([]string, len(instance.Volumes))
for i, vol := range instance.Volumes {
vols[i] = fmt.Sprintf("%s:%s", vol.Name, vol.At)
if vol.ReadOnly {
vols[i] += ":ro"
}
}
table.AddField(strings.Join(vols, ", "), nil)
table.AddField(instance.ServiceGroup.UUID, nil)
}
table.AddField(fmt.Sprintf("%dus", instance.BootTimeUS), nil)
table.EndRow()
}
return table.Render(iostreams.G(ctx).Out)
}
// PrintVolumes pretty-prints the provided set of volumes or returns
// an error if unable to send to stdout via the provided context.
func PrintVolumes(ctx context.Context, format string, volumes ...kraftcloudvolumes.Volume) error {
err := iostreams.G(ctx).StartPager()
if err != nil {
log.G(ctx).Errorf("error starting pager: %v", err)
}
defer iostreams.G(ctx).StopPager()
cs := iostreams.G(ctx).ColorScheme()
table, err := tableprinter.NewTablePrinter(ctx,
tableprinter.WithMaxWidth(iostreams.G(ctx).TerminalWidth()),
tableprinter.WithOutputFormatFromString(format),
)
if err != nil {
return err
}
// Header row
if format != "table" {
table.AddField("UUID", cs.Bold)
}
table.AddField("NAME", cs.Bold)
table.AddField("CREATED AT", cs.Bold)
table.AddField("SIZE", cs.Bold)
table.AddField("ATTACHED TO", cs.Bold)
table.AddField("STATE", cs.Bold)
table.AddField("PERSISTENT", cs.Bold)
table.EndRow()
for _, volume := range volumes {
var createdAt string
if len(volume.CreatedAt) > 0 {
createdTime, err := time.Parse(time.RFC3339, volume.CreatedAt)
if err != nil {
return fmt.Errorf("could not parse time for '%s': %w", volume.UUID, err)
}
createdAt = humanize.Time(createdTime)
}
if format != "table" {
table.AddField(volume.UUID, nil)
}
table.AddField(volume.Name, nil)
table.AddField(createdAt, nil)
table.AddField(humanize.IBytes(uint64(volume.SizeMB)*humanize.MiByte), nil)
var attachedTo []string
for _, instance := range volume.AttachedTo {
attachedTo = append(attachedTo, instance.Name)
}
table.AddField(strings.Join(attachedTo, ","), nil)
table.AddField(string(volume.State), nil)
table.AddField(fmt.Sprintf("%t", volume.Persistent), nil)
table.EndRow()
}
return table.Render(iostreams.G(ctx).Out)
}
// PrintServiceGroups pretty-prints the provided set of service groups or returns
// an error if unable to send to stdout via the provided context.
func PrintServiceGroups(ctx context.Context, format string, serviceGroups ...kraftcloudservices.ServiceGroup) error {
err := iostreams.G(ctx).StartPager()
if err != nil {
log.G(ctx).Errorf("error starting pager: %v", err)
}
defer iostreams.G(ctx).StopPager()
cs := iostreams.G(ctx).ColorScheme()
table, err := tableprinter.NewTablePrinter(ctx,
tableprinter.WithMaxWidth(iostreams.G(ctx).TerminalWidth()),
tableprinter.WithOutputFormatFromString(format),
)
if err != nil {
return err
}
// Header row
if format != "table" {
table.AddField("UUID", cs.Bold)
}
table.AddField("NAME", cs.Bold)
table.AddField("FQDN", cs.Bold)
table.AddField("SERVICES", cs.Bold)
table.AddField("INSTANCES", cs.Bold)
table.AddField("CREATED AT", cs.Bold)
table.AddField("PERSISTENT", cs.Bold)
table.EndRow()
for _, sg := range serviceGroups {
if format != "table" {
table.AddField(sg.UUID, nil)
}
table.AddField(sg.Name, nil)
table.AddField(sg.FQDN, nil)
var services []string
for _, service := range sg.Services {
var handlers []string
for _, handler := range service.Handlers {
handlers = append(handlers, string(handler))
}
services = append(services, fmt.Sprintf("%d:%d/%s", service.Port, service.DestinationPort, strings.Join(handlers, "+")))
}
table.AddField(strings.Join(services, " "), nil)
table.AddField(strings.Join(sg.Instances, " "), nil)
var createdAt string
if len(sg.CreatedAt) > 0 {
createdTime, err := time.Parse(time.RFC3339, sg.CreatedAt)
if err != nil {
return fmt.Errorf("could not parse time for '%s': %w", sg.UUID, err)
}
createdAt = humanize.Time(createdTime)
}
table.AddField(createdAt, nil)
table.AddField(fmt.Sprintf("%v", sg.Persistent), nil)
table.EndRow()
}
return table.Render(iostreams.G(ctx).Out)
}
// PrintQuotas pretty-prints the provided set of user quotas or returns
// an error if unable to send to stdout via the provided context.
func PrintQuotas(ctx context.Context, format string, quotas ...kraftcloudusers.Quotas) error {
if err := iostreams.G(ctx).StartPager(); err != nil {
log.G(ctx).Errorf("error starting pager: %v", err)
}
defer iostreams.G(ctx).StopPager()
cs := iostreams.G(ctx).ColorScheme()
table, err := tableprinter.NewTablePrinter(ctx,
tableprinter.WithMaxWidth(iostreams.G(ctx).TerminalWidth()),
tableprinter.WithOutputFormatFromString(format),
)
if err != nil {
return err
}
if format != "table" {
table.AddField("UUID", cs.Bold)
}
table.AddField("NAME", cs.Bold)
table.AddField("LIVE INSTANCES", cs.Bold)
table.AddField("TOTAL INSTANCES", cs.Bold)
table.AddField("MAX TOTAL INSTANCES", cs.Bold)
table.AddField("LIVE MEMORY", cs.Bold)
table.AddField("SERVICE GROUPS", cs.Bold)
table.AddField("SERVICES", cs.Bold)
table.AddField("TOTAL VOLUME SIZE", cs.Bold)
table.AddField("VOLUMES", cs.Bold)
table.EndRow()
for _, quota := range quotas {
if format != "table" {
table.AddField(quota.UUID, nil)
}
table.AddField(quota.Name, nil)
table.AddField(fmt.Sprintf("%d/%d", quota.Used.LiveInstances, quota.Hard.LiveInstances), nil)
table.AddField(fmt.Sprintf("%d/%d", quota.Used.Instances, quota.Hard.Instances), nil)
table.AddField(fmt.Sprintf("%s/%s",
humanize.IBytes(uint64(quota.Used.LiveMemoryMb)*humanize.MiByte),
humanize.IBytes(uint64(quota.Hard.LiveMemoryMb)*humanize.MiByte),
), nil)
table.AddField(fmt.Sprintf("%d/%d", quota.Used.ServiceGroups, quota.Hard.ServiceGroups), nil)
table.AddField(fmt.Sprintf("%d/%d", quota.Used.Services, quota.Hard.Services), nil)
table.AddField(fmt.Sprintf("%s/%s",
humanize.IBytes(uint64(quota.Used.TotalVolumeMb)*humanize.MiByte),
humanize.IBytes(uint64(quota.Hard.TotalVolumeMb)*humanize.MiByte),
), nil)
table.AddField(fmt.Sprintf("%d/%d", quota.Used.Volumes, quota.Hard.Volumes), nil)
table.EndRow()
}
return table.Render(iostreams.G(ctx).Out)
}
// PrettyPrintInstance outputs a single instance and information about it.
func PrettyPrintInstance(ctx context.Context, instance *kraftcloudinstances.Instance, autoStart bool) {
out := iostreams.G(ctx).Out
fqdn := instance.FQDN
if len(fqdn) > 0 {
for _, port := range instance.ServiceGroup.Services {
if port.Port == 443 {
fqdn = "https://" + fqdn
break
}
}
}
var color func(...string) string
if instance.State == "running" || instance.State == "starting" {
color = textGreen
} else if instance.State == "stopped" {
color = textRed
} else {
color = textYellow
}
fmt.Fprintf(out, "\n%s%s%s %s\n", textGray("["), color("●"), textGray("]"), instance.UUID)
fmt.Fprintf(out, " %s: %s\n", textGray("name"), instance.Name)
fmt.Fprintf(out, " %s: %s\n", textGray("state"), color(instance.State))
if len(fqdn) > 0 {
if strings.HasPrefix(fqdn, "https://") {
fmt.Fprintf(out, " %s: %s\n", textGray("url"), fqdn)
} else {
fmt.Fprintf(out, " %s: %s\n", textGray("fqdn"), fqdn)
}
}
fmt.Fprintf(out, " %s: %s\n", textGray("image"), instance.Image)
fmt.Fprintf(out, " %s: %.2f ms\n", textGray("boot time"), float64(instance.BootTimeUS)/1000)
fmt.Fprintf(out, " %s: %d MiB\n", textGray("memory"), instance.MemoryMB)
if len(instance.ServiceGroup.Name) > 0 {
fmt.Fprintf(out, " %s: %s\n", textGray("service group"), instance.ServiceGroup.Name)
}
if len(instance.Args) > 0 {
fmt.Fprintf(out, " %s: %s\n", textGray("args"), strings.Join(instance.Args, " "))
}
fmt.Fprintf(out, "\n")
if instance.State != "running" && instance.State != "starting" && autoStart {
log.G(ctx).Info("it looks like the instance did not come online, to view logs run:")
fmt.Fprintf(out, "\n kraft cloud instance logs %s\n\n", instance.Name)
}
}