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
52282b1a
Commit
52282b1a
authored
Jul 24, 2015
by
Mike Danese
Browse files
Options
Browse Files
Download
Plain Diff
Merge pull request #11300 from mwielgus/kubectl_replace_tempfile
Dump stdin to a temporary file in kubectl replace --force
parents
de6d8705
866bd7b4
Hide whitespace changes
Inline
Side-by-side
Showing
4 changed files
with
67 additions
and
1 deletion
+67
-1
replace.go
pkg/kubectl/cmd/replace.go
+18
-0
helpers.go
pkg/kubectl/cmd/util/helpers.go
+25
-0
helpers_test.go
pkg/kubectl/cmd/util/helpers_test.go
+23
-0
builder.go
pkg/kubectl/resource/builder.go
+1
-1
No files found.
pkg/kubectl/cmd/replace.go
View file @
52282b1a
...
...
@@ -19,7 +19,9 @@ package cmd
import
(
"fmt"
"io"
"io/ioutil"
"os"
"path/filepath"
"github.com/spf13/cobra"
...
...
@@ -131,6 +133,22 @@ func forceReplace(f *cmdutil.Factory, out io.Writer, cmd *cobra.Command, args []
return
err
}
for
i
,
filename
:=
range
filenames
{
if
filename
==
"-"
{
tempDir
,
err
:=
ioutil
.
TempDir
(
""
,
"kubectl_replace_"
)
if
err
!=
nil
{
return
err
}
defer
os
.
RemoveAll
(
tempDir
)
tempFilename
:=
filepath
.
Join
(
tempDir
,
"resource.stdin"
)
err
=
cmdutil
.
DumpReaderToFile
(
os
.
Stdin
,
tempFilename
)
if
err
!=
nil
{
return
err
}
filenames
[
i
]
=
tempFilename
}
}
mapper
,
typer
:=
f
.
Object
()
r
:=
resource
.
NewBuilder
(
mapper
,
typer
,
f
.
ClientMapperForCommand
())
.
ContinueOnError
()
.
...
...
pkg/kubectl/cmd/util/helpers.go
View file @
52282b1a
...
...
@@ -406,3 +406,28 @@ func Merge(dst runtime.Object, fragment, kind string) (runtime.Object, error) {
}
return
out
,
nil
}
// DumpReaderToFile writes all data from the given io.Reader to the specified file
// (usually for temporary use).
func
DumpReaderToFile
(
reader
io
.
Reader
,
filename
string
)
error
{
f
,
err
:=
os
.
OpenFile
(
filename
,
os
.
O_WRONLY
|
os
.
O_CREATE
|
os
.
O_TRUNC
,
0600
)
defer
f
.
Close
()
if
err
!=
nil
{
return
err
}
buffer
:=
make
([]
byte
,
1024
)
for
{
count
,
err
:=
reader
.
Read
(
buffer
)
if
err
==
io
.
EOF
{
break
}
if
err
!=
nil
{
return
err
}
_
,
err
=
f
.
Write
(
buffer
[
:
count
])
if
err
!=
nil
{
return
err
}
}
return
nil
}
pkg/kubectl/cmd/util/helpers_test.go
View file @
52282b1a
...
...
@@ -22,6 +22,7 @@ import (
"net/http"
"net/http/httptest"
"reflect"
"strings"
"syscall"
"testing"
...
...
@@ -298,3 +299,25 @@ func TestCheckInvalidErr(t *testing.T) {
}
}
}
func
TestDumpReaderToFile
(
t
*
testing
.
T
)
{
testString
:=
"TEST STRING"
tempFile
,
err
:=
ioutil
.
TempFile
(
""
,
"hlpers_test_dump_"
)
if
err
!=
nil
{
t
.
Errorf
(
"unexpected error setting up a temporary file %v"
,
err
)
}
defer
syscall
.
Unlink
(
tempFile
.
Name
())
defer
tempFile
.
Close
()
err
=
DumpReaderToFile
(
strings
.
NewReader
(
testString
),
tempFile
.
Name
())
if
err
!=
nil
{
t
.
Errorf
(
"error in DumpReaderToFile: %v"
,
err
)
}
data
,
err
:=
ioutil
.
ReadFile
(
tempFile
.
Name
())
if
err
!=
nil
{
t
.
Errorf
(
"error when reading %s: %v"
,
tempFile
,
err
)
}
stringData
:=
string
(
data
)
if
stringData
!=
testString
{
t
.
Fatalf
(
"Wrong file content %s != %s"
,
testString
,
stringData
)
}
}
pkg/kubectl/resource/builder.go
View file @
52282b1a
...
...
@@ -164,7 +164,7 @@ func (b *Builder) Path(paths ...string) *Builder {
continue
}
visitors
,
err
:=
ExpandPathsToFileVisitors
(
b
.
mapper
,
p
,
false
,
[]
string
{
".json"
,
".yaml"
,
".yml"
},
b
.
continueOnError
,
b
.
schema
)
visitors
,
err
:=
ExpandPathsToFileVisitors
(
b
.
mapper
,
p
,
false
,
[]
string
{
".json"
,
".
stdin"
,
".
yaml"
,
".yml"
},
b
.
continueOnError
,
b
.
schema
)
if
err
!=
nil
{
b
.
errs
=
append
(
b
.
errs
,
fmt
.
Errorf
(
"error reading %q: %v"
,
p
,
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