-
Notifications
You must be signed in to change notification settings - Fork 1
/
source_images.go
85 lines (73 loc) · 2.31 KB
/
source_images.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
package batch
import (
"github.com/rokka-io/rokka-go/rokka"
"gopkg.in/cheggaaa/pb.v1"
)
// SourceImagesReader reads images from rokka on an organization.
type SourceImagesReader struct {
Organization string
}
// Read uses the search API to paginate through all images.
func (sir *SourceImagesReader) Read(client *rokka.Client, images chan string, bar *pb.ProgressBar) error {
cursor := ""
for {
opt := rokka.ListSourceImagesOptions{
Offset: cursor,
}
res, err := client.ListSourceImages(sir.Organization, opt)
if err != nil {
return err
}
bar.Total = int64(res.Total)
for _, element := range res.Items {
images <- element.Hash
}
if res.Cursor == "" || cursor == res.Cursor || len(res.Items) == 0 {
return nil
}
cursor = res.Cursor
}
}
// Count fetches one image in order to get the Total amount of images available.
func (sir *SourceImagesReader) Count(client *rokka.Client) (int, error) {
listSourceImagesOptions := rokka.ListSourceImagesOptions{
Limit: 1,
}
res, err := client.ListSourceImages(sir.Organization, listSourceImagesOptions)
if err != nil {
return 0, err
}
return res.Total, nil
}
// CopyAllSourceImagesWriter uses the copy all API to transfer images from one organization to a destination organization.
type CopyAllSourceImagesWriter struct {
SourceOrganization string
DestinationOrganization string
}
func (cas *CopyAllSourceImagesWriter) Write(client *rokka.Client, images []string) OperationResult {
OK, notOK, err := client.CopySourceImages(cas.SourceOrganization, images, cas.DestinationOrganization)
return OperationResult{OK: OK, NotOK: notOK, Error: err}
}
// DeleteAllSourceImagesWriter deletes source images of an organization.
type DeleteAllSourceImagesWriter struct {
Organization string
}
func (das *DeleteAllSourceImagesWriter) Write(client *rokka.Client, images []string) OperationResult {
var lastErr error
OK := 0
notOK := 0
for _, hash := range images {
lastErr = client.DeleteSourceImage(das.Organization, hash)
if lastErr != nil {
notOK++
} else {
OK++
}
}
return OperationResult{OK: OK, NotOK: notOK, Error: lastErr}
}
// NoopWriter does not do anything. It is used for the dry run.
type NoopWriter struct{}
func (nw *NoopWriter) Write(client *rokka.Client, images []string) OperationResult {
return OperationResult{OK: len(images)}
}