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