-
Notifications
You must be signed in to change notification settings - Fork 13
/
sync.go
183 lines (160 loc) · 5.97 KB
/
sync.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
177
178
179
180
181
182
183
package main
import (
"fmt"
"path/filepath"
"github.com/Xuanwo/navvy"
"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 syncFlags struct {
checkMD5 bool
dryRun bool
existing bool
ignoreExisting bool
recursive bool
update bool
multipartFlags
inExcludeFlags
}
var syncFlag = syncFlags{}
// SyncCommand will handle sync command.
var SyncCommand = &cobra.Command{
Use: "sync <source-path> <dest-path>",
Short: i18n.Sprintf("sync between local directory and QS-Directory"),
Long: i18n.Sprintf(`qsctl sync between local directory and QS-Directory. The first path argument
is the source directory and second the destination directory.`),
Example: utils.AlignPrintWithColon(
i18n.Sprintf("Sync local directory to QS-Directory: qsctl sync . qs://bucket-name/dir/"),
i18n.Sprintf("Sync QS-Directory to local directory: qsctl sync qs://bucket-name/test/ test_local/"),
i18n.Sprintf("Sync directory recursively: qsctl sync qs://bucket-name/test/ test_local/ -r"),
i18n.Sprintf("Sync skip updating files that already exist on receiver: qsctl sync . qs://bucket-name/dir/ --ignore-existing"),
i18n.Sprintf("Only sync files that newer than files on receiver: qsctl sync . qs://bucket-name/dir/ --update"),
i18n.Sprintf("Only sync files that already exist on receiver: qsctl sync . qs://bucket-name/dir/ --existing"),
i18n.Sprintf("Show files that would sync (but not really do): qsctl sync . qs://bucket-name/dir/ --dry-run"),
),
Args: cobra.ExactArgs(2),
PreRunE: func(c *cobra.Command, args []string) error {
if err := validateSyncFlag(c, args); err != nil {
return err
}
if err := parseSyncFlag(); 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 := syncRun(cmd, args); err != nil {
i18n.Fprintf(cmd.OutOrStderr(), "Execute %s command error: %s\n", "sync", err.Error())
return err
}
return nil
},
}
func syncRun(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 || rootTask.GetDestinationType() != types.ObjectTypeDir {
return fmt.Errorf(i18n.Sprintf("both source and destination should be directories"))
}
t := task.NewSync(rootTask)
t.SetCheckMD5(syncFlag.checkMD5)
t.SetRecursive(syncFlag.recursive)
t.SetPartThreshold(syncFlag.partThreshold)
if syncFlag.partSize != 0 {
t.SetPartSize(syncFlag.partSize)
}
// set check functions
var fn []func(task navvy.Task) navvy.Task
if syncFlag.existing {
fn = append(fn, task.NewIsDestinationObjectExistTask)
}
if syncFlag.ignoreExisting {
fn = append(fn, task.NewIsDestinationObjectNotExistTask)
}
if syncFlag.update {
fn = append(fn, task.NewIsUpdateAtGreaterTask)
}
if syncFlag.excludeRegx != nil {
fn = append(fn, func(tt navvy.Task) navvy.Task {
st := task.NewIsSourcePathExcludeInclude(tt)
st.SetExcludeRegexp(syncFlag.excludeRegx)
st.SetIncludeRegexp(syncFlag.includeRegx)
return st
})
}
t.SetCheckTasks(fn)
if syncFlag.dryRun {
t.SetDryRunFunc(func(o *types.Object) {
i18n.Fprintf(c.OutOrStdout(), "%s\n", o.Name)
})
} else {
t.SetDryRunFunc(nil)
t.SetHandleObjCallback(func(o *types.Object) {
i18n.Fprintf(c.OutOrStdout(), "<%s> synced\n", o.Name)
})
}
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> and <%s> synced.\n",
filepath.Join(srcWorkDir, t.GetSourcePath()), filepath.Join(dstWorkDir, t.GetDestinationPath()))
return nil
}
func initSyncFlag() {
SyncCommand.Flags().BoolVarP(&syncFlag.dryRun, constants.DryRunFlag, "n", false,
i18n.Sprintf(`show what would have been transferred`))
SyncCommand.Flags().BoolVar(&syncFlag.existing, constants.ExistingFlag, false,
i18n.Sprintf(`skip creating new files in dest dirs`))
SyncCommand.Flags().BoolVar(&syncFlag.ignoreExisting, constants.IgnoreExistingFlag, false,
i18n.Sprintf(`skip updating files in dest dirs, only copy those not exist`))
SyncCommand.Flags().BoolVarP(&syncFlag.recursive, constants.RecursiveFlag, "r", false,
i18n.Sprintf(`recurse into sub directories`))
SyncCommand.Flags().BoolVarP(&syncFlag.update, constants.UpdateFlag, "u", false,
i18n.Sprintf(`skip files that are newer in dest dirs`))
SyncCommand.Flags().StringVar(&syncFlag.partThresholdStr, constants.PartThresholdFlag, "",
i18n.Sprintf("set threshold to enable multipart upload"))
SyncCommand.Flags().StringVar(&syncFlag.partSizeStr, constants.PartSizeFlag, "",
i18n.Sprintf("set part size for multipart upload"))
SyncCommand.Flags().StringVar(&syncFlag.excludeRegxStr, constants.ExcludeRegexp, "",
i18n.Sprintf("regular expression for files to exclude"))
SyncCommand.Flags().StringVar(&syncFlag.includeRegxStr, constants.IncludeRegexp, "",
i18n.Sprintf("regular expression for files to include (not work if exclude-regx not set)"))
}
func validateSyncFlag(_ *cobra.Command, _ []string) (err error) {
if syncFlag.existing && syncFlag.ignoreExisting {
return fmt.Errorf(i18n.Sprintf("both --existing and --ignore-existing are set, no files would be synced"))
}
return nil
}
func parseSyncFlag() error {
if err := syncFlag.multipartFlags.parse(); err != nil {
return err
}
if err := syncFlag.inExcludeFlags.parse(); err != nil {
return err
}
return nil
}
func resetSyncFlag() {
syncFlag = syncFlags{}
}
type syncShellHandler struct {
blankShellHandler
}
func (h syncShellHandler) postRun(_ error) {
resetSyncFlag()
}