github.com/diafour/helm@v3.0.0-beta.3+incompatible/pkg/getter/plugingetter.go (about) 1 /* 2 Copyright The Helm Authors. 3 Licensed under the Apache License, Version 2.0 (the "License"); 4 you may not use this file except in compliance with the License. 5 You may obtain a copy of the License at 6 7 http://www.apache.org/licenses/LICENSE-2.0 8 9 Unless required by applicable law or agreed to in writing, software 10 distributed under the License is distributed on an "AS IS" BASIS, 11 WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 See the License for the specific language governing permissions and 13 limitations under the License. 14 */ 15 16 package getter 17 18 import ( 19 "bytes" 20 "os" 21 "os/exec" 22 "path/filepath" 23 "strings" 24 25 "github.com/pkg/errors" 26 27 "helm.sh/helm/pkg/cli" 28 "helm.sh/helm/pkg/plugin" 29 ) 30 31 // collectPlugins scans for getter plugins. 32 // This will load plugins according to the cli. 33 func collectPlugins(settings *cli.EnvSettings) (Providers, error) { 34 plugins, err := plugin.FindPlugins(settings.PluginsDirectory) 35 if err != nil { 36 return nil, err 37 } 38 var result Providers 39 for _, plugin := range plugins { 40 for _, downloader := range plugin.Metadata.Downloaders { 41 result = append(result, Provider{ 42 Schemes: downloader.Protocols, 43 New: NewPluginGetter( 44 downloader.Command, 45 settings, 46 plugin.Metadata.Name, 47 plugin.Dir, 48 ), 49 }) 50 } 51 } 52 return result, nil 53 } 54 55 // pluginGetter is a generic type to invoke custom downloaders, 56 // implemented in plugins. 57 type pluginGetter struct { 58 command string 59 settings *cli.EnvSettings 60 name string 61 base string 62 opts options 63 } 64 65 // Get runs downloader plugin command 66 func (p *pluginGetter) Get(href string, options ...Option) (*bytes.Buffer, error) { 67 for _, opt := range options { 68 opt(&p.opts) 69 } 70 commands := strings.Split(p.command, " ") 71 argv := append(commands[1:], p.opts.certFile, p.opts.keyFile, p.opts.caFile, href) 72 prog := exec.Command(filepath.Join(p.base, commands[0]), argv...) 73 plugin.SetupPluginEnv(p.settings, p.name, p.base) 74 prog.Env = os.Environ() 75 buf := bytes.NewBuffer(nil) 76 prog.Stdout = buf 77 prog.Stderr = os.Stderr 78 if err := prog.Run(); err != nil { 79 if eerr, ok := err.(*exec.ExitError); ok { 80 os.Stderr.Write(eerr.Stderr) 81 return nil, errors.Errorf("plugin %q exited with error", p.command) 82 } 83 return nil, err 84 } 85 return buf, nil 86 } 87 88 // NewPluginGetter constructs a valid plugin getter 89 func NewPluginGetter(command string, settings *cli.EnvSettings, name, base string) Constructor { 90 return func(options ...Option) (Getter, error) { 91 result := &pluginGetter{ 92 command: command, 93 settings: settings, 94 name: name, 95 base: base, 96 } 97 for _, opt := range options { 98 opt(&result.opts) 99 } 100 return result, nil 101 } 102 }