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