github.com/appscode/helm@v3.0.0-alpha.1+incompatible/cmd/helm/repo_add.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 main
    18  
    19  import (
    20  	"fmt"
    21  	"io"
    22  
    23  	"github.com/pkg/errors"
    24  	"github.com/spf13/cobra"
    25  
    26  	"helm.sh/helm/cmd/helm/require"
    27  	"helm.sh/helm/pkg/getter"
    28  	"helm.sh/helm/pkg/helmpath"
    29  	"helm.sh/helm/pkg/repo"
    30  )
    31  
    32  type repoAddOptions struct {
    33  	name     string
    34  	url      string
    35  	username string
    36  	password string
    37  	home     helmpath.Home
    38  	noupdate bool
    39  
    40  	certFile string
    41  	keyFile  string
    42  	caFile   string
    43  }
    44  
    45  func newRepoAddCmd(out io.Writer) *cobra.Command {
    46  	o := &repoAddOptions{}
    47  
    48  	cmd := &cobra.Command{
    49  		Use:   "add [NAME] [URL]",
    50  		Short: "add a chart repository",
    51  		Args:  require.ExactArgs(2),
    52  		RunE: func(cmd *cobra.Command, args []string) error {
    53  			o.name = args[0]
    54  			o.url = args[1]
    55  			o.home = settings.Home
    56  
    57  			return o.run(out)
    58  		},
    59  	}
    60  
    61  	f := cmd.Flags()
    62  	f.StringVar(&o.username, "username", "", "chart repository username")
    63  	f.StringVar(&o.password, "password", "", "chart repository password")
    64  	f.BoolVar(&o.noupdate, "no-update", false, "raise error if repo is already registered")
    65  	f.StringVar(&o.certFile, "cert-file", "", "identify HTTPS client using this SSL certificate file")
    66  	f.StringVar(&o.keyFile, "key-file", "", "identify HTTPS client using this SSL key file")
    67  	f.StringVar(&o.caFile, "ca-file", "", "verify certificates of HTTPS-enabled servers using this CA bundle")
    68  
    69  	return cmd
    70  }
    71  
    72  func (o *repoAddOptions) run(out io.Writer) error {
    73  	if err := addRepository(o.name, o.url, o.username, o.password, o.home, o.certFile, o.keyFile, o.caFile, o.noupdate); err != nil {
    74  		return err
    75  	}
    76  	fmt.Fprintf(out, "%q has been added to your repositories\n", o.name)
    77  	return nil
    78  }
    79  
    80  func addRepository(name, url, username, password string, home helmpath.Home, certFile, keyFile, caFile string, noUpdate bool) error {
    81  	f, err := repo.LoadFile(home.RepositoryFile())
    82  	if err != nil {
    83  		return err
    84  	}
    85  
    86  	if noUpdate && f.Has(name) {
    87  		return errors.Errorf("repository name (%s) already exists, please specify a different name", name)
    88  	}
    89  
    90  	cif := home.CacheIndex(name)
    91  	c := repo.Entry{
    92  		Name:     name,
    93  		Cache:    cif,
    94  		URL:      url,
    95  		Username: username,
    96  		Password: password,
    97  		CertFile: certFile,
    98  		KeyFile:  keyFile,
    99  		CAFile:   caFile,
   100  	}
   101  
   102  	r, err := repo.NewChartRepository(&c, getter.All(settings))
   103  	if err != nil {
   104  		return err
   105  	}
   106  
   107  	if err := r.DownloadIndexFile(home.Cache()); err != nil {
   108  		return errors.Wrapf(err, "looks like %q is not a valid chart repository or cannot be reached", url)
   109  	}
   110  
   111  	f.Update(&c)
   112  
   113  	return f.WriteFile(home.RepositoryFile(), 0644)
   114  }