github.com/defensepoint-snyk-test/helm-new@v0.0.0-20211130153739-c57ea64d6603/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/spf13/cobra"
    24  
    25  	"golang.org/x/crypto/ssh/terminal"
    26  	"k8s.io/helm/pkg/getter"
    27  	"k8s.io/helm/pkg/helm/helmpath"
    28  	"k8s.io/helm/pkg/repo"
    29  	"syscall"
    30  )
    31  
    32  type repoAddCmd 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  	out io.Writer
    45  }
    46  
    47  func newRepoAddCmd(out io.Writer) *cobra.Command {
    48  	add := &repoAddCmd{out: out}
    49  
    50  	cmd := &cobra.Command{
    51  		Use:   "add [flags] [NAME] [URL]",
    52  		Short: "add a chart repository",
    53  		RunE: func(cmd *cobra.Command, args []string) error {
    54  			if err := checkArgsLength(len(args), "name for the chart repository", "the url of the chart repository"); err != nil {
    55  				return err
    56  			}
    57  
    58  			add.name = args[0]
    59  			add.url = args[1]
    60  			add.home = settings.Home
    61  
    62  			return add.run()
    63  		},
    64  	}
    65  
    66  	f := cmd.Flags()
    67  	f.StringVar(&add.username, "username", "", "chart repository username")
    68  	f.StringVar(&add.password, "password", "", "chart repository password")
    69  	f.BoolVar(&add.noupdate, "no-update", false, "raise error if repo is already registered")
    70  	f.StringVar(&add.certFile, "cert-file", "", "identify HTTPS client using this SSL certificate file")
    71  	f.StringVar(&add.keyFile, "key-file", "", "identify HTTPS client using this SSL key file")
    72  	f.StringVar(&add.caFile, "ca-file", "", "verify certificates of HTTPS-enabled servers using this CA bundle")
    73  
    74  	return cmd
    75  }
    76  
    77  func (a *repoAddCmd) run() error {
    78  	if a.username != "" && a.password == "" {
    79  		fmt.Fprint(a.out, "Password:")
    80  		password, err := readPassword()
    81  		fmt.Fprintln(a.out)
    82  		if err != nil {
    83  			return err
    84  		}
    85  		a.password = password
    86  	}
    87  
    88  	if err := addRepository(a.name, a.url, a.username, a.password, a.home, a.certFile, a.keyFile, a.caFile, a.noupdate); err != nil {
    89  		return err
    90  	}
    91  	fmt.Fprintf(a.out, "%q has been added to your repositories\n", a.name)
    92  	return nil
    93  }
    94  
    95  func readPassword() (string, error) {
    96  	password, err := terminal.ReadPassword(int(syscall.Stdin))
    97  	if err != nil {
    98  		return "", err
    99  	}
   100  	return string(password), nil
   101  }
   102  
   103  func addRepository(name, url, username, password string, home helmpath.Home, certFile, keyFile, caFile string, noUpdate bool) error {
   104  	f, err := repo.LoadRepositoriesFile(home.RepositoryFile())
   105  	if err != nil {
   106  		return err
   107  	}
   108  
   109  	if noUpdate && f.Has(name) {
   110  		return fmt.Errorf("repository name (%s) already exists, please specify a different name", name)
   111  	}
   112  
   113  	cif := home.CacheIndex(name)
   114  	c := repo.Entry{
   115  		Name:     name,
   116  		Cache:    cif,
   117  		URL:      url,
   118  		Username: username,
   119  		Password: password,
   120  		CertFile: certFile,
   121  		KeyFile:  keyFile,
   122  		CAFile:   caFile,
   123  	}
   124  
   125  	r, err := repo.NewChartRepository(&c, getter.All(settings))
   126  	if err != nil {
   127  		return err
   128  	}
   129  
   130  	if err := r.DownloadIndexFile(home.Cache()); err != nil {
   131  		return fmt.Errorf("Looks like %q is not a valid chart repository or cannot be reached: %s", url, err.Error())
   132  	}
   133  
   134  	f.Update(&c)
   135  
   136  	return f.WriteFile(home.RepositoryFile(), 0644)
   137  }