...
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15 package crane
16
17 import (
18 "context"
19 "crypto/tls"
20 "net/http"
21
22 "github.com/google/go-containerregistry/pkg/authn"
23 "github.com/google/go-containerregistry/pkg/name"
24 v1 "github.com/google/go-containerregistry/pkg/v1"
25 "github.com/google/go-containerregistry/pkg/v1/remote"
26 )
27
28
29 type Options struct {
30 Name []name.Option
31 Remote []remote.Option
32 Platform *v1.Platform
33 Keychain authn.Keychain
34 Transport http.RoundTripper
35
36 auth authn.Authenticator
37 insecure bool
38 jobs int
39 noclobber bool
40 ctx context.Context
41 }
42
43
44
45
46
47 func GetOptions(opts ...Option) Options {
48 return makeOptions(opts...)
49 }
50
51 func makeOptions(opts ...Option) Options {
52 opt := Options{
53 Remote: []remote.Option{
54 remote.WithAuthFromKeychain(authn.DefaultKeychain),
55 },
56 Keychain: authn.DefaultKeychain,
57 jobs: 4,
58 ctx: context.Background(),
59 }
60
61 for _, o := range opts {
62 o(&opt)
63 }
64
65
66
67 if opt.insecure && opt.Transport == nil {
68 transport := remote.DefaultTransport.(*http.Transport).Clone()
69 transport.TLSClientConfig = &tls.Config{
70 InsecureSkipVerify: true,
71 }
72
73 WithTransport(transport)(&opt)
74 } else if opt.Transport == nil {
75 opt.Transport = remote.DefaultTransport
76 }
77
78 return opt
79 }
80
81
82 type Option func(*Options)
83
84
85
86
87 func WithTransport(t http.RoundTripper) Option {
88 return func(o *Options) {
89 o.Remote = append(o.Remote, remote.WithTransport(t))
90 o.Transport = t
91 }
92 }
93
94
95
96
97 func Insecure(o *Options) {
98 o.Name = append(o.Name, name.Insecure)
99 o.insecure = true
100 }
101
102
103 func WithPlatform(platform *v1.Platform) Option {
104 return func(o *Options) {
105 if platform != nil {
106 o.Remote = append(o.Remote, remote.WithPlatform(*platform))
107 }
108 o.Platform = platform
109 }
110 }
111
112
113
114
115
116
117 func WithAuthFromKeychain(keys authn.Keychain) Option {
118 return func(o *Options) {
119
120 o.Remote[0] = remote.WithAuthFromKeychain(keys)
121 o.Keychain = keys
122 }
123 }
124
125
126
127
128
129 func WithAuth(auth authn.Authenticator) Option {
130 return func(o *Options) {
131
132 o.Remote[0] = remote.WithAuth(auth)
133 o.auth = auth
134 }
135 }
136
137
138
139 func WithUserAgent(ua string) Option {
140 return func(o *Options) {
141 o.Remote = append(o.Remote, remote.WithUserAgent(ua))
142 }
143 }
144
145
146
147 func WithNondistributable() Option {
148 return func(o *Options) {
149 o.Remote = append(o.Remote, remote.WithNondistributable)
150 }
151 }
152
153
154 func WithContext(ctx context.Context) Option {
155 return func(o *Options) {
156 o.ctx = ctx
157 o.Remote = append(o.Remote, remote.WithContext(ctx))
158 }
159 }
160
161
162
163
164 func WithJobs(jobs int) Option {
165 return func(o *Options) {
166 if jobs > 0 {
167 o.jobs = jobs
168 }
169 o.Remote = append(o.Remote, remote.WithJobs(o.jobs))
170 }
171 }
172
173
174 func WithNoClobber(noclobber bool) Option {
175 return func(o *Options) {
176 o.noclobber = noclobber
177 }
178 }
179
View as plain text