github.com/darkowlzz/helm@v2.5.1-0.20171213183701-6707fe0468d4+incompatible/cmd/helm/repo_add.go (about)

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