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

     1  /*
     2  Adapted from
     3  https://github.com/kubernetes/kubectl/tree/master/pkg/cmd/get
     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/get"
    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 getCmd struct {
    39  	options *get.GetOptions
    40  	cmd     *cobra.Command
    41  }
    42  
    43  var _ tiltCmd = &getCmd{}
    44  
    45  func newGetCmd(streams genericclioptions.IOStreams) *getCmd {
    46  	o := get.NewGetOptions("tilt", streams)
    47  	return &getCmd{
    48  		options: o,
    49  	}
    50  }
    51  
    52  func (c *getCmd) name() model.TiltSubcommand { return "get" }
    53  
    54  func (c *getCmd) register() *cobra.Command {
    55  	cmd := &cobra.Command{
    56  		Use:                   "get TYPE [NAME | -l label]",
    57  		DisableFlagsInUseLine: true,
    58  		Short:                 "Display one or many resources",
    59  	}
    60  	c.cmd = cmd
    61  	o := c.options
    62  
    63  	o.PrintFlags.AddFlags(cmd)
    64  
    65  	cmd.Flags().BoolVarP(&o.Watch, "watch", "w", o.Watch, "After listing/getting the requested object, watch for changes. Uninitialized objects are excluded if no object name is provided.")
    66  	cmd.Flags().BoolVar(&o.WatchOnly, "watch-only", o.WatchOnly, "Watch for changes to the requested object(s), without listing/getting first.")
    67  	cmd.Flags().BoolVar(&o.IgnoreNotFound, "ignore-not-found", o.IgnoreNotFound, "If the requested object does not exist the command will return exit code 0.")
    68  	cmd.Flags().StringVarP(&o.LabelSelector, "selector", "l", o.LabelSelector, "Selector (label query) to filter on, supports '=', '==', and '!='.(e.g. -l key1=value1,key2=value2)")
    69  	cmd.Flags().StringVar(&o.FieldSelector, "field-selector", o.FieldSelector, "Selector (field query) to filter on, supports '=', '==', and '!='.(e.g. --field-selector key1=value1,key2=value2). The server only supports a limited number of field queries per type.")
    70  	addConnectServerFlags(cmd)
    71  	return cmd
    72  }
    73  
    74  func (c *getCmd) run(ctx context.Context, args []string) error {
    75  	a := analytics.Get(ctx)
    76  	cmdTags := engineanalytics.CmdTags(map[string]string{})
    77  	a.Incr("cmd.get", cmdTags.AsMap())
    78  	defer a.Flush(time.Second)
    79  
    80  	o := c.options
    81  	getter, err := wireClientGetter(ctx)
    82  	if err != nil {
    83  		return err
    84  	}
    85  
    86  	f := cmdutil.NewFactory(getter)
    87  	cmd := c.cmd
    88  	cmdutil.CheckErr(o.Complete(f, cmd, args))
    89  	cmdutil.CheckErr(o.Validate())
    90  	cmdutil.CheckErr(o.Run(f, args))
    91  	return nil
    92  }