-
Notifications
You must be signed in to change notification settings - Fork 90
/
backup.go
55 lines (45 loc) · 1.41 KB
/
backup.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
package print
import (
"encoding/json"
"fmt"
"time"
velerov1 "github.com/vmware-tanzu/velero/pkg/apis/velero/v1"
)
func Backups(backups []velerov1.Backup, format string) {
switch format {
case "json":
printBackupsJSON(backups)
default:
printBackupsTable(backups)
}
}
func printBackupsJSON(backups []velerov1.Backup) {
str, _ := json.MarshalIndent(backups, "", " ")
fmt.Println(string(str))
}
func printBackupsTable(backups []velerov1.Backup) {
w := NewTabWriter()
defer w.Flush()
fmtColumns := "%s\t%s\t%s\t%s\t%s\t%s\t%s\n"
fmt.Fprintf(w, fmtColumns, "NAME", "STATUS", "ERRORS", "WARNINGS", "STARTED", "COMPLETED", "EXPIRES")
for _, b := range backups {
expiresAt := ""
if b.Status.Expiration != nil {
expiresAtDuration := b.Status.Expiration.Time.Sub(time.Now())
expiresAt = fmt.Sprintf("%dd", uint64(expiresAtDuration.Hours()/24))
}
var startedAt *time.Time
if b.Status.StartTimestamp != nil && !b.Status.StartTimestamp.Time.IsZero() {
startedAt = &b.Status.StartTimestamp.Time
}
var completedAt *time.Time
if b.Status.CompletionTimestamp != nil && !b.Status.CompletionTimestamp.Time.IsZero() {
completedAt = &b.Status.CompletionTimestamp.Time
}
phase := b.Status.Phase
if phase == "" {
phase = "New"
}
fmt.Fprintf(w, fmtColumns, b.ObjectMeta.Name, phase, fmt.Sprintf("%d", b.Status.Errors), fmt.Sprintf("%d", b.Status.Warnings), startedAt, completedAt, expiresAt)
}
}