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

     1  /*
     2  Adapted from
     3  https://github.com/kubernetes/kubectl/tree/master/pkg/cmd/edit
     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  	cmdutil "k8s.io/kubectl/pkg/cmd/util"
    31  	"k8s.io/kubectl/pkg/cmd/util/editor"
    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 editCmd struct {
    39  	streams genericclioptions.IOStreams
    40  	options *editor.EditOptions
    41  	cmd     *cobra.Command
    42  }
    43  
    44  var _ tiltCmd = &editCmd{}
    45  
    46  func newEditCmd(streams genericclioptions.IOStreams) *editCmd {
    47  	return &editCmd{
    48  		streams: streams,
    49  	}
    50  }
    51  
    52  func (c *editCmd) name() model.TiltSubcommand { return "edit" }
    53  
    54  func (c *editCmd) register() *cobra.Command {
    55  	o := editor.NewEditOptions(editor.NormalEditMode, c.streams)
    56  
    57  	cmd := &cobra.Command{
    58  		Use:                   "edit (RESOURCE/NAME | -f FILENAME)",
    59  		DisableFlagsInUseLine: true,
    60  		Short:                 "Edit a resource on the server",
    61  	}
    62  
    63  	// bind flag structs
    64  	o.RecordFlags.AddFlags(cmd)
    65  	o.PrintFlags.AddFlags(cmd)
    66  
    67  	usage := "to use to edit the resource"
    68  	cmdutil.AddFilenameOptionFlags(cmd, &o.FilenameOptions, usage)
    69  	cmdutil.AddValidateFlags(cmd)
    70  	cmd.Flags().BoolVarP(&o.OutputPatch, "output-patch", "", o.OutputPatch, "Output the patch if the resource is edited.")
    71  	cmd.Flags().BoolVar(&o.WindowsLineEndings, "windows-line-endings", o.WindowsLineEndings,
    72  		"Defaults to the line ending native to your platform.")
    73  	cmdutil.AddFieldManagerFlagVar(cmd, &o.FieldManager, "kubectl-edit")
    74  	cmdutil.AddApplyAnnotationVarFlags(cmd, &o.ApplyAnnotation)
    75  
    76  	addConnectServerFlags(cmd)
    77  
    78  	c.options = o
    79  	c.cmd = cmd
    80  	return cmd
    81  }
    82  
    83  func (c *editCmd) run(ctx context.Context, args []string) error {
    84  	a := analytics.Get(ctx)
    85  	cmdTags := engineanalytics.CmdTags(map[string]string{})
    86  	a.Incr("cmd.edit", cmdTags.AsMap())
    87  	defer a.Flush(time.Second)
    88  
    89  	o := c.options
    90  	getter, err := wireClientGetter(ctx)
    91  	if err != nil {
    92  		return err
    93  	}
    94  
    95  	f := cmdutil.NewFactory(getter)
    96  	cmdutil.CheckErr(o.Complete(f, args, c.cmd))
    97  	cmdutil.CheckErr(o.Run())
    98  	return nil
    99  }