-
Notifications
You must be signed in to change notification settings - Fork 14
/
fake_relocator.go
69 lines (57 loc) · 1.46 KB
/
fake_relocator.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
// Copyright 2020-Present VMware, Inc.
// SPDX-License-Identifier: Apache-2.0
package fakes
import (
"fmt"
"io"
"io/ioutil"
"github.com/google/go-containerregistry/pkg/authn"
"github.com/google/go-containerregistry/pkg/name"
v1 "github.com/google/go-containerregistry/pkg/v1"
)
type Relocator struct {
skip bool
calls []struct {
Keychain authn.Keychain
Image v1.Image
Dest string
}
writer io.Writer
}
func (r *Relocator) Relocate(keychain authn.Keychain, image v1.Image, dest string) (string, error) {
r.calls = append(r.calls, struct {
Keychain authn.Keychain
Image v1.Image
Dest string
}{keychain, image, dest})
digest, err := image.Digest()
if err != nil {
return "", err
}
sha := digest.String()
destRef, err := name.ParseReference(dest)
if err != nil {
return "", err
}
refDigestStr := fmt.Sprintf("%s/%s@%s", destRef.Context().RegistryStr(), destRef.Context().RepositoryStr(), sha)
var message string
if r.skip {
message = fmt.Sprintf("\tSkipping '%s'\n", refDigestStr)
} else {
message = fmt.Sprintf("\tUploading '%s'\n", refDigestStr)
}
if r.writer == nil {
r.writer = ioutil.Discard
}
_, err = r.writer.Write([]byte(message))
return refDigestStr, err
}
func (r *Relocator) CallCount() int {
return len(r.calls)
}
func (r *Relocator) SetSkip(skip bool) {
r.skip = skip
}
func (r *Relocator) RelocateCall(i int) (authn.Keychain, v1.Image, string) {
return r.calls[i].Keychain, r.calls[i].Image, r.calls[i].Dest
}