-
Notifications
You must be signed in to change notification settings - Fork 13
/
cp.go
176 lines (153 loc) · 4.78 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
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
package main
import (
"fmt"
"path/filepath"
"github.com/aos-dev/go-storage/v2/types"
"github.com/qingstor/noah/task"
"github.com/spf13/cobra"
"github.com/qingstor/qsctl/v2/cmd/qsctl/taskutils"
"github.com/qingstor/qsctl/v2/constants"
"github.com/qingstor/qsctl/v2/pkg/i18n"
"github.com/qingstor/qsctl/v2/utils"
)
type cpFlags struct {
checkMD5 bool
expectSize string
maximumMemoryContent string
recursive bool
multipartFlags
}
var cpFlag = cpFlags{}
// CpCommand will handle copy command.
var CpCommand = &cobra.Command{
Use: "cp <source-path> <dest-path>",
Short: i18n.Sprintf("copy from/to qingstor"),
Long: i18n.Sprintf("qsctl cp can copy file/folder/stdin to qingstor or copy qingstor objects to local/stdout"),
Example: utils.AlignPrintWithColon(
i18n.Sprintf("Copy file: qsctl cp /path/to/file qs://prefix/a"),
i18n.Sprintf("Copy folder: qsctl cp /path/to/folder qs://prefix/a/ -r"),
i18n.Sprintf("Copy all files in folder: qsctl cp /path/to/folder/ qs://prefix/a/ -r"),
i18n.Sprintf("Read from stdin: cat /path/to/file | qsctl cp - qs://prefix/stdin"),
i18n.Sprintf("Write to stdout: qsctl cp qs://prefix/b - > /path/to/file"),
),
Args: cobra.ExactArgs(2),
PreRunE: func(c *cobra.Command, args []string) error {
c.SilenceErrors = false // reset runtime error show in preRun
if err := parseCpFlag(); err != nil {
return err
}
return nil
},
RunE: func(cmd *cobra.Command, args []string) error {
cmd.SilenceErrors = true // handle runtime errors with i18n, do not show error
if err := cpRun(cmd, args); err != nil {
i18n.Fprintf(cmd.OutOrStderr(), "Execute %s command error: %s\n", "cp", err.Error())
return err
}
return nil
},
}
func initCpFlag() {
CpCommand.PersistentFlags().StringVar(&cpFlag.expectSize,
constants.ExpectSizeFlag,
"",
i18n.Sprintf(`expected size of the input file
accept: 100MB, 1.8G
(only used and required for input from stdin)`),
)
CpCommand.PersistentFlags().StringVar(&cpFlag.maximumMemoryContent,
constants.MaximumMemoryContentFlag,
"",
i18n.Sprintf(`maximum content loaded in memory
(only used for input from stdin)`),
)
CpCommand.Flags().BoolVarP(&cpFlag.recursive,
constants.RecursiveFlag,
"r",
false,
i18n.Sprintf("copy directory recursively"),
)
CpCommand.Flags().StringVar(&cpFlag.partThresholdStr,
constants.PartThresholdFlag,
"",
i18n.Sprintf("set threshold to enable multipart upload"),
)
CpCommand.Flags().StringVar(&cpFlag.partSizeStr,
constants.PartSizeFlag,
"",
i18n.Sprintf("set part size for multipart upload"),
)
}
func cpRun(c *cobra.Command, args []string) (err error) {
silenceUsage(c) // silence usage when handled error returns
rootTask := taskutils.NewBetweenStorageTask(10)
srcWorkDir, dstWorkDir, err := utils.ParseBetweenStorageInput(rootTask, args[0], args[1])
if err != nil {
return
}
if rootTask.GetSourceType() == types.ObjectTypeDir && !cpFlag.recursive {
return fmt.Errorf(i18n.Sprintf("-r is required to copy a directory"))
}
if cpFlag.recursive && rootTask.GetSourceType() != types.ObjectTypeDir {
return fmt.Errorf(i18n.Sprintf("src should be a directory while -r is set"))
}
if rootTask.GetSourceType() == types.ObjectTypeDir &&
rootTask.GetDestinationType() != types.ObjectTypeDir {
return fmt.Errorf(i18n.Sprintf("cannot copy a directory to a non-directory dest"))
}
if cpFlag.recursive {
t := task.NewCopyDir(rootTask)
t.SetCheckMD5(cpFlag.checkMD5)
t.SetPartThreshold(cpFlag.partThreshold)
if cpFlag.partSize != 0 {
t.SetPartSize(cpFlag.partSize)
}
t.SetHandleObjCallback(func(o *types.Object) {
i18n.Fprintf(c.OutOrStdout(), "<%s> copied\n", o.Name)
})
t.SetCheckTasks(nil)
t.Run(c.Context())
if t.GetFault().HasError() {
return t.GetFault()
}
if h := taskutils.HandlerFromContext(c.Context()); h != nil {
h.WaitProgress()
}
i18n.Fprintf(c.OutOrStdout(), "Dir <%s> copied to <%s>.\n",
filepath.Join(srcWorkDir, t.GetSourcePath()), filepath.Join(dstWorkDir, t.GetDestinationPath()))
return nil
}
t := task.NewCopyFile(rootTask)
t.SetCheckMD5(cpFlag.checkMD5)
t.SetPartThreshold(cpFlag.partThreshold)
if cpFlag.partSize != 0 {
t.SetPartSize(cpFlag.partSize)
}
t.SetCheckTasks(nil)
t.Run(c.Context())
if t.GetFault().HasError() {
return t.GetFault()
}
if h := taskutils.HandlerFromContext(c.Context()); h != nil {
h.WaitProgress()
}
i18n.Fprintf(c.OutOrStdout(), "File <%s> copied to <%s>.\n",
filepath.Join(srcWorkDir, t.GetSourcePath()), filepath.Join(dstWorkDir, t.GetDestinationPath()))
return
}
func parseCpFlag() error {
if err := cpFlag.multipartFlags.parse(); err != nil {
resetCpFlag()
return err
}
return nil
}
func resetCpFlag() {
cpFlag = cpFlags{}
}
type cpShellHandler struct {
blankShellHandler
}
func (h cpShellHandler) postRun(_ error) {
resetCpFlag()
}