MCPcopy
hub / github.com/1Panel-dev/KubePi / CopyFileFromPod

Method CopyFileFromPod

pkg/util/podtool/copyfrompod.go:8–54  ·  view source on GitHub ↗
(filePath string, destPath string)

Source from the content-addressed store, hash-verified

6 "os"
7)
8func (p *PodTool) CopyFileFromPod(filePath string, destPath string) error {
9 reader, outStream := io.Pipe()
10
11 p.ExecConfig = ExecConfig{
12 Command: []string{"cat", filePath},
13 Stdin: os.Stdin,
14 Stdout: outStream,
15 Stderr: os.Stderr,
16 NoPreserve: true,
17 }
18
19 err := p.Exec(Download)
20 if err != nil {
21 return err
22 }
23
24 file, err := os.OpenFile(destPath, os.O_RDWR|os.O_CREATE|os.O_APPEND, 0666)
25 if err != nil {
26 return err
27 }
28
29 r := bufio.NewReader(reader)
30 w := bufio.NewWriter(file)
31 size := 4 * 1024
32 buf := make([]byte, 4*1024)
33 for {
34 n, err := r.Read(buf)
35 if err != nil && err != io.EOF {
36 return err
37 }
38 if n == 0 {
39 break
40 }
41 _, err = w.Write(buf[:n])
42 if err != nil {
43 return err
44 }
45 if n < size {
46 break
47 }
48 }
49 err = w.Flush()
50 if err != nil {
51 return err
52 }
53 return err
54}
55func (p *PodTool) CopyFolderFromPod(filePath string, destPath string) error {
56 reader, outStream := io.Pipe()
57

Callers 1

DownloadFileMethod · 0.80

Calls 4

ExecMethod · 0.95
OpenFileMethod · 0.80
ReadMethod · 0.45
WriteMethod · 0.45

Tested by

no test coverage detected