mirror of
https://github.com/k3s-io/kubernetes.git
synced 2025-07-31 07:20:13 +00:00
Merge pull request #66225 from charrywanganthony/edit-o-json
Automatic merge from submit-queue (batch tested with PRs 66225, 66648, 65799, 66630, 66619). If you want to cherry-pick this change to another branch, please follow the instructions <a href="https://github.com/kubernetes/community/blob/master/contributors/devel/cherry-picks.md">here</a>. fix error binding of edit output format **What this PR does / why we need it**: `kubectl edit xxx/xxx -o json` won't print result in json format **Release note**: ```release-note NONE ```
This commit is contained in:
commit
1833aabcf2
@ -76,10 +76,10 @@ func NewCmdApplyEditLastApplied(f cmdutil.Factory, ioStreams genericclioptions.I
|
|||||||
|
|
||||||
// bind flag structs
|
// bind flag structs
|
||||||
o.RecordFlags.AddFlags(cmd)
|
o.RecordFlags.AddFlags(cmd)
|
||||||
|
o.PrintFlags.AddFlags(cmd)
|
||||||
|
|
||||||
usage := "to use to edit the resource"
|
usage := "to use to edit the resource"
|
||||||
cmdutil.AddFilenameOptionFlags(cmd, &o.FilenameOptions, usage)
|
cmdutil.AddFilenameOptionFlags(cmd, &o.FilenameOptions, usage)
|
||||||
cmd.Flags().StringVarP(&o.Output, "output", "o", o.Output, "Output format. One of: yaml|json.")
|
|
||||||
cmd.Flags().BoolVar(&o.WindowsLineEndings, "windows-line-endings", o.WindowsLineEndings,
|
cmd.Flags().BoolVar(&o.WindowsLineEndings, "windows-line-endings", o.WindowsLineEndings,
|
||||||
"Defaults to the line ending native to your platform.")
|
"Defaults to the line ending native to your platform.")
|
||||||
cmdutil.AddIncludeUninitializedFlag(cmd)
|
cmdutil.AddIncludeUninitializedFlag(cmd)
|
||||||
|
@ -209,7 +209,7 @@ func (o *CreateOptions) RunCreate(f cmdutil.Factory, cmd *cobra.Command) error {
|
|||||||
}
|
}
|
||||||
|
|
||||||
if o.EditBeforeCreate {
|
if o.EditBeforeCreate {
|
||||||
return RunEditOnCreate(f, o.RecordFlags, o.IOStreams, cmd, &o.FilenameOptions)
|
return RunEditOnCreate(f, o.PrintFlags, o.RecordFlags, o.IOStreams, cmd, &o.FilenameOptions)
|
||||||
}
|
}
|
||||||
schema, err := f.Validator(cmdutil.GetFlagBool(cmd, "validate"))
|
schema, err := f.Validator(cmdutil.GetFlagBool(cmd, "validate"))
|
||||||
if err != nil {
|
if err != nil {
|
||||||
@ -298,13 +298,13 @@ func (o *CreateOptions) raw(f cmdutil.Factory) error {
|
|||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func RunEditOnCreate(f cmdutil.Factory, recordFlags *genericclioptions.RecordFlags, ioStreams genericclioptions.IOStreams, cmd *cobra.Command, options *resource.FilenameOptions) error {
|
func RunEditOnCreate(f cmdutil.Factory, printFlags *genericclioptions.PrintFlags, recordFlags *genericclioptions.RecordFlags, ioStreams genericclioptions.IOStreams, cmd *cobra.Command, options *resource.FilenameOptions) error {
|
||||||
editOptions := editor.NewEditOptions(editor.EditBeforeCreateMode, ioStreams)
|
editOptions := editor.NewEditOptions(editor.EditBeforeCreateMode, ioStreams)
|
||||||
editOptions.FilenameOptions = *options
|
editOptions.FilenameOptions = *options
|
||||||
editOptions.ValidateOptions = cmdutil.ValidateOptions{
|
editOptions.ValidateOptions = cmdutil.ValidateOptions{
|
||||||
EnableValidation: cmdutil.GetFlagBool(cmd, "validate"),
|
EnableValidation: cmdutil.GetFlagBool(cmd, "validate"),
|
||||||
}
|
}
|
||||||
editOptions.Output = cmdutil.GetFlagString(cmd, "output")
|
editOptions.PrintFlags = printFlags
|
||||||
editOptions.ApplyAnnotation = cmdutil.GetFlagBool(cmd, cmdutil.ApplyAnnotationsFlag)
|
editOptions.ApplyAnnotation = cmdutil.GetFlagBool(cmd, cmdutil.ApplyAnnotationsFlag)
|
||||||
editOptions.RecordFlags = recordFlags
|
editOptions.RecordFlags = recordFlags
|
||||||
|
|
||||||
|
@ -6,7 +6,7 @@ args:
|
|||||||
outputFormat: yaml
|
outputFormat: yaml
|
||||||
namespace: myproject
|
namespace: myproject
|
||||||
expectedStdout:
|
expectedStdout:
|
||||||
- service/svc1 edited
|
- 'targetPort: 92'
|
||||||
expectedExitCode: 0
|
expectedExitCode: 0
|
||||||
steps:
|
steps:
|
||||||
- type: request
|
- type: request
|
||||||
|
@ -60,7 +60,6 @@ type EditOptions struct {
|
|||||||
PrintFlags *genericclioptions.PrintFlags
|
PrintFlags *genericclioptions.PrintFlags
|
||||||
ToPrinter func(string) (printers.ResourcePrinter, error)
|
ToPrinter func(string) (printers.ResourcePrinter, error)
|
||||||
|
|
||||||
Output string
|
|
||||||
OutputPatch bool
|
OutputPatch bool
|
||||||
WindowsLineEndings bool
|
WindowsLineEndings bool
|
||||||
|
|
||||||
@ -90,19 +89,61 @@ func NewEditOptions(editMode EditMode, ioStreams genericclioptions.IOStreams) *E
|
|||||||
|
|
||||||
PrintFlags: genericclioptions.NewPrintFlags("edited").WithTypeSetter(scheme.Scheme),
|
PrintFlags: genericclioptions.NewPrintFlags("edited").WithTypeSetter(scheme.Scheme),
|
||||||
|
|
||||||
|
editPrinterOptions: &editPrinterOptions{
|
||||||
|
// create new editor-specific PrintFlags, with all
|
||||||
|
// output flags disabled, except json / yaml
|
||||||
|
printFlags: (&genericclioptions.PrintFlags{
|
||||||
|
JSONYamlPrintFlags: genericclioptions.NewJSONYamlPrintFlags(),
|
||||||
|
}).WithDefaultOutput("yaml"),
|
||||||
|
ext: ".yaml",
|
||||||
|
addHeader: true,
|
||||||
|
},
|
||||||
|
|
||||||
WindowsLineEndings: goruntime.GOOS == "windows",
|
WindowsLineEndings: goruntime.GOOS == "windows",
|
||||||
|
|
||||||
Recorder: genericclioptions.NoopRecorder{},
|
Recorder: genericclioptions.NoopRecorder{},
|
||||||
|
|
||||||
IOStreams: ioStreams,
|
IOStreams: ioStreams,
|
||||||
Output: "yaml",
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
type editPrinterOptions struct {
|
type editPrinterOptions struct {
|
||||||
printer printers.ResourcePrinter
|
printFlags *genericclioptions.PrintFlags
|
||||||
ext string
|
ext string
|
||||||
addHeader bool
|
addHeader bool
|
||||||
|
}
|
||||||
|
|
||||||
|
func (e *editPrinterOptions) Complete(fromPrintFlags *genericclioptions.PrintFlags) error {
|
||||||
|
if e.printFlags == nil {
|
||||||
|
return fmt.Errorf("missing PrintFlags in editor printer options")
|
||||||
|
}
|
||||||
|
|
||||||
|
// bind output format from existing printflags
|
||||||
|
if fromPrintFlags != nil && len(*fromPrintFlags.OutputFormat) > 0 {
|
||||||
|
e.printFlags.OutputFormat = fromPrintFlags.OutputFormat
|
||||||
|
}
|
||||||
|
|
||||||
|
// prevent a commented header at the top of the user's
|
||||||
|
// default editor if presenting contents as json.
|
||||||
|
if *e.printFlags.OutputFormat == "json" {
|
||||||
|
e.addHeader = false
|
||||||
|
e.ext = ".json"
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
|
// we default to yaml if check above is false, as only json or yaml are supported
|
||||||
|
e.addHeader = true
|
||||||
|
e.ext = ".yaml"
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
|
func (e *editPrinterOptions) PrintObj(obj runtime.Object, out io.Writer) error {
|
||||||
|
p, err := e.printFlags.ToPrinter()
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
|
return p.PrintObj(obj, out)
|
||||||
}
|
}
|
||||||
|
|
||||||
// Complete completes all the required options
|
// Complete completes all the required options
|
||||||
@ -118,12 +159,8 @@ func (o *EditOptions) Complete(f cmdutil.Factory, args []string, cmd *cobra.Comm
|
|||||||
if o.EditMode != NormalEditMode && o.EditMode != EditBeforeCreateMode && o.EditMode != ApplyEditMode {
|
if o.EditMode != NormalEditMode && o.EditMode != EditBeforeCreateMode && o.EditMode != ApplyEditMode {
|
||||||
return fmt.Errorf("unsupported edit mode %q", o.EditMode)
|
return fmt.Errorf("unsupported edit mode %q", o.EditMode)
|
||||||
}
|
}
|
||||||
if o.Output != "" {
|
|
||||||
if o.Output != "yaml" && o.Output != "json" {
|
o.editPrinterOptions.Complete(o.PrintFlags)
|
||||||
return fmt.Errorf("invalid output format %s, only yaml|json supported", o.Output)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
o.editPrinterOptions = getPrinter(o.Output)
|
|
||||||
|
|
||||||
if o.OutputPatch && o.EditMode != NormalEditMode {
|
if o.OutputPatch && o.EditMode != NormalEditMode {
|
||||||
return fmt.Errorf("the edit mode doesn't support output the patch")
|
return fmt.Errorf("the edit mode doesn't support output the patch")
|
||||||
@ -225,7 +262,7 @@ func (o *EditOptions) Run() error {
|
|||||||
}
|
}
|
||||||
|
|
||||||
if !containsError {
|
if !containsError {
|
||||||
if err := o.editPrinterOptions.printer.PrintObj(originalObj, w); err != nil {
|
if err := o.editPrinterOptions.PrintObj(originalObj, w); err != nil {
|
||||||
return preservedFile(err, results.file, o.ErrOut)
|
return preservedFile(err, results.file, o.ErrOut)
|
||||||
}
|
}
|
||||||
original = buf.Bytes()
|
original = buf.Bytes()
|
||||||
@ -509,30 +546,6 @@ func encodeToJson(obj runtime.Unstructured) ([]byte, error) {
|
|||||||
return js, nil
|
return js, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func getPrinter(format string) *editPrinterOptions {
|
|
||||||
switch format {
|
|
||||||
case "json":
|
|
||||||
return &editPrinterOptions{
|
|
||||||
printer: &printers.JSONPrinter{},
|
|
||||||
ext: ".json",
|
|
||||||
addHeader: false,
|
|
||||||
}
|
|
||||||
case "yaml":
|
|
||||||
return &editPrinterOptions{
|
|
||||||
printer: &printers.YAMLPrinter{},
|
|
||||||
ext: ".yaml",
|
|
||||||
addHeader: true,
|
|
||||||
}
|
|
||||||
default:
|
|
||||||
// if format is not specified, use yaml as default
|
|
||||||
return &editPrinterOptions{
|
|
||||||
printer: &printers.YAMLPrinter{},
|
|
||||||
ext: ".yaml",
|
|
||||||
addHeader: true,
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
func (o *EditOptions) visitToPatch(originalInfos []*resource.Info, patchVisitor resource.Visitor, results *editResults) error {
|
func (o *EditOptions) visitToPatch(originalInfos []*resource.Info, patchVisitor resource.Visitor, results *editResults) error {
|
||||||
err := patchVisitor.Visit(func(info *resource.Info, incomingErr error) error {
|
err := patchVisitor.Visit(func(info *resource.Info, incomingErr error) error {
|
||||||
editObjUID, err := meta.NewAccessor().UID(info.Object)
|
editObjUID, err := meta.NewAccessor().UID(info.Object)
|
||||||
|
@ -25,6 +25,9 @@ import (
|
|||||||
)
|
)
|
||||||
|
|
||||||
func (f *JSONYamlPrintFlags) AllowedFormats() []string {
|
func (f *JSONYamlPrintFlags) AllowedFormats() []string {
|
||||||
|
if f == nil {
|
||||||
|
return []string{}
|
||||||
|
}
|
||||||
return []string{"json", "yaml"}
|
return []string{"json", "yaml"}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -41,6 +41,9 @@ func (f *NamePrintFlags) Complete(successTemplate string) error {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func (f *NamePrintFlags) AllowedFormats() []string {
|
func (f *NamePrintFlags) AllowedFormats() []string {
|
||||||
|
if f == nil {
|
||||||
|
return []string{}
|
||||||
|
}
|
||||||
return []string{"name"}
|
return []string{"name"}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user