github.com/azure-devops-engineer/helm@v3.0.0-alpha.2+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.PluginDirs())
    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) (*bytes.Buffer, error) {
    67  	commands := strings.Split(p.command, " ")
    68  	argv := append(commands[1:], p.opts.certFile, p.opts.keyFile, p.opts.caFile, href)
    69  	prog := exec.Command(filepath.Join(p.base, commands[0]), argv...)
    70  	plugin.SetupPluginEnv(p.settings, p.name, p.base)
    71  	prog.Env = os.Environ()
    72  	buf := bytes.NewBuffer(nil)
    73  	prog.Stdout = buf
    74  	prog.Stderr = os.Stderr
    75  	if err := prog.Run(); err != nil {
    76  		if eerr, ok := err.(*exec.ExitError); ok {
    77  			os.Stderr.Write(eerr.Stderr)
    78  			return nil, errors.Errorf("plugin %q exited with error", p.command)
    79  		}
    80  		return nil, err
    81  	}
    82  	return buf, nil
    83  }
    84  
    85  // NewPluginGetter constructs a valid plugin getter
    86  func NewPluginGetter(command string, settings cli.EnvSettings, name, base string) Constructor {
    87  	return func(options ...Option) (Getter, error) {
    88  		result := &pluginGetter{
    89  			command:  command,
    90  			settings: settings,
    91  			name:     name,
    92  			base:     base,
    93  		}
    94  		for _, opt := range options {
    95  			opt(&result.opts)
    96  		}
    97  		return result, nil
    98  	}
    99  }