github.com/loafoe/helm@v1.0.1/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  	"path/filepath"
    24  
    25  	"github.com/pkg/errors"
    26  	"github.com/spf13/cobra"
    27  
    28  	"helm.sh/helm/v3/cmd/helm/require"
    29  	"helm.sh/helm/v3/pkg/helmpath"
    30  	"helm.sh/helm/v3/pkg/repo"
    31  )
    32  
    33  type repoRemoveOptions struct {
    34  	names     []string
    35  	repoFile  string
    36  	repoCache string
    37  }
    38  
    39  func newRepoRemoveCmd(out io.Writer) *cobra.Command {
    40  	o := &repoRemoveOptions{}
    41  
    42  	cmd := &cobra.Command{
    43  		Use:     "remove [REPO1 [REPO2 ...]]",
    44  		Aliases: []string{"rm"},
    45  		Short:   "remove one or more chart repositories",
    46  		Args:    require.MinimumNArgs(1),
    47  		ValidArgsFunction: func(cmd *cobra.Command, args []string, toComplete string) ([]string, cobra.ShellCompDirective) {
    48  			return compListRepos(toComplete, args), cobra.ShellCompDirectiveNoFileComp
    49  		},
    50  		RunE: func(cmd *cobra.Command, args []string) error {
    51  			o.repoFile = settings.RepositoryConfig
    52  			o.repoCache = settings.RepositoryCache
    53  			o.names = args
    54  			return o.run(out)
    55  		},
    56  	}
    57  	return cmd
    58  }
    59  
    60  func (o *repoRemoveOptions) run(out io.Writer) error {
    61  	r, err := repo.LoadFile(o.repoFile)
    62  	if isNotExist(err) || len(r.Repositories) == 0 {
    63  		return errors.New("no repositories configured")
    64  	}
    65  
    66  	for _, name := range o.names {
    67  		if !r.Remove(name) {
    68  			return errors.Errorf("no repo named %q found", name)
    69  		}
    70  		if err := r.WriteFile(o.repoFile, 0644); err != nil {
    71  			return err
    72  		}
    73  
    74  		if err := removeRepoCache(o.repoCache, name); err != nil {
    75  			return err
    76  		}
    77  		fmt.Fprintf(out, "%q has been removed from your repositories\n", name)
    78  	}
    79  
    80  	return nil
    81  }
    82  
    83  func removeRepoCache(root, name string) error {
    84  	idx := filepath.Join(root, helmpath.CacheChartsFile(name))
    85  	if _, err := os.Stat(idx); err == nil {
    86  		os.Remove(idx)
    87  	}
    88  
    89  	idx = filepath.Join(root, helmpath.CacheIndexFile(name))
    90  	if _, err := os.Stat(idx); os.IsNotExist(err) {
    91  		return nil
    92  	} else if err != nil {
    93  		return errors.Wrapf(err, "can't remove index file %s", idx)
    94  	}
    95  	return os.Remove(idx)
    96  }