github.com/stefanmcshane/helm@v0.0.0-20221213002717-88a4a2c6e77d/pkg/action/push.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 action
    18  
    19  import (
    20  	"strings"
    21  
    22  	"github.com/stefanmcshane/helm/pkg/cli"
    23  	"github.com/stefanmcshane/helm/pkg/pusher"
    24  	"github.com/stefanmcshane/helm/pkg/registry"
    25  	"github.com/stefanmcshane/helm/pkg/uploader"
    26  )
    27  
    28  // Push is the action for uploading a chart.
    29  //
    30  // It provides the implementation of 'helm push'.
    31  type Push struct {
    32  	Settings *cli.EnvSettings
    33  	cfg      *Configuration
    34  }
    35  
    36  // PushOpt is a type of function that sets options for a push action.
    37  type PushOpt func(*Push)
    38  
    39  // WithPushConfig sets the cfg field on the push configuration object.
    40  func WithPushConfig(cfg *Configuration) PushOpt {
    41  	return func(p *Push) {
    42  		p.cfg = cfg
    43  	}
    44  }
    45  
    46  // NewPushWithOpts creates a new push, with configuration options.
    47  func NewPushWithOpts(opts ...PushOpt) *Push {
    48  	p := &Push{}
    49  	for _, fn := range opts {
    50  		fn(p)
    51  	}
    52  	return p
    53  }
    54  
    55  // Run executes 'helm push' against the given chart archive.
    56  func (p *Push) Run(chartRef string, remote string) (string, error) {
    57  	var out strings.Builder
    58  
    59  	c := uploader.ChartUploader{
    60  		Out:     &out,
    61  		Pushers: pusher.All(p.Settings),
    62  		Options: []pusher.Option{},
    63  	}
    64  
    65  	if registry.IsOCI(remote) {
    66  		c.Options = append(c.Options, pusher.WithRegistryClient(p.cfg.RegistryClient))
    67  	}
    68  
    69  	return out.String(), c.UploadTo(chartRef, remote)
    70  }