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