github.com/stefanmcshane/helm@v0.0.0-20221213002717-88a4a2c6e77d/pkg/pusher/pusher.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 pusher
    18  
    19  import (
    20  	"github.com/pkg/errors"
    21  
    22  	"github.com/stefanmcshane/helm/pkg/cli"
    23  	"github.com/stefanmcshane/helm/pkg/registry"
    24  )
    25  
    26  // options are generic parameters to be provided to the pusher during instantiation.
    27  //
    28  // Pushers may or may not ignore these parameters as they are passed in.
    29  type options struct {
    30  	registryClient *registry.Client
    31  }
    32  
    33  // Option allows specifying various settings configurable by the user for overriding the defaults
    34  // used when performing Push operations with the Pusher.
    35  type Option func(*options)
    36  
    37  // WithRegistryClient sets the registryClient option.
    38  func WithRegistryClient(client *registry.Client) Option {
    39  	return func(opts *options) {
    40  		opts.registryClient = client
    41  	}
    42  }
    43  
    44  // Pusher is an interface to support upload to the specified URL.
    45  type Pusher interface {
    46  	// Push file content by url string
    47  	Push(chartRef, url string, options ...Option) error
    48  }
    49  
    50  // Constructor is the function for every pusher which creates a specific instance
    51  // according to the configuration
    52  type Constructor func(options ...Option) (Pusher, error)
    53  
    54  // Provider represents any pusher and the schemes that it supports.
    55  type Provider struct {
    56  	Schemes []string
    57  	New     Constructor
    58  }
    59  
    60  // Provides returns true if the given scheme is supported by this Provider.
    61  func (p Provider) Provides(scheme string) bool {
    62  	for _, i := range p.Schemes {
    63  		if i == scheme {
    64  			return true
    65  		}
    66  	}
    67  	return false
    68  }
    69  
    70  // Providers is a collection of Provider objects.
    71  type Providers []Provider
    72  
    73  // ByScheme returns a Provider that handles the given scheme.
    74  //
    75  // If no provider handles this scheme, this will return an error.
    76  func (p Providers) ByScheme(scheme string) (Pusher, error) {
    77  	for _, pp := range p {
    78  		if pp.Provides(scheme) {
    79  			return pp.New()
    80  		}
    81  	}
    82  	return nil, errors.Errorf("scheme %q not supported", scheme)
    83  }
    84  
    85  var ociProvider = Provider{
    86  	Schemes: []string{registry.OCIScheme},
    87  	New:     NewOCIPusher,
    88  }
    89  
    90  // All finds all of the registered pushers as a list of Provider instances.
    91  // Currently, just the built-in pushers are collected.
    92  func All(settings *cli.EnvSettings) Providers {
    93  	result := Providers{ociProvider}
    94  	return result
    95  }