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

     1  /*
     2  Adapted from
     3  https://github.com/kubernetes/kubectl/tree/master/pkg/cmd/explain
     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/explain"
    31  	cmdutil "k8s.io/kubectl/pkg/cmd/util"
    32  	"k8s.io/kubectl/pkg/util/i18n"
    33  	"k8s.io/kubectl/pkg/util/templates"
    34  
    35  	"github.com/tilt-dev/tilt/internal/analytics"
    36  	engineanalytics "github.com/tilt-dev/tilt/internal/engine/analytics"
    37  	"github.com/tilt-dev/tilt/pkg/model"
    38  )
    39  
    40  var (
    41  	explainLong = templates.LongDesc(i18n.T(`
    42  		List the fields for supported resources.
    43  		This command describes the fields associated with each supported API resource.
    44  		Fields are identified via a simple JSONPath identifier:
    45  			<type>.<fieldName>[.<fieldName>]
    46  		Add the --recursive flag to display all of the fields at once without descriptions.
    47  		Information about each field is retrieved from the server in OpenAPI format.`))
    48  
    49  	explainExamples = templates.Examples(i18n.T(`
    50  		# Get the documentation of the resource and its fields
    51  		tilt explain cmds
    52  		# Get the documentation of a specific field of a resource
    53  		tilt explain cmds.spec.args`))
    54  )
    55  
    56  type explainCmd struct {
    57  	options *explain.ExplainOptions
    58  	cmd     *cobra.Command
    59  }
    60  
    61  var _ tiltCmd = &explainCmd{}
    62  
    63  func newExplainCmd(streams genericclioptions.IOStreams) *explainCmd {
    64  	o := explain.NewExplainOptions("tilt", streams)
    65  	return &explainCmd{
    66  		options: o,
    67  	}
    68  }
    69  
    70  func (c *explainCmd) name() model.TiltSubcommand { return "explain" }
    71  
    72  func (c *explainCmd) register() *cobra.Command {
    73  
    74  	cmd := &cobra.Command{
    75  		Use:                   "explain RESOURCE",
    76  		DisableFlagsInUseLine: true,
    77  		Short:                 i18n.T("Get documentation for a resource"),
    78  		Long:                  explainLong,
    79  		Example:               explainExamples,
    80  	}
    81  	cmd.Flags().BoolVar(&c.options.Recursive, "recursive", c.options.Recursive, "Print the fields of fields (Currently only 1 level deep)")
    82  	cmd.Flags().StringVar(&c.options.APIVersion, "api-version", c.options.APIVersion, "Get different explanations for particular API version (API group/version)")
    83  
    84  	// TODO(nick): Currently, tilt explain must connect to a running tilt
    85  	// environment.  But there's not really a fundamental reason why we couldn't
    86  	// fall back to a headless server, like 'tilt dump openapi' does.
    87  	addConnectServerFlags(cmd)
    88  
    89  	return cmd
    90  }
    91  
    92  func (c *explainCmd) run(ctx context.Context, args []string) error {
    93  	a := analytics.Get(ctx)
    94  	cmdTags := engineanalytics.CmdTags(map[string]string{})
    95  	a.Incr("cmd.explain", cmdTags.AsMap())
    96  	defer a.Flush(time.Second)
    97  
    98  	o := c.options
    99  	getter, err := wireClientGetter(ctx)
   100  	if err != nil {
   101  		return err
   102  	}
   103  
   104  	f := cmdutil.NewFactory(getter)
   105  	cmd := c.cmd
   106  	cmdutil.CheckErr(o.Complete(f, cmd, args))
   107  	cmdutil.CheckErr(o.Validate())
   108  	cmdutil.CheckErr(o.Run())
   109  	return nil
   110  }