...
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15 package gcrane
16
17 import (
18 "context"
19 "net/http"
20 "runtime"
21
22 "github.com/google/go-containerregistry/pkg/authn"
23 "github.com/google/go-containerregistry/pkg/crane"
24 "github.com/google/go-containerregistry/pkg/v1/google"
25 "github.com/google/go-containerregistry/pkg/v1/remote"
26 )
27
28
29 type Option func(*options)
30
31 type options struct {
32 jobs int
33 remote []remote.Option
34 google []google.Option
35 crane []crane.Option
36 }
37
38 func makeOptions(opts ...Option) *options {
39 o := &options{
40 jobs: runtime.GOMAXPROCS(0),
41 remote: []remote.Option{
42 remote.WithAuthFromKeychain(Keychain),
43 },
44 google: []google.Option{
45 google.WithAuthFromKeychain(Keychain),
46 },
47 crane: []crane.Option{
48 crane.WithAuthFromKeychain(Keychain),
49 },
50 }
51
52 for _, option := range opts {
53 option(o)
54 }
55
56 return o
57 }
58
59
60
61
62 func WithJobs(jobs int) Option {
63 return func(o *options) {
64 o.jobs = jobs
65 }
66 }
67
68
69
70 func WithTransport(t http.RoundTripper) Option {
71 return func(o *options) {
72 o.remote = append(o.remote, remote.WithTransport(t))
73 o.google = append(o.google, google.WithTransport(t))
74 o.crane = append(o.crane, crane.WithTransport(t))
75 }
76 }
77
78
79
80 func WithUserAgent(ua string) Option {
81 return func(o *options) {
82 o.remote = append(o.remote, remote.WithUserAgent(ua))
83 o.google = append(o.google, google.WithUserAgent(ua))
84 o.crane = append(o.crane, crane.WithUserAgent(ua))
85 }
86 }
87
88
89 func WithContext(ctx context.Context) Option {
90 return func(o *options) {
91 o.remote = append(o.remote, remote.WithContext(ctx))
92 o.google = append(o.google, google.WithContext(ctx))
93 o.crane = append(o.crane, crane.WithContext(ctx))
94 }
95 }
96
97
98
99
100
101
102 func WithKeychain(keys authn.Keychain) Option {
103 return func(o *options) {
104
105 o.remote[0] = remote.WithAuthFromKeychain(keys)
106 o.google[0] = google.WithAuthFromKeychain(keys)
107 o.crane[0] = crane.WithAuthFromKeychain(keys)
108 }
109 }
110
111
112
113
114
115 func WithAuth(auth authn.Authenticator) Option {
116 return func(o *options) {
117
118 o.remote[0] = remote.WithAuth(auth)
119 o.google[0] = google.WithAuth(auth)
120 o.crane[0] = crane.WithAuth(auth)
121 }
122 }
123
View as plain text