github.com/telepresenceio/telepresence/v2@v2.20.0-pro.6.0.20240517030216-236ea954e789/pkg/client/cli/cmd/uninstall.go (about)

     1  package cmd
     2  
     3  import (
     4  	"errors"
     5  
     6  	"github.com/spf13/cobra"
     7  
     8  	"github.com/telepresenceio/telepresence/rpc/v2/connector"
     9  	"github.com/telepresenceio/telepresence/v2/pkg/client/cli/ann"
    10  	"github.com/telepresenceio/telepresence/v2/pkg/client/cli/connect"
    11  	"github.com/telepresenceio/telepresence/v2/pkg/client/cli/daemon"
    12  	"github.com/telepresenceio/telepresence/v2/pkg/client/cli/helm"
    13  	"github.com/telepresenceio/telepresence/v2/pkg/errcat"
    14  	"github.com/telepresenceio/telepresence/v2/pkg/ioutil"
    15  )
    16  
    17  type uninstallCommand struct {
    18  	agent      bool
    19  	allAgents  bool
    20  	everything bool
    21  }
    22  
    23  func uninstall() *cobra.Command {
    24  	ui := &uninstallCommand{}
    25  	cmd := &cobra.Command{
    26  		Use:  "uninstall [flags] { --agent <agents...> | --all-agents }",
    27  		Args: ui.args,
    28  
    29  		Short: "Uninstall telepresence agents",
    30  		RunE:  ui.run,
    31  	}
    32  	flags := cmd.Flags()
    33  
    34  	flags.BoolVarP(&ui.agent, "agent", "d", false, "uninstall intercept agent on specific deployments")
    35  	flags.BoolVarP(&ui.allAgents, "all-agents", "a", false, "uninstall intercept agent on all deployments")
    36  
    37  	// Hidden from help but will yield a deprecation warning if used
    38  	flags.BoolVarP(&ui.everything, "everything", "e", false, "uninstall agents and the traffic manager")
    39  	flags.Lookup("everything").Hidden = true
    40  	return cmd
    41  }
    42  
    43  func (u *uninstallCommand) args(cmd *cobra.Command, args []string) error {
    44  	if u.agent && u.allAgents {
    45  		return errors.New("--agent and --all-agents are mutually exclusive")
    46  	}
    47  	if !(u.agent || u.allAgents) {
    48  		return errors.New("please specify --agent or --all-agents")
    49  	}
    50  	switch {
    51  	case u.agent && len(args) == 0:
    52  		return errors.New("at least one argument (the name of an agent) is expected")
    53  	case !u.agent && len(args) != 0:
    54  		return errors.New("unexpected argument(s)")
    55  	}
    56  	return nil
    57  }
    58  
    59  // uninstall.
    60  func (u *uninstallCommand) run(cmd *cobra.Command, args []string) error {
    61  	if u.everything {
    62  		ha := &HelmCommand{
    63  			Request: helm.Request{Type: helm.Uninstall},
    64  			rq:      daemon.InitRequest(cmd),
    65  		}
    66  		ioutil.Println(cmd.OutOrStderr(), "--everything is deprecated. Please use telepresence helm uninstall")
    67  		return ha.run(cmd, args)
    68  	}
    69  	cmd.Annotations = map[string]string{
    70  		ann.Session: ann.Required,
    71  	}
    72  	if err := connect.InitCommand(cmd); err != nil {
    73  		return err
    74  	}
    75  	ur := &connector.UninstallRequest{
    76  		UninstallType: 0,
    77  	}
    78  	switch {
    79  	case u.agent:
    80  		ur.UninstallType = connector.UninstallRequest_NAMED_AGENTS
    81  		ur.Agents = args
    82  	case u.everything:
    83  		return nil
    84  	default:
    85  		ur.UninstallType = connector.UninstallRequest_ALL_AGENTS
    86  	}
    87  	ctx := cmd.Context()
    88  	r, err := daemon.GetUserClient(ctx).Uninstall(ctx, ur)
    89  	if err != nil {
    90  		return err
    91  	}
    92  	return errcat.FromResult(r)
    93  }