mirror of
https://github.com/k3s-io/kubernetes.git
synced 2025-07-23 11:50:44 +00:00
kubectl supports copying files with wild card
This commit is contained in:
parent
37b9bd36f7
commit
6576e1f790
@ -338,7 +338,12 @@ func makeTar(srcPath, destPath string, writer io.Writer) error {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func recursiveTar(srcBase, srcFile, destBase, destFile string, tw *tar.Writer) error {
|
func recursiveTar(srcBase, srcFile, destBase, destFile string, tw *tar.Writer) error {
|
||||||
fpath := path.Join(srcBase, srcFile)
|
srcPath := path.Join(srcBase, srcFile)
|
||||||
|
matchedPaths, err := filepath.Glob(srcPath)
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
for _, fpath := range matchedPaths {
|
||||||
stat, err := os.Lstat(fpath)
|
stat, err := os.Lstat(fpath)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
@ -398,6 +403,7 @@ func recursiveTar(srcBase, srcFile, destBase, destFile string, tw *tar.Writer) e
|
|||||||
}
|
}
|
||||||
return f.Close()
|
return f.Close()
|
||||||
}
|
}
|
||||||
|
}
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -46,6 +46,7 @@ type FileType int
|
|||||||
const (
|
const (
|
||||||
RegularFile FileType = 0
|
RegularFile FileType = 0
|
||||||
SymLink FileType = 1
|
SymLink FileType = 1
|
||||||
|
RegexFile FileType = 2
|
||||||
)
|
)
|
||||||
|
|
||||||
func TestExtractFileSpec(t *testing.T) {
|
func TestExtractFileSpec(t *testing.T) {
|
||||||
@ -209,6 +210,7 @@ func TestTarUntar(t *testing.T) {
|
|||||||
|
|
||||||
files := []struct {
|
files := []struct {
|
||||||
name string
|
name string
|
||||||
|
nameList []string
|
||||||
data string
|
data string
|
||||||
fileType FileType
|
fileType FileType
|
||||||
}{
|
}{
|
||||||
@ -237,6 +239,12 @@ func TestTarUntar(t *testing.T) {
|
|||||||
data: "/tmp/gakki",
|
data: "/tmp/gakki",
|
||||||
fileType: SymLink,
|
fileType: SymLink,
|
||||||
},
|
},
|
||||||
|
{
|
||||||
|
name: "blah*",
|
||||||
|
nameList: []string{"blah1", "blah2"},
|
||||||
|
data: "regexp file name",
|
||||||
|
fileType: RegexFile,
|
||||||
|
},
|
||||||
}
|
}
|
||||||
|
|
||||||
for _, file := range files {
|
for _, file := range files {
|
||||||
@ -245,26 +253,19 @@ func TestTarUntar(t *testing.T) {
|
|||||||
t.Fatalf("unexpected error: %v", err)
|
t.Fatalf("unexpected error: %v", err)
|
||||||
}
|
}
|
||||||
if file.fileType == RegularFile {
|
if file.fileType == RegularFile {
|
||||||
f, err := os.Create(filepath)
|
createTmpFile(t, filepath, file.data)
|
||||||
if err != nil {
|
|
||||||
t.Fatalf("unexpected error: %v", err)
|
|
||||||
}
|
|
||||||
defer f.Close()
|
|
||||||
if _, err := io.Copy(f, bytes.NewBuffer([]byte(file.data))); err != nil {
|
|
||||||
t.Fatalf("unexpected error: %v", err)
|
|
||||||
}
|
|
||||||
if err := f.Close(); err != nil {
|
|
||||||
t.Fatal(err)
|
|
||||||
}
|
|
||||||
} else if file.fileType == SymLink {
|
} else if file.fileType == SymLink {
|
||||||
err := os.Symlink(file.data, filepath)
|
err := os.Symlink(file.data, filepath)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
t.Fatalf("unexpected error: %v", err)
|
t.Fatalf("unexpected error: %v", err)
|
||||||
}
|
}
|
||||||
|
} else if file.fileType == RegexFile {
|
||||||
|
for _, fileName := range file.nameList {
|
||||||
|
createTmpFile(t, path.Join(dir, fileName), file.data)
|
||||||
|
}
|
||||||
} else {
|
} else {
|
||||||
t.Fatalf("unexpected file type: %v", file)
|
t.Fatalf("unexpected file type: %v", file)
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
writer := &bytes.Buffer{}
|
writer := &bytes.Buffer{}
|
||||||
@ -282,22 +283,7 @@ func TestTarUntar(t *testing.T) {
|
|||||||
filePath := filepath.Join(absPath, file.name)
|
filePath := filepath.Join(absPath, file.name)
|
||||||
|
|
||||||
if file.fileType == RegularFile {
|
if file.fileType == RegularFile {
|
||||||
f, err := os.Open(filePath)
|
cmpFileData(t, filePath, file.data)
|
||||||
if err != nil {
|
|
||||||
t.Fatalf("unexpected error: %v", err)
|
|
||||||
}
|
|
||||||
|
|
||||||
defer f.Close()
|
|
||||||
buff := &bytes.Buffer{}
|
|
||||||
if _, err := io.Copy(buff, f); err != nil {
|
|
||||||
t.Fatal(err)
|
|
||||||
}
|
|
||||||
if err := f.Close(); err != nil {
|
|
||||||
t.Fatal(err)
|
|
||||||
}
|
|
||||||
if file.data != string(buff.Bytes()) {
|
|
||||||
t.Fatalf("expected: %s, saw: %s", file.data, string(buff.Bytes()))
|
|
||||||
}
|
|
||||||
} else if file.fileType == SymLink {
|
} else if file.fileType == SymLink {
|
||||||
dest, err := os.Readlink(filePath)
|
dest, err := os.Readlink(filePath)
|
||||||
|
|
||||||
@ -308,6 +294,10 @@ func TestTarUntar(t *testing.T) {
|
|||||||
if file.data != dest {
|
if file.data != dest {
|
||||||
t.Fatalf("expected: %s, saw: %s", file.data, dest)
|
t.Fatalf("expected: %s, saw: %s", file.data, dest)
|
||||||
}
|
}
|
||||||
|
} else if file.fileType == RegexFile {
|
||||||
|
for _, fileName := range file.nameList {
|
||||||
|
cmpFileData(t, path.Join(dir, fileName), file.data)
|
||||||
|
}
|
||||||
} else {
|
} else {
|
||||||
t.Fatalf("unexpected file type: %v", file)
|
t.Fatalf("unexpected file type: %v", file)
|
||||||
}
|
}
|
||||||
@ -362,17 +352,7 @@ func TestCopyToLocalFileOrDir(t *testing.T) {
|
|||||||
t.Errorf("unexpected error: %v", err)
|
t.Errorf("unexpected error: %v", err)
|
||||||
t.FailNow()
|
t.FailNow()
|
||||||
}
|
}
|
||||||
srcFile, err := os.Create(srcFilePath)
|
createTmpFile(t, srcFilePath, file.data)
|
||||||
if err != nil {
|
|
||||||
t.Errorf("unexpected error: %v", err)
|
|
||||||
t.FailNow()
|
|
||||||
}
|
|
||||||
defer srcFile.Close()
|
|
||||||
|
|
||||||
if _, err := io.Copy(srcFile, bytes.NewBuffer([]byte(file.data))); err != nil {
|
|
||||||
t.Errorf("unexpected error: %v", err)
|
|
||||||
t.FailNow()
|
|
||||||
}
|
|
||||||
if file.destDirExists {
|
if file.destDirExists {
|
||||||
if err := os.MkdirAll(destPath, 0755); err != nil {
|
if err := os.MkdirAll(destPath, 0755); err != nil {
|
||||||
t.Errorf("unexpected error: %v", err)
|
t.Errorf("unexpected error: %v", err)
|
||||||
@ -400,7 +380,6 @@ func TestCopyToLocalFileOrDir(t *testing.T) {
|
|||||||
t.Errorf("unexpected error: %v", err)
|
t.Errorf("unexpected error: %v", err)
|
||||||
t.FailNow()
|
t.FailNow()
|
||||||
}
|
}
|
||||||
|
|
||||||
actualDestFilePath := destPath
|
actualDestFilePath := destPath
|
||||||
if file.destDirExists {
|
if file.destDirExists {
|
||||||
actualDestFilePath = filepath.Join(destPath, filepath.Base(srcFilePath))
|
actualDestFilePath = filepath.Join(destPath, filepath.Base(srcFilePath))
|
||||||
@ -409,17 +388,7 @@ func TestCopyToLocalFileOrDir(t *testing.T) {
|
|||||||
if err != nil && os.IsNotExist(err) {
|
if err != nil && os.IsNotExist(err) {
|
||||||
t.Errorf("expecting %s exists, but actually it's missing", actualDestFilePath)
|
t.Errorf("expecting %s exists, but actually it's missing", actualDestFilePath)
|
||||||
}
|
}
|
||||||
destFile, err := os.Open(actualDestFilePath)
|
cmpFileData(t, actualDestFilePath, file.data)
|
||||||
if err != nil {
|
|
||||||
t.Errorf("unexpected error: %v", err)
|
|
||||||
t.FailNow()
|
|
||||||
}
|
|
||||||
defer destFile.Close()
|
|
||||||
buff := &bytes.Buffer{}
|
|
||||||
io.Copy(buff, destFile)
|
|
||||||
if file.data != string(buff.Bytes()) {
|
|
||||||
t.Errorf("expected: %s, actual: %s", file.data, string(buff.Bytes()))
|
|
||||||
}
|
|
||||||
}()
|
}()
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -470,16 +439,7 @@ func TestTarDestinationName(t *testing.T) {
|
|||||||
t.Errorf("unexpected error: %v", err)
|
t.Errorf("unexpected error: %v", err)
|
||||||
t.FailNow()
|
t.FailNow()
|
||||||
}
|
}
|
||||||
f, err := os.Create(filepath)
|
createTmpFile(t, filepath, file.data)
|
||||||
if err != nil {
|
|
||||||
t.Errorf("unexpected error: %v", err)
|
|
||||||
t.FailNow()
|
|
||||||
}
|
|
||||||
defer f.Close()
|
|
||||||
if _, err := io.Copy(f, bytes.NewBuffer([]byte(file.data))); err != nil {
|
|
||||||
t.Errorf("unexpected error: %v", err)
|
|
||||||
t.FailNow()
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
reader, writer := io.Pipe()
|
reader, writer := io.Pipe()
|
||||||
@ -743,3 +703,36 @@ func TestValidate(t *testing.T) {
|
|||||||
})
|
})
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func createTmpFile(t *testing.T, filepath, data string) {
|
||||||
|
f, err := os.Create(filepath)
|
||||||
|
if err != nil {
|
||||||
|
t.Fatalf("unexpected error: %v", err)
|
||||||
|
}
|
||||||
|
defer f.Close()
|
||||||
|
if _, err := io.Copy(f, bytes.NewBuffer([]byte(data))); err != nil {
|
||||||
|
t.Fatalf("unexpected error: %v", err)
|
||||||
|
}
|
||||||
|
if err := f.Close(); err != nil {
|
||||||
|
t.Fatal(err)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func cmpFileData(t *testing.T, filePath, data string) {
|
||||||
|
f, err := os.Open(filePath)
|
||||||
|
if err != nil {
|
||||||
|
t.Fatalf("unexpected error: %v", err)
|
||||||
|
}
|
||||||
|
|
||||||
|
defer f.Close()
|
||||||
|
buff := &bytes.Buffer{}
|
||||||
|
if _, err := io.Copy(buff, f); err != nil {
|
||||||
|
t.Fatal(err)
|
||||||
|
}
|
||||||
|
if err := f.Close(); err != nil {
|
||||||
|
t.Fatal(err)
|
||||||
|
}
|
||||||
|
if data != string(buff.Bytes()) {
|
||||||
|
t.Fatalf("expected: %s, saw: %s", data, string(buff.Bytes()))
|
||||||
|
}
|
||||||
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user