github.com/tilt-dev/tilt@v0.33.15-0.20240515162809-0a22ed45d8a0/internal/cli/delete.go (about)

     1  /*
     2  Adapted from
     3  https://github.com/kubernetes/kubectl/tree/master/pkg/cmd/delete
     4  */
     5  
     6  /*
     7  Copyright 2014 The Kubernetes Authors.
     8  
     9  Licensed under the Apache License, Version 2.0 (the "License");
    10  you may not use this file except in compliance with the License.
    11  You may obtain a copy of the License at
    12  
    13      http://www.apache.org/licenses/LICENSE-2.0
    14  
    15  Unless required by applicable law or agreed to in writing, software
    16  distributed under the License is distributed on an "AS IS" BASIS,
    17  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    18  See the License for the specific language governing permissions and
    19  limitations under the License.
    20  */
    21  
    22  package cli
    23  
    24  import (
    25  	"context"
    26  	"time"
    27  
    28  	"github.com/spf13/cobra"
    29  	"k8s.io/cli-runtime/pkg/genericclioptions"
    30  	"k8s.io/kubectl/pkg/cmd/delete"
    31  	cmdutil "k8s.io/kubectl/pkg/cmd/util"
    32  
    33  	"github.com/tilt-dev/tilt/internal/analytics"
    34  	engineanalytics "github.com/tilt-dev/tilt/internal/engine/analytics"
    35  	"github.com/tilt-dev/tilt/pkg/model"
    36  )
    37  
    38  type deleteCmd struct {
    39  	streams     genericclioptions.IOStreams
    40  	deleteFlags *delete.DeleteFlags
    41  	cmd         *cobra.Command
    42  }
    43  
    44  var _ tiltCmd = &deleteCmd{}
    45  
    46  func newDeleteCmd(streams genericclioptions.IOStreams) *deleteCmd {
    47  	deleteFlags := delete.NewDeleteCommandFlags("containing the resource to delete.")
    48  	return &deleteCmd{
    49  		streams:     streams,
    50  		deleteFlags: deleteFlags,
    51  	}
    52  }
    53  
    54  func (c *deleteCmd) name() model.TiltSubcommand { return "delete" }
    55  
    56  func (c *deleteCmd) register() *cobra.Command {
    57  	cmd := &cobra.Command{
    58  		Use:                   "delete ([-f FILENAME] | [-k DIRECTORY] | TYPE [(NAME | -l label | --all)])",
    59  		DisableFlagsInUseLine: true,
    60  		Short:                 "Delete resources by filenames, stdin, resources and names, or by resources and label selector",
    61  	}
    62  	c.cmd = cmd
    63  	c.deleteFlags.AddFlags(cmd)
    64  
    65  	cmdutil.AddDryRunFlag(cmd)
    66  
    67  	addConnectServerFlags(cmd)
    68  
    69  	return cmd
    70  }
    71  
    72  func (c *deleteCmd) run(ctx context.Context, args []string) error {
    73  	a := analytics.Get(ctx)
    74  	cmdTags := engineanalytics.CmdTags(map[string]string{})
    75  	a.Incr("cmd.delete", cmdTags.AsMap())
    76  	defer a.Flush(time.Second)
    77  
    78  	o, err := c.deleteFlags.ToOptions(nil, c.streams)
    79  	if err != nil {
    80  		return err
    81  	}
    82  
    83  	getter, err := wireClientGetter(ctx)
    84  	if err != nil {
    85  		return err
    86  	}
    87  
    88  	f := cmdutil.NewFactory(getter)
    89  	cmdutil.CheckErr(err)
    90  	cmdutil.CheckErr(o.Complete(f, args, c.cmd))
    91  	cmdutil.CheckErr(o.Validate())
    92  	cmdutil.CheckErr(o.RunDelete(f))
    93  
    94  	return nil
    95  }