github.com/amy/helm@v2.7.2+incompatible/cmd/helm/repo_remove.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 "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 err := checkArgsLength(len(args), "name of chart repository"); err != nil { 45 return err 46 } 47 remove.name = args[0] 48 remove.home = settings.Home 49 50 return remove.run() 51 }, 52 } 53 54 return cmd 55 } 56 57 func (r *repoRemoveCmd) run() error { 58 return removeRepoLine(r.out, r.name, r.home) 59 } 60 61 func removeRepoLine(out io.Writer, name string, home helmpath.Home) error { 62 repoFile := home.RepositoryFile() 63 r, err := repo.LoadRepositoriesFile(repoFile) 64 if err != nil { 65 return err 66 } 67 68 if !r.Remove(name) { 69 return fmt.Errorf("no repo named %q found", name) 70 } 71 if err := r.WriteFile(repoFile, 0644); err != nil { 72 return err 73 } 74 75 if err := removeRepoCache(name, home); err != nil { 76 return err 77 } 78 79 fmt.Fprintf(out, "%q has been removed from your repositories\n", name) 80 81 return nil 82 } 83 84 func removeRepoCache(name string, home helmpath.Home) error { 85 if _, err := os.Stat(home.CacheIndex(name)); err == nil { 86 err = os.Remove(home.CacheIndex(name)) 87 if err != nil { 88 return err 89 } 90 } 91 return nil 92 }