...
1
16
17 package getter
18
19 import (
20 "bytes"
21 "net/http"
22 "time"
23
24 "github.com/pkg/errors"
25
26 "helm.sh/helm/v3/pkg/cli"
27 "helm.sh/helm/v3/pkg/registry"
28 )
29
30
31
32
33 type options struct {
34 url string
35 certFile string
36 keyFile string
37 caFile string
38 unTar bool
39 insecureSkipVerifyTLS bool
40 plainHTTP bool
41 username string
42 password string
43 passCredentialsAll bool
44 userAgent string
45 version string
46 registryClient *registry.Client
47 timeout time.Duration
48 transport *http.Transport
49 }
50
51
52
53 type Option func(*options)
54
55
56
57 func WithURL(url string) Option {
58 return func(opts *options) {
59 opts.url = url
60 }
61 }
62
63
64 func WithBasicAuth(username, password string) Option {
65 return func(opts *options) {
66 opts.username = username
67 opts.password = password
68 }
69 }
70
71 func WithPassCredentialsAll(pass bool) Option {
72 return func(opts *options) {
73 opts.passCredentialsAll = pass
74 }
75 }
76
77
78 func WithUserAgent(userAgent string) Option {
79 return func(opts *options) {
80 opts.userAgent = userAgent
81 }
82 }
83
84
85 func WithInsecureSkipVerifyTLS(insecureSkipVerifyTLS bool) Option {
86 return func(opts *options) {
87 opts.insecureSkipVerifyTLS = insecureSkipVerifyTLS
88 }
89 }
90
91
92 func WithTLSClientConfig(certFile, keyFile, caFile string) Option {
93 return func(opts *options) {
94 opts.certFile = certFile
95 opts.keyFile = keyFile
96 opts.caFile = caFile
97 }
98 }
99
100 func WithPlainHTTP(plainHTTP bool) Option {
101 return func(opts *options) {
102 opts.plainHTTP = plainHTTP
103 }
104 }
105
106
107 func WithTimeout(timeout time.Duration) Option {
108 return func(opts *options) {
109 opts.timeout = timeout
110 }
111 }
112
113 func WithTagName(tagname string) Option {
114 return func(opts *options) {
115 opts.version = tagname
116 }
117 }
118
119 func WithRegistryClient(client *registry.Client) Option {
120 return func(opts *options) {
121 opts.registryClient = client
122 }
123 }
124
125 func WithUntar() Option {
126 return func(opts *options) {
127 opts.unTar = true
128 }
129 }
130
131
132 func WithTransport(transport *http.Transport) Option {
133 return func(opts *options) {
134 opts.transport = transport
135 }
136 }
137
138
139 type Getter interface {
140
141 Get(url string, options ...Option) (*bytes.Buffer, error)
142 }
143
144
145
146 type Constructor func(options ...Option) (Getter, error)
147
148
149
150
151
152 type Provider struct {
153 Schemes []string
154 New Constructor
155 }
156
157
158 func (p Provider) Provides(scheme string) bool {
159 for _, i := range p.Schemes {
160 if i == scheme {
161 return true
162 }
163 }
164 return false
165 }
166
167
168 type Providers []Provider
169
170
171
172
173 func (p Providers) ByScheme(scheme string) (Getter, error) {
174 for _, pp := range p {
175 if pp.Provides(scheme) {
176 return pp.New()
177 }
178 }
179 return nil, errors.Errorf("scheme %q not supported", scheme)
180 }
181
182 const (
183
184
185
186 DefaultHTTPTimeout = 120
187 )
188
189 var defaultOptions = []Option{WithTimeout(time.Second * DefaultHTTPTimeout)}
190
191 var httpProvider = Provider{
192 Schemes: []string{"http", "https"},
193 New: func(options ...Option) (Getter, error) {
194 options = append(options, defaultOptions...)
195 return NewHTTPGetter(options...)
196 },
197 }
198
199 var ociProvider = Provider{
200 Schemes: []string{registry.OCIScheme},
201 New: NewOCIGetter,
202 }
203
204
205
206
207 func All(settings *cli.EnvSettings) Providers {
208 result := Providers{httpProvider, ociProvider}
209 pluginDownloaders, _ := collectPlugins(settings)
210 result = append(result, pluginDownloaders...)
211 return result
212 }
213
View as plain text