github.com/tilt-dev/tilt@v0.33.15-0.20240515162809-0a22ed45d8a0/internal/cli/apply.go (about) 1 /* 2 Adapted from 3 https://github.com/kubernetes/kubectl/tree/master/pkg/cmd/apply 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/apply" 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 applyCmd struct { 39 streams genericclioptions.IOStreams 40 flags *apply.ApplyFlags 41 cmd *cobra.Command 42 } 43 44 var _ tiltCmd = &applyCmd{} 45 46 func newApplyCmd(streams genericclioptions.IOStreams) *applyCmd { 47 return &applyCmd{ 48 streams: streams, 49 } 50 } 51 52 func (c *applyCmd) name() model.TiltSubcommand { return "apply" } 53 54 func (c *applyCmd) register() *cobra.Command { 55 flags := apply.NewApplyFlags(c.streams) 56 57 cmd := &cobra.Command{ 58 Use: "apply (-f FILENAME | -k DIRECTORY)", 59 DisableFlagsInUseLine: true, 60 Short: "Apply a configuration to a resource by filename or stdin", 61 } 62 63 flags.AddFlags(cmd) 64 65 addConnectServerFlags(cmd) 66 67 c.cmd = cmd 68 c.flags = flags 69 return cmd 70 } 71 72 func (c *applyCmd) run(ctx context.Context, args []string) error { 73 a := analytics.Get(ctx) 74 cmdTags := engineanalytics.CmdTags(map[string]string{}) 75 a.Incr("cmd.apply", cmdTags.AsMap()) 76 defer a.Flush(time.Second) 77 78 getter, err := wireClientGetter(ctx) 79 if err != nil { 80 return err 81 } 82 83 f := cmdutil.NewFactory(getter) 84 85 cmd := c.cmd 86 o, err := c.flags.ToOptions(f, cmd, "tilt", args) 87 if err != nil { 88 return err 89 } 90 91 cmdutil.CheckErr(err) 92 cmdutil.CheckErr(o.Validate()) 93 cmdutil.CheckErr(o.Run()) 94 return nil 95 }