...
1
16
17 package edit
18
19 import (
20 "github.com/spf13/cobra"
21
22 "k8s.io/cli-runtime/pkg/genericiooptions"
23
24 cmdutil "k8s.io/kubectl/pkg/cmd/util"
25 "k8s.io/kubectl/pkg/cmd/util/editor"
26 "k8s.io/kubectl/pkg/util/completion"
27 "k8s.io/kubectl/pkg/util/i18n"
28 "k8s.io/kubectl/pkg/util/templates"
29 )
30
31 var (
32 editLong = templates.LongDesc(i18n.T(`
33 Edit a resource from the default editor.
34
35 The edit command allows you to directly edit any API resource you can retrieve via the
36 command-line tools. It will open the editor defined by your KUBE_EDITOR, or EDITOR
37 environment variables, or fall back to 'vi' for Linux or 'notepad' for Windows.
38 When attempting to open the editor, it will first attempt to use the shell
39 that has been defined in the 'SHELL' environment variable. If this is not defined,
40 the default shell will be used, which is '/bin/bash' for Linux or 'cmd' for Windows.
41
42 You can edit multiple objects, although changes are applied one at a time. The command
43 accepts file names as well as command-line arguments, although the files you point to must
44 be previously saved versions of resources.
45
46 Editing is done with the API version used to fetch the resource.
47 To edit using a specific API version, fully-qualify the resource, version, and group.
48
49 The default format is YAML. To edit in JSON, specify "-o json".
50
51 The flag --windows-line-endings can be used to force Windows line endings,
52 otherwise the default for your operating system will be used.
53
54 In the event an error occurs while updating, a temporary file will be created on disk
55 that contains your unapplied changes. The most common error when updating a resource
56 is another editor changing the resource on the server. When this occurs, you will have
57 to apply your changes to the newer version of the resource, or update your temporary
58 saved copy to include the latest resource version.`))
59
60 editExample = templates.Examples(i18n.T(`
61 # Edit the service named 'registry'
62 kubectl edit svc/registry
63
64 # Use an alternative editor
65 KUBE_EDITOR="nano" kubectl edit svc/registry
66
67 # Edit the job 'myjob' in JSON using the v1 API format
68 kubectl edit job.v1.batch/myjob -o json
69
70 # Edit the deployment 'mydeployment' in YAML and save the modified config in its annotation
71 kubectl edit deployment/mydeployment -o yaml --save-config
72
73 # Edit the 'status' subresource for the 'mydeployment' deployment
74 kubectl edit deployment mydeployment --subresource='status'`))
75 )
76
77
78 func NewCmdEdit(f cmdutil.Factory, ioStreams genericiooptions.IOStreams) *cobra.Command {
79 o := editor.NewEditOptions(editor.NormalEditMode, ioStreams)
80 cmd := &cobra.Command{
81 Use: "edit (RESOURCE/NAME | -f FILENAME)",
82 DisableFlagsInUseLine: true,
83 Short: i18n.T("Edit a resource on the server"),
84 Long: editLong,
85 Example: editExample,
86 ValidArgsFunction: completion.ResourceTypeAndNameCompletionFunc(f),
87 Run: func(cmd *cobra.Command, args []string) {
88 cmdutil.CheckErr(o.Complete(f, args, cmd))
89 cmdutil.CheckErr(o.Validate())
90 cmdutil.CheckErr(o.Run())
91 },
92 }
93
94
95 o.RecordFlags.AddFlags(cmd)
96 o.PrintFlags.AddFlags(cmd)
97
98 usage := "to use to edit the resource"
99 cmdutil.AddFilenameOptionFlags(cmd, &o.FilenameOptions, usage)
100 cmdutil.AddValidateFlags(cmd)
101 cmd.Flags().BoolVarP(&o.OutputPatch, "output-patch", "", o.OutputPatch, "Output the patch if the resource is edited.")
102 cmd.Flags().BoolVar(&o.WindowsLineEndings, "windows-line-endings", o.WindowsLineEndings,
103 "Defaults to the line ending native to your platform.")
104 cmdutil.AddFieldManagerFlagVar(cmd, &o.FieldManager, "kubectl-edit")
105 cmdutil.AddApplyAnnotationVarFlags(cmd, &o.ApplyAnnotation)
106 cmdutil.AddSubresourceFlags(cmd, &o.Subresource, "If specified, edit will operate on the subresource of the requested object.", editor.SupportedSubresources...)
107 return cmd
108 }
109
View as plain text