github.com/umeshredd/helm@v3.0.0-alpha.1+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  // Getter is an interface to support GET to the specified URL.
    28  type Getter interface {
    29  	//Get file content by url string
    30  	Get(url string) (*bytes.Buffer, error)
    31  }
    32  
    33  // Constructor is the function for every getter which creates a specific instance
    34  // according to the configuration
    35  type Constructor func(URL, CertFile, KeyFile, CAFile string) (Getter, error)
    36  
    37  // Provider represents any getter and the schemes that it supports.
    38  //
    39  // For example, an HTTP provider may provide one getter that handles both
    40  // 'http' and 'https' schemes.
    41  type Provider struct {
    42  	Schemes []string
    43  	New     Constructor
    44  }
    45  
    46  // Provides returns true if the given scheme is supported by this Provider.
    47  func (p Provider) Provides(scheme string) bool {
    48  	for _, i := range p.Schemes {
    49  		if i == scheme {
    50  			return true
    51  		}
    52  	}
    53  	return false
    54  }
    55  
    56  // Providers is a collection of Provider objects.
    57  type Providers []Provider
    58  
    59  // ByScheme returns a Provider that handles the given scheme.
    60  //
    61  // If no provider handles this scheme, this will return an error.
    62  func (p Providers) ByScheme(scheme string) (Constructor, error) {
    63  	for _, pp := range p {
    64  		if pp.Provides(scheme) {
    65  			return pp.New, nil
    66  		}
    67  	}
    68  	return nil, errors.Errorf("scheme %q not supported", scheme)
    69  }
    70  
    71  // All finds all of the registered getters as a list of Provider instances.
    72  // Currently the build-in http/https getter and the discovered
    73  // plugins with downloader notations are collected.
    74  func All(settings cli.EnvSettings) Providers {
    75  	result := Providers{
    76  		{
    77  			Schemes: []string{"http", "https"},
    78  			New:     newHTTPGetter,
    79  		},
    80  	}
    81  	pluginDownloaders, _ := collectPlugins(settings)
    82  	result = append(result, pluginDownloaders...)
    83  	return result
    84  }
    85  
    86  // ByScheme returns a getter for the given scheme.
    87  //
    88  // If the scheme is not supported, this will return an error.
    89  func ByScheme(scheme string, settings cli.EnvSettings) (Provider, error) {
    90  	// Q: What do you call a scheme string who's the boss?
    91  	// A: Bruce Schemestring, of course.
    92  	a := All(settings)
    93  	for _, p := range a {
    94  		if p.Provides(scheme) {
    95  			return p, nil
    96  		}
    97  	}
    98  	return Provider{}, errors.Errorf("scheme %q not supported", scheme)
    99  }