github.com/tilt-dev/tilt@v0.36.0/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  	flags *explain.ExplainFlags
    58  }
    59  
    60  var _ tiltCmd = &explainCmd{}
    61  
    62  func newExplainCmd(streams genericclioptions.IOStreams) *explainCmd {
    63  	f := explain.NewExplainFlags(streams)
    64  	return &explainCmd{
    65  		flags: f,
    66  	}
    67  }
    68  
    69  func (c *explainCmd) name() model.TiltSubcommand { return "explain" }
    70  
    71  func (c *explainCmd) register() *cobra.Command {
    72  
    73  	cmd := &cobra.Command{
    74  		Use:                   "explain RESOURCE",
    75  		DisableFlagsInUseLine: true,
    76  		Short:                 i18n.T("Get documentation for a resource"),
    77  		Long:                  explainLong,
    78  		Example:               explainExamples,
    79  	}
    80  	cmd.Flags().BoolVar(&c.flags.Recursive, "recursive", c.flags.Recursive, "Print the fields of fields (Currently only 1 level deep)")
    81  	cmd.Flags().StringVar(&c.flags.APIVersion, "api-version", c.flags.APIVersion, "Get different explanations for particular API version (API group/version)")
    82  
    83  	// TODO(nick): Currently, tilt explain must connect to a running tilt
    84  	// environment.  But there's not really a fundamental reason why we couldn't
    85  	// fall back to a headless server, like 'tilt dump openapi' does.
    86  	addConnectServerFlags(cmd)
    87  
    88  	return cmd
    89  }
    90  
    91  func (c *explainCmd) run(ctx context.Context, args []string) error {
    92  	a := analytics.Get(ctx)
    93  	cmdTags := engineanalytics.CmdTags(map[string]string{})
    94  	a.Incr("cmd.explain", cmdTags.AsMap())
    95  	defer a.Flush(time.Second)
    96  
    97  	getter, err := wireClientGetter(ctx)
    98  	if err != nil {
    99  		return err
   100  	}
   101  
   102  	f := cmdutil.NewFactory(getter)
   103  	o, err := c.flags.ToOptions(f, "tilt", args)
   104  	cmdutil.CheckErr(err)
   105  	cmdutil.CheckErr(o.Validate())
   106  	cmdutil.CheckErr(o.Run())
   107  	return nil
   108  }