/
cp.go
73 lines (57 loc) · 1.73 KB
/
cp.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
// Package cmd contains all the cobra commands for the CLI application.
package cmd
import (
"fmt"
"path/filepath"
"strings"
"github.com/89luca89/lilipod/pkg/containerutils"
"github.com/89luca89/lilipod/pkg/fileutils"
"github.com/89luca89/lilipod/pkg/logging"
"github.com/89luca89/lilipod/pkg/procutils"
"github.com/spf13/cobra"
)
// NewCpCommand will copy a file to or from a container.
func NewCpCommand() *cobra.Command {
cpCommand := &cobra.Command{
Use: "cp [options] [container:]SRC_PATH [container:]DEST_PATH",
Short: "Copy files/folders between a container and the local filesystem",
PreRunE: logging.Init,
RunE: cp,
SilenceUsage: true,
SilenceErrors: true,
TraverseChildren: true,
}
cpCommand.Flags().SetInterspersed(false)
return cpCommand
}
func cp(cmd *cobra.Command, arguments []string) error {
if len(arguments) < 2 {
return cmd.Help()
}
success, err := procutils.EnsureFakeRoot(true)
if err != nil {
return err
}
if success {
return nil
}
src := arguments[0]
dest := arguments[1]
if strings.Contains(src, ":") {
container := strings.Split(src, ":")[0]
file := strings.Split(src, ":")[1]
src = filepath.Join(containerutils.GetRootfsDir(container), file)
if !fileutils.Exist(containerutils.GetDir(container)) {
return fmt.Errorf("container %s does not exist", container)
}
}
if strings.Contains(dest, ":") {
container := strings.Split(dest, ":")[0]
file := strings.Split(dest, ":")[1]
dest = filepath.Join(containerutils.GetRootfsDir(container), file)
if !fileutils.Exist(containerutils.GetDir(container)) {
return fmt.Errorf("container %s does not exist", container)
}
}
return fileutils.CopyFileContainer(src, dest)
}