github.com/tilt-dev/tilt@v0.33.15-0.20240515162809-0a22ed45d8a0/internal/cli/create.go (about) 1 /* 2 Adapted from 3 https://github.com/kubernetes/kubectl/tree/master/pkg/cmd/create 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 "runtime" 27 "time" 28 29 "github.com/spf13/cobra" 30 "k8s.io/cli-runtime/pkg/genericclioptions" 31 "k8s.io/kubectl/pkg/cmd/create" 32 cmdutil "k8s.io/kubectl/pkg/cmd/util" 33 34 "github.com/tilt-dev/tilt/internal/analytics" 35 engineanalytics "github.com/tilt-dev/tilt/internal/engine/analytics" 36 "github.com/tilt-dev/tilt/pkg/model" 37 ) 38 39 type createCmd struct { 40 streams genericclioptions.IOStreams 41 options *create.CreateOptions 42 cmd *cobra.Command 43 } 44 45 var _ tiltCmd = &createCmd{} 46 47 func newCreateCmd(streams genericclioptions.IOStreams) *createCmd { 48 return &createCmd{ 49 streams: streams, 50 } 51 } 52 53 func (c *createCmd) name() model.TiltSubcommand { return "create" } 54 55 func (c *createCmd) register() *cobra.Command { 56 o := create.NewCreateOptions(c.streams) 57 58 cmd := &cobra.Command{ 59 Use: "create -f FILENAME", 60 DisableFlagsInUseLine: true, 61 Short: "Create a resource from a file or from stdin.", 62 } 63 64 // bind flag structs 65 o.RecordFlags.AddFlags(cmd) 66 67 usage := "to use to create the resource" 68 cmdutil.AddFilenameOptionFlags(cmd, &o.FilenameOptions, usage) 69 cmdutil.AddValidateFlags(cmd) 70 cmd.Flags().BoolVar(&o.EditBeforeCreate, "edit", o.EditBeforeCreate, "Edit the API resource before creating") 71 cmd.Flags().Bool("windows-line-endings", runtime.GOOS == "windows", 72 "Only relevant if --edit=true. Defaults to the line ending native to your platform.") 73 cmdutil.AddApplyAnnotationFlags(cmd) 74 cmdutil.AddDryRunFlag(cmd) 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 77 o.PrintFlags.AddFlags(cmd) 78 addConnectServerFlags(cmd) 79 80 c.options = o 81 c.cmd = cmd 82 83 addCommand(cmd, newCreateFileWatchCmd(c.streams)) 84 addCommand(cmd, newCreateCmdCmd(c.streams)) 85 addCommand(cmd, newCreateRepoCmd(c.streams)) 86 addCommand(cmd, newCreateExtCmd(c.streams)) 87 88 return cmd 89 } 90 91 func (c *createCmd) run(ctx context.Context, args []string) error { 92 a := analytics.Get(ctx) 93 cmdTags := engineanalytics.CmdTags(map[string]string{}) 94 a.Incr("cmd.create", cmdTags.AsMap()) 95 defer a.Flush(time.Second) 96 97 o := c.options 98 getter, err := wireClientGetter(ctx) 99 if err != nil { 100 return err 101 } 102 103 f := cmdutil.NewFactory(getter) 104 cmd := c.cmd 105 106 if cmdutil.IsFilenameSliceEmpty(o.FilenameOptions.Filenames, o.FilenameOptions.Kustomize) { 107 _, _ = c.streams.ErrOut.Write([]byte("Error: must specify one of -f and -k\n\n")) 108 return nil 109 } 110 cmdutil.CheckErr(o.Complete(f, cmd, args)) 111 cmdutil.CheckErr(o.RunCreate(f, cmd)) 112 return nil 113 }