github.com/latiif/helm@v2.15.0+incompatible/cmd/helm/repo_remove.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 "os" 23 24 "github.com/spf13/cobra" 25 26 "k8s.io/helm/pkg/helm/helmpath" 27 "k8s.io/helm/pkg/repo" 28 ) 29 30 type repoRemoveCmd struct { 31 out io.Writer 32 name string 33 home helmpath.Home 34 } 35 36 func newRepoRemoveCmd(out io.Writer) *cobra.Command { 37 remove := &repoRemoveCmd{out: out} 38 39 cmd := &cobra.Command{ 40 Use: "remove [flags] [NAME]", 41 Aliases: []string{"rm"}, 42 Short: "Remove a chart repository", 43 RunE: func(cmd *cobra.Command, args []string) error { 44 if len(args) == 0 { 45 return fmt.Errorf("need at least one argument, name of chart repository") 46 } 47 48 remove.home = settings.Home 49 for i := 0; i < len(args); i++ { 50 remove.name = args[i] 51 if err := remove.run(); err != nil { 52 return err 53 } 54 } 55 return nil 56 }, 57 } 58 59 return cmd 60 } 61 62 func (r *repoRemoveCmd) run() error { 63 return removeRepoLine(r.out, r.name, r.home) 64 } 65 66 func removeRepoLine(out io.Writer, name string, home helmpath.Home) error { 67 repoFile := home.RepositoryFile() 68 r, err := repo.LoadRepositoriesFile(repoFile) 69 if err != nil { 70 return err 71 } 72 73 if !r.Remove(name) { 74 return fmt.Errorf("no repo named %q found", name) 75 } 76 if err := r.WriteFile(repoFile, 0644); err != nil { 77 return err 78 } 79 80 if err := removeRepoCache(name, home); err != nil { 81 return err 82 } 83 84 fmt.Fprintf(out, "%q has been removed from your repositories\n", name) 85 86 return nil 87 } 88 89 func removeRepoCache(name string, home helmpath.Home) error { 90 if _, err := os.Stat(home.CacheIndex(name)); err == nil { 91 err = os.Remove(home.CacheIndex(name)) 92 if err != nil { 93 return err 94 } 95 } 96 return nil 97 }