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