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