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