-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathutils.go
116 lines (97 loc) · 2.46 KB
/
utils.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
package main
import (
"context"
"io"
"reflect"
"sort"
"strings"
"time"
"github.com/minio/minio-go/v7"
progressbar "github.com/schollz/progressbar/v3"
)
func check[T any](v T, err error) T {
if err != nil {
panic(err)
}
return v
}
func check0(err error) {
if err != nil {
panic(err)
}
}
func dismiss[T any](v T, err error) T {
return v
}
func AllFieldsDefined(v interface{}) bool {
value := reflect.ValueOf(v)
if value.Kind() != reflect.Struct {
return false
}
numFields := value.NumField()
for i := 0; i < numFields; i++ {
fieldValue := value.Field(i)
if reflect.DeepEqual(fieldValue.Interface(), reflect.Zero(fieldValue.Type()).Interface()) {
return false
}
}
return true
}
func copyProgress(writer io.Writer, reader ReaderWithLength, label string) (int64, error) {
bar := progressbar.DefaultBytes(
int64(reader.Len()),
label,
)
defer bar.Close()
return io.Copy(io.MultiWriter(writer, bar), reader)
}
func copyProgressN(writer io.Writer, reader io.Reader, n int64, label string) (int64, error) {
bar := progressbar.DefaultBytes(n, label)
defer bar.Close()
return io.CopyN(io.MultiWriter(writer, bar), reader, n)
}
func contains[T comparable](s []T, e T) bool {
for _, a := range s {
if a == e {
return true
}
}
return false
}
func vennDiff[T comparable](left, right []T) (keysInLeft, keysInBoth, keysInRight []T) {
for _, leftK := range left {
inBoth := contains[T](right, leftK)
if inBoth {
keysInBoth = append(keysInBoth, leftK)
} else {
keysInLeft = append(keysInLeft, leftK)
}
}
for _, rightK := range right {
inLeft := contains[T](left, rightK)
if !inLeft {
keysInRight = append(keysInRight, rightK)
}
}
return
}
func getListOfArchives(config *BackupConfig) []time.Time {
archivesChan := config.MinioClient.ListObjects(context.Background(), config.Config.S3.Bucket, minio.ListObjectsOptions{
// Recursive: true,
})
var archives []time.Time
for archive := range archivesChan {
archives = append(archives, check(time.Parse(time.RFC3339, archive.Key[:strings.Index(archive.Key, "/")])))
}
sort.SliceStable(archives, func(i, j int) bool {
return archives[i].Before(archives[j])
})
return archives
}
func getObjectsFromArchives(config *BackupConfig, archive time.Time) <-chan minio.ObjectInfo {
archiveName := archive.Format(time.RFC3339)
return config.MinioClient.ListObjects(context.Background(), config.Config.S3.Bucket, minio.ListObjectsOptions{
Prefix: archiveName,
Recursive: true,
})
}