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

     1  /*
     2  Adapted from
     3  https://github.com/kubernetes/kubectl/tree/master/pkg/cmd/apiresources
     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/apiresources"
    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 apiresourcesCmd struct {
    39  	options *apiresources.APIResourceOptions
    40  	cmd     *cobra.Command
    41  }
    42  
    43  var _ tiltCmd = &apiresourcesCmd{}
    44  
    45  func newApiresourcesCmd(streams genericclioptions.IOStreams) *apiresourcesCmd {
    46  	o := apiresources.NewAPIResourceOptions(streams)
    47  	return &apiresourcesCmd{
    48  		options: o,
    49  	}
    50  }
    51  
    52  func (c *apiresourcesCmd) name() model.TiltSubcommand { return "apiresources" }
    53  
    54  func (c *apiresourcesCmd) register() *cobra.Command {
    55  	cmd := &cobra.Command{
    56  		Use:   "api-resources",
    57  		Short: "Print the supported API resources",
    58  	}
    59  	c.cmd = cmd
    60  	o := c.options
    61  
    62  	cmd.Flags().BoolVar(&o.NoHeaders, "no-headers", o.NoHeaders, "When using the default or custom-column output format, don't print headers (default print headers).")
    63  	cmd.Flags().StringVarP(&o.Output, "output", "o", o.Output, "Output format. One of: wide|name.")
    64  
    65  	cmd.Flags().StringVar(&o.APIGroup, "api-group", o.APIGroup, "Limit to resources in the specified API group.")
    66  	cmd.Flags().BoolVar(&o.Namespaced, "namespaced", o.Namespaced, "If false, non-namespaced resources will be returned, otherwise returning namespaced resources by default.")
    67  	cmd.Flags().StringSliceVar(&o.Verbs, "verbs", o.Verbs, "Limit to resources that support the specified verbs.")
    68  	cmd.Flags().StringVar(&o.SortBy, "sort-by", o.SortBy, "If non-empty, sort list of resources using specified field. The field can be either 'name' or 'kind'.")
    69  	cmd.Flags().BoolVar(&o.Cached, "cached", o.Cached, "Use the cached list of resources if available.")
    70  
    71  	addConnectServerFlags(cmd)
    72  	return cmd
    73  }
    74  
    75  func (c *apiresourcesCmd) run(ctx context.Context, args []string) error {
    76  	a := analytics.Get(ctx)
    77  	cmdTags := engineanalytics.CmdTags(map[string]string{})
    78  	a.Incr("cmd.api-resources", cmdTags.AsMap())
    79  	defer a.Flush(time.Second)
    80  
    81  	o := c.options
    82  	getter, err := wireClientGetter(ctx)
    83  	if err != nil {
    84  		return err
    85  	}
    86  
    87  	f := cmdutil.NewFactory(getter)
    88  	cmd := c.cmd
    89  	cmdutil.CheckErr(o.Complete(f, cmd, args))
    90  	cmdutil.CheckErr(o.Validate())
    91  	cmdutil.CheckErr(o.RunAPIResources())
    92  	return nil
    93  }