Skip to content
Projects
Groups
Snippets
Help
This project
Loading...
Sign in / Register
Toggle navigation
K
k3s
Project
Project
Details
Activity
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Board
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Registry
Registry
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
Jacklull
k3s
Commits
5e79a25e
Unverified
Commit
5e79a25e
authored
Jul 16, 2018
by
juanvallejo
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
add support for "success" output for edit command
Adds support for the "success" printer in the "edit" command, while ensuring outputFormat constraints for the in-editor printer.
parent
a87b82c0
Hide whitespace changes
Inline
Side-by-side
Showing
3 changed files
with
55 additions
and
34 deletions
+55
-34
editoptions.go
pkg/kubectl/cmd/util/editor/editoptions.go
+49
-34
json_yaml_flags.go
pkg/kubectl/genericclioptions/json_yaml_flags.go
+3
-0
name_flags.go
pkg/kubectl/genericclioptions/name_flags.go
+3
-0
No files found.
pkg/kubectl/cmd/util/editor/editoptions.go
View file @
5e79a25e
...
@@ -89,6 +89,16 @@ func NewEditOptions(editMode EditMode, ioStreams genericclioptions.IOStreams) *E
...
@@ -89,6 +89,16 @@ 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
{},
...
@@ -98,9 +108,42 @@ func NewEditOptions(editMode EditMode, ioStreams genericclioptions.IOStreams) *E
...
@@ -98,9 +108,42 @@ func NewEditOptions(editMode EditMode, ioStreams genericclioptions.IOStreams) *E
}
}
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
...
@@ -116,12 +159,8 @@ func (o *EditOptions) Complete(f cmdutil.Factory, args []string, cmd *cobra.Comm
...
@@ -116,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
.
PrintFlags
.
OutputFormat
!=
""
{
if
*
o
.
PrintFlags
.
OutputFormat
!=
"yaml"
&&
*
o
.
PrintFlags
.
OutputFormat
!=
"json"
{
o
.
editPrinterOptions
.
Complete
(
o
.
PrintFlags
)
return
fmt
.
Errorf
(
"invalid output format %s, only yaml|json supported"
,
*
o
.
PrintFlags
.
OutputFormat
)
}
}
o
.
editPrinterOptions
=
getPrinter
(
*
o
.
PrintFlags
.
OutputFormat
)
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"
)
...
@@ -223,7 +262,7 @@ func (o *EditOptions) Run() error {
...
@@ -223,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
()
...
@@ -507,30 +546,6 @@ func encodeToJson(obj runtime.Unstructured) ([]byte, error) {
...
@@ -507,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
)
...
...
pkg/kubectl/genericclioptions/json_yaml_flags.go
View file @
5e79a25e
...
@@ -25,6 +25,9 @@ import (
...
@@ -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"
}
}
}
...
...
pkg/kubectl/genericclioptions/name_flags.go
View file @
5e79a25e
...
@@ -41,6 +41,9 @@ func (f *NamePrintFlags) Complete(successTemplate string) error {
...
@@ -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"
}
}
}
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment