github.com/diafour/helm@v3.0.0-beta.3+incompatible/pkg/getter/getter.go (about) 1 /* 2 Copyright The Helm Authors. 3 4 Licensed under the Apache License, Version 2.0 (the "License"); 5 you may not use this file except in compliance with the License. 6 You may obtain a copy of the License at 7 8 http://www.apache.org/licenses/LICENSE-2.0 9 10 Unless required by applicable law or agreed to in writing, software 11 distributed under the License is distributed on an "AS IS" BASIS, 12 WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 See the License for the specific language governing permissions and 14 limitations under the License. 15 */ 16 17 package getter 18 19 import ( 20 "bytes" 21 22 "github.com/pkg/errors" 23 24 "helm.sh/helm/pkg/cli" 25 ) 26 27 // options are generic parameters to be provided to the getter during instantiation. 28 // 29 // Getters may or may not ignore these parameters as they are passed in. 30 type options struct { 31 url string 32 certFile string 33 keyFile string 34 caFile string 35 username string 36 password string 37 userAgent string 38 } 39 40 // Option allows specifying various settings configurable by the user for overriding the defaults 41 // used when performing Get operations with the Getter. 42 type Option func(*options) 43 44 // WithURL informs the getter the server name that will be used when fetching objects. Used in conjunction with 45 // WithTLSClientConfig to set the TLSClientConfig's server name. 46 func WithURL(url string) Option { 47 return func(opts *options) { 48 opts.url = url 49 } 50 } 51 52 // WithBasicAuth sets the request's Authorization header to use the provided credentials 53 func WithBasicAuth(username, password string) Option { 54 return func(opts *options) { 55 opts.username = username 56 opts.password = password 57 } 58 } 59 60 // WithUserAgent sets the request's User-Agent header to use the provided agent name. 61 func WithUserAgent(userAgent string) Option { 62 return func(opts *options) { 63 opts.userAgent = userAgent 64 } 65 } 66 67 // WithTLSClientConfig sets the client client auth with the provided credentials. 68 func WithTLSClientConfig(certFile, keyFile, caFile string) Option { 69 return func(opts *options) { 70 opts.certFile = certFile 71 opts.keyFile = keyFile 72 opts.caFile = caFile 73 } 74 } 75 76 // Getter is an interface to support GET to the specified URL. 77 type Getter interface { 78 // Get file content by url string 79 Get(url string, options ...Option) (*bytes.Buffer, error) 80 } 81 82 // Constructor is the function for every getter which creates a specific instance 83 // according to the configuration 84 type Constructor func(options ...Option) (Getter, error) 85 86 // Provider represents any getter and the schemes that it supports. 87 // 88 // For example, an HTTP provider may provide one getter that handles both 89 // 'http' and 'https' schemes. 90 type Provider struct { 91 Schemes []string 92 New Constructor 93 } 94 95 // Provides returns true if the given scheme is supported by this Provider. 96 func (p Provider) Provides(scheme string) bool { 97 for _, i := range p.Schemes { 98 if i == scheme { 99 return true 100 } 101 } 102 return false 103 } 104 105 // Providers is a collection of Provider objects. 106 type Providers []Provider 107 108 // ByScheme returns a Provider that handles the given scheme. 109 // 110 // If no provider handles this scheme, this will return an error. 111 func (p Providers) ByScheme(scheme string) (Getter, error) { 112 for _, pp := range p { 113 if pp.Provides(scheme) { 114 return pp.New() 115 } 116 } 117 return nil, errors.Errorf("scheme %q not supported", scheme) 118 } 119 120 var httpProvider = Provider{ 121 Schemes: []string{"http", "https"}, 122 New: NewHTTPGetter, 123 } 124 125 // All finds all of the registered getters as a list of Provider instances. 126 // Currently, the built-in getters and the discovered plugins with downloader 127 // notations are collected. 128 func All(settings *cli.EnvSettings) Providers { 129 result := Providers{httpProvider} 130 pluginDownloaders, _ := collectPlugins(settings) 131 result = append(result, pluginDownloaders...) 132 return result 133 }