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

     1  /*
     2  Adapted from
     3  https://github.com/kubernetes/kubectl/tree/master/pkg/cmd/describe
     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/apimachinery/pkg/api/meta"
    30  	"k8s.io/cli-runtime/pkg/genericclioptions"
    31  	"k8s.io/cli-runtime/pkg/resource"
    32  	"k8s.io/kubectl/pkg/cmd/describe"
    33  	cmdutil "k8s.io/kubectl/pkg/cmd/util"
    34  	pkgdescribe "k8s.io/kubectl/pkg/describe"
    35  
    36  	"github.com/tilt-dev/tilt/internal/analytics"
    37  	engineanalytics "github.com/tilt-dev/tilt/internal/engine/analytics"
    38  	"github.com/tilt-dev/tilt/pkg/model"
    39  )
    40  
    41  type describeCmd struct {
    42  	options *describe.DescribeOptions
    43  	cmd     *cobra.Command
    44  }
    45  
    46  var _ tiltCmd = &describeCmd{}
    47  
    48  func newDescribeCmd(streams genericclioptions.IOStreams) *describeCmd {
    49  	o := &describe.DescribeOptions{
    50  		FilenameOptions: &resource.FilenameOptions{},
    51  		DescriberSettings: &pkgdescribe.DescriberSettings{
    52  			ShowEvents: false,
    53  		},
    54  
    55  		CmdParent: "tilt",
    56  		IOStreams: streams,
    57  	}
    58  	return &describeCmd{
    59  		options: o,
    60  	}
    61  }
    62  
    63  func (c *describeCmd) name() model.TiltSubcommand { return "describe" }
    64  
    65  func (c *describeCmd) register() *cobra.Command {
    66  	cmd := &cobra.Command{
    67  		Use:                   "describe (-f FILENAME | TYPE [NAME_PREFIX | -l label] | TYPE/NAME)",
    68  		DisableFlagsInUseLine: true,
    69  		Short:                 "Show details of a specific resource or group of resources",
    70  	}
    71  	c.cmd = cmd
    72  	o := c.options
    73  
    74  	cmdutil.AddFilenameOptionFlags(cmd, o.FilenameOptions, "containing the resources to describe")
    75  	cmd.Flags().StringVarP(&o.Selector, "selector", "l", o.Selector, "Selector (label query) to filter on, supports '=', '==', and '!='.(e.g. -l key1=value1,key2=value2)")
    76  	addConnectServerFlags(cmd)
    77  	return cmd
    78  }
    79  
    80  func (c *describeCmd) run(ctx context.Context, args []string) error {
    81  	a := analytics.Get(ctx)
    82  	cmdTags := engineanalytics.CmdTags(map[string]string{})
    83  	a.Incr("cmd.describe", cmdTags.AsMap())
    84  	defer a.Flush(time.Second)
    85  
    86  	o := c.options
    87  	getter, err := wireClientGetter(ctx)
    88  	if err != nil {
    89  		return err
    90  	}
    91  
    92  	f := cmdutil.NewFactory(getter)
    93  	o.NewBuilder = f.NewBuilder
    94  	o.BuilderArgs = args
    95  	o.Describer = func(mapping *meta.RESTMapping) (pkgdescribe.ResourceDescriber, error) {
    96  		return pkgdescribe.DescriberFn(f, mapping)
    97  	}
    98  	cmdutil.CheckErr(o.Validate())
    99  	cmdutil.CheckErr(o.Run())
   100  	return nil
   101  }