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
d43f4bb3
Commit
d43f4bb3
authored
May 10, 2017
by
Shiyang Wang
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
refactor functions in editoptions.go to use less arguments
parent
3fbfafdd
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
with
18 additions
and
21 deletions
+18
-21
editoptions.go
pkg/kubectl/cmd/util/editor/editoptions.go
+18
-21
No files found.
pkg/kubectl/cmd/util/editor/editoptions.go
View file @
d43f4bb3
...
...
@@ -59,6 +59,7 @@ type EditOptions struct {
Mapper
meta
.
RESTMapper
ResourceMapper
*
resource
.
Mapper
OriginalResult
*
resource
.
Result
Encoder
runtime
.
Encoder
EditMode
EditMode
...
...
@@ -129,7 +130,7 @@ func (o *EditOptions) Complete(f cmdutil.Factory, out, errOut io.Writer, args []
o
.
Mapper
=
mapper
o
.
CmdNamespace
=
cmdNamespace
o
.
Encoder
=
f
.
JSONEncoder
()
o
.
f
=
f
// Set up writer
...
...
@@ -145,7 +146,6 @@ func (o *EditOptions) Validate() error {
}
func
(
o
*
EditOptions
)
Run
()
error
{
encoder
:=
o
.
f
.
JSONEncoder
()
edit
:=
NewDefaultEditor
(
o
.
f
.
EditorEnvs
())
// editFn is invoked for each edit session (once with a list for normal edit, once for each individual resource in a edit-on-create invocation)
editFn
:=
func
(
infos
[]
*
resource
.
Info
)
error
{
...
...
@@ -273,15 +273,15 @@ func (o *EditOptions) Run() error {
}
// iterate through all items to apply annotations
if
err
:=
visitAnnotation
(
o
.
ApplyAnnotation
,
o
.
Record
,
o
.
ChangeCause
,
updatedVisitor
,
encode
r
);
err
!=
nil
{
if
err
:=
o
.
visitAnnotation
(
updatedVisito
r
);
err
!=
nil
{
return
preservedFile
(
err
,
file
,
o
.
ErrOut
)
}
switch
o
.
EditMode
{
case
NormalEditMode
:
err
=
visitToPatch
(
infos
,
updatedVisitor
,
o
.
Mapper
,
encoder
,
o
.
Out
,
o
.
ErrOut
,
&
results
)
err
=
o
.
visitToPatch
(
infos
,
updatedVisitor
,
&
results
)
case
EditBeforeCreateMode
:
err
=
visitToCreate
(
updatedVisitor
,
o
.
Mapper
,
o
.
Out
)
err
=
o
.
visitToCreate
(
updatedVisitor
)
default
:
err
=
fmt
.
Errorf
(
"unsupported edit mode %q"
,
o
.
EditMode
)
}
...
...
@@ -360,12 +360,9 @@ func getPrinter(format string) *editPrinterOptions {
}
}
func
visitToPatch
(
func
(
o
*
EditOptions
)
visitToPatch
(
originalInfos
[]
*
resource
.
Info
,
patchVisitor
resource
.
Visitor
,
mapper
meta
.
RESTMapper
,
encoder
runtime
.
Encoder
,
out
,
errOut
io
.
Writer
,
results
*
editResults
,
)
error
{
err
:=
patchVisitor
.
Visit
(
func
(
info
*
resource
.
Info
,
incomingErr
error
)
error
{
...
...
@@ -389,11 +386,11 @@ func visitToPatch(
return
fmt
.
Errorf
(
"no original object found for %#v"
,
info
.
Object
)
}
originalSerialization
,
err
:=
runtime
.
Encode
(
e
ncoder
,
originalInfo
.
Object
)
originalSerialization
,
err
:=
runtime
.
Encode
(
o
.
E
ncoder
,
originalInfo
.
Object
)
if
err
!=
nil
{
return
err
}
editedSerialization
,
err
:=
runtime
.
Encode
(
e
ncoder
,
info
.
Object
)
editedSerialization
,
err
:=
runtime
.
Encode
(
o
.
E
ncoder
,
info
.
Object
)
if
err
!=
nil
{
return
err
}
...
...
@@ -411,7 +408,7 @@ func visitToPatch(
if
reflect
.
DeepEqual
(
originalJS
,
editedJS
)
{
// no edit, so just skip it.
cmdutil
.
PrintSuccess
(
mapper
,
false
,
o
ut
,
info
.
Mapping
.
Resource
,
info
.
Name
,
false
,
"skipped"
)
cmdutil
.
PrintSuccess
(
o
.
Mapper
,
false
,
o
.
O
ut
,
info
.
Mapping
.
Resource
,
info
.
Name
,
false
,
"skipped"
)
return
nil
}
...
...
@@ -457,38 +454,38 @@ func visitToPatch(
patched
,
err
:=
resource
.
NewHelper
(
info
.
Client
,
info
.
Mapping
)
.
Patch
(
info
.
Namespace
,
info
.
Name
,
patchType
,
patch
)
if
err
!=
nil
{
fmt
.
Fprintln
(
e
rrOut
,
results
.
addError
(
err
,
info
))
fmt
.
Fprintln
(
o
.
E
rrOut
,
results
.
addError
(
err
,
info
))
return
nil
}
info
.
Refresh
(
patched
,
true
)
cmdutil
.
PrintSuccess
(
mapper
,
false
,
o
ut
,
info
.
Mapping
.
Resource
,
info
.
Name
,
false
,
"edited"
)
cmdutil
.
PrintSuccess
(
o
.
Mapper
,
false
,
o
.
O
ut
,
info
.
Mapping
.
Resource
,
info
.
Name
,
false
,
"edited"
)
return
nil
})
return
err
}
func
visitToCreate
(
createVisitor
resource
.
Visitor
,
mapper
meta
.
RESTMapper
,
out
io
.
Write
r
)
error
{
func
(
o
*
EditOptions
)
visitToCreate
(
createVisitor
resource
.
Visito
r
)
error
{
err
:=
createVisitor
.
Visit
(
func
(
info
*
resource
.
Info
,
incomingErr
error
)
error
{
if
err
:=
resource
.
CreateAndRefresh
(
info
);
err
!=
nil
{
return
err
}
cmdutil
.
PrintSuccess
(
mapper
,
false
,
o
ut
,
info
.
Mapping
.
Resource
,
info
.
Name
,
false
,
"created"
)
cmdutil
.
PrintSuccess
(
o
.
Mapper
,
false
,
o
.
O
ut
,
info
.
Mapping
.
Resource
,
info
.
Name
,
false
,
"created"
)
return
nil
})
return
err
}
func
visitAnnotation
(
applyAnnotation
,
record
bool
,
changeCause
string
,
annotationVisitor
resource
.
Visitor
,
encoder
runtime
.
Encode
r
)
error
{
func
(
o
*
EditOptions
)
visitAnnotation
(
annotationVisitor
resource
.
Visito
r
)
error
{
// iterate through all items to apply annotations
err
:=
annotationVisitor
.
Visit
(
func
(
info
*
resource
.
Info
,
incomingErr
error
)
error
{
// put configuration annotation in "updates"
if
a
pplyAnnotation
{
if
err
:=
kubectl
.
CreateOrUpdateAnnotation
(
true
,
info
,
e
ncoder
);
err
!=
nil
{
if
o
.
A
pplyAnnotation
{
if
err
:=
kubectl
.
CreateOrUpdateAnnotation
(
true
,
info
,
o
.
E
ncoder
);
err
!=
nil
{
return
err
}
}
if
r
ecord
||
cmdutil
.
ContainsChangeCause
(
info
)
{
if
err
:=
cmdutil
.
RecordChangeCause
(
info
.
Object
,
c
hangeCause
);
err
!=
nil
{
if
o
.
R
ecord
||
cmdutil
.
ContainsChangeCause
(
info
)
{
if
err
:=
cmdutil
.
RecordChangeCause
(
info
.
Object
,
o
.
C
hangeCause
);
err
!=
nil
{
return
err
}
}
...
...
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