github.com/codefresh-io/kcfi@v0.0.0-20230301195427-c1578715cc46/cmd/kcfi/uninstall.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  	"time"
    23  
    24  	"github.com/spf13/cobra"
    25  
    26  	"github.com/codefresh-io/kcfi/pkg/helm-internal/completion"
    27  	"helm.sh/helm/v3/cmd/helm/require"
    28  	"helm.sh/helm/v3/pkg/action"
    29  )
    30  
    31  const uninstallDesc = `
    32  This command takes a release name and uninstalls the release.
    33  
    34  It removes all of the resources associated with the last release of the chart
    35  as well as the release history, freeing it up for future use.
    36  
    37  Use the '--dry-run' flag to see which releases will be uninstalled without actually
    38  uninstalling them.
    39  `
    40  
    41  func newUninstallCmd(cfg *action.Configuration, out io.Writer) *cobra.Command {
    42  	client := action.NewUninstall(cfg)
    43  
    44  	cmd := &cobra.Command{
    45  		Use:        "uninstall RELEASE_NAME [...]",
    46  		Aliases:    []string{"del", "delete", "un"},
    47  		SuggestFor: []string{"remove", "rm"},
    48  		Short:      "uninstall a release",
    49  		Long:       uninstallDesc,
    50  		Args:       require.MinimumNArgs(1),
    51  		RunE: func(cmd *cobra.Command, args []string) error {
    52  			for i := 0; i < len(args); i++ {
    53  
    54  				res, err := client.Run(args[i])
    55  				if err != nil {
    56  					return err
    57  				}
    58  				if res != nil && res.Info != "" {
    59  					fmt.Fprintln(out, res.Info)
    60  				}
    61  
    62  				fmt.Fprintf(out, "release \"%s\" uninstalled\n", args[i])
    63  			}
    64  			return nil
    65  		},
    66  	}
    67  
    68  	// Function providing dynamic auto-completion
    69  	completion.RegisterValidArgsFunc(cmd, func(cmd *cobra.Command, args []string, toComplete string) ([]string, completion.BashCompDirective) {
    70  		if len(args) != 0 {
    71  			return nil, completion.BashCompDirectiveNoFileComp
    72  		}
    73  		return compListReleases(toComplete, cfg)
    74  	})
    75  
    76  	f := cmd.Flags()
    77  	f.BoolVar(&client.DryRun, "dry-run", false, "simulate a uninstall")
    78  	f.BoolVar(&client.DisableHooks, "no-hooks", false, "prevent hooks from running during uninstallation")
    79  	f.BoolVar(&client.KeepHistory, "keep-history", false, "remove all associated resources and mark the release as deleted, but retain the release history")
    80  	f.DurationVar(&client.Timeout, "timeout", 300*time.Second, "time to wait for any individual Kubernetes operation (like Jobs for hooks)")
    81  	f.StringVar(&client.Description, "description", "", "add a custom description")
    82  
    83  	return cmd
    84  }