edit.go 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106
  1. /*
  2. Copyright 2015 The Kubernetes Authors.
  3. Licensed under the Apache License, Version 2.0 (the "License");
  4. you may not use this file except in compliance with the License.
  5. You may obtain a copy of the License at
  6. http://www.apache.org/licenses/LICENSE-2.0
  7. Unless required by applicable law or agreed to in writing, software
  8. distributed under the License is distributed on an "AS IS" BASIS,
  9. WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  10. See the License for the specific language governing permissions and
  11. limitations under the License.
  12. */
  13. package edit
  14. import (
  15. "fmt"
  16. "github.com/spf13/cobra"
  17. "k8s.io/cli-runtime/pkg/genericclioptions"
  18. cmdutil "k8s.io/kubernetes/pkg/kubectl/cmd/util"
  19. "k8s.io/kubernetes/pkg/kubectl/cmd/util/editor"
  20. "k8s.io/kubernetes/pkg/kubectl/util/i18n"
  21. "k8s.io/kubernetes/pkg/kubectl/util/templates"
  22. )
  23. var (
  24. editLong = templates.LongDesc(i18n.T(`
  25. Edit a resource from the default editor.
  26. The edit command allows you to directly edit any API resource you can retrieve via the
  27. command line tools. It will open the editor defined by your KUBE_EDITOR, or EDITOR
  28. environment variables, or fall back to 'vi' for Linux or 'notepad' for Windows.
  29. You can edit multiple objects, although changes are applied one at a time. The command
  30. accepts filenames as well as command line arguments, although the files you point to must
  31. be previously saved versions of resources.
  32. Editing is done with the API version used to fetch the resource.
  33. To edit using a specific API version, fully-qualify the resource, version, and group.
  34. The default format is YAML. To edit in JSON, specify "-o json".
  35. The flag --windows-line-endings can be used to force Windows line endings,
  36. otherwise the default for your operating system will be used.
  37. In the event an error occurs while updating, a temporary file will be created on disk
  38. that contains your unapplied changes. The most common error when updating a resource
  39. is another editor changing the resource on the server. When this occurs, you will have
  40. to apply your changes to the newer version of the resource, or update your temporary
  41. saved copy to include the latest resource version.`))
  42. editExample = templates.Examples(i18n.T(`
  43. # Edit the service named 'docker-registry':
  44. kubectl edit svc/docker-registry
  45. # Use an alternative editor
  46. KUBE_EDITOR="nano" kubectl edit svc/docker-registry
  47. # Edit the job 'myjob' in JSON using the v1 API format:
  48. kubectl edit job.v1.batch/myjob -o json
  49. # Edit the deployment 'mydeployment' in YAML and save the modified config in its annotation:
  50. kubectl edit deployment/mydeployment -o yaml --save-config`))
  51. )
  52. // NewCmdEdit creates the `edit` command
  53. func NewCmdEdit(f cmdutil.Factory, ioStreams genericclioptions.IOStreams) *cobra.Command {
  54. o := editor.NewEditOptions(editor.NormalEditMode, ioStreams)
  55. o.ValidateOptions = cmdutil.ValidateOptions{EnableValidation: true}
  56. cmd := &cobra.Command{
  57. Use: "edit (RESOURCE/NAME | -f FILENAME)",
  58. DisableFlagsInUseLine: true,
  59. Short: i18n.T("Edit a resource on the server"),
  60. Long: editLong,
  61. Example: fmt.Sprintf(editExample),
  62. Run: func(cmd *cobra.Command, args []string) {
  63. if err := o.Complete(f, args, cmd); err != nil {
  64. cmdutil.CheckErr(err)
  65. }
  66. if err := o.Run(); err != nil {
  67. cmdutil.CheckErr(err)
  68. }
  69. },
  70. }
  71. // bind flag structs
  72. o.RecordFlags.AddFlags(cmd)
  73. o.PrintFlags.AddFlags(cmd)
  74. usage := "to use to edit the resource"
  75. cmdutil.AddFilenameOptionFlags(cmd, &o.FilenameOptions, usage)
  76. cmdutil.AddValidateOptionFlags(cmd, &o.ValidateOptions)
  77. cmd.Flags().BoolVarP(&o.OutputPatch, "output-patch", "", o.OutputPatch, "Output the patch if the resource is edited.")
  78. cmd.Flags().BoolVar(&o.WindowsLineEndings, "windows-line-endings", o.WindowsLineEndings,
  79. "Defaults to the line ending native to your platform.")
  80. cmdutil.AddApplyAnnotationVarFlags(cmd, &o.ApplyAnnotation)
  81. cmdutil.AddIncludeUninitializedFlag(cmd)
  82. return cmd
  83. }