-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathopt.go
More file actions
142 lines (117 loc) · 2.42 KB
/
opt.go
File metadata and controls
142 lines (117 loc) · 2.42 KB
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
package acp
import (
"os"
"path"
"github.com/sirupsen/logrus"
)
type source struct {
base string
path string
}
func (s *source) src() string {
return path.Join(s.base, s.path)
}
func (s *source) dst(dst string) string {
return path.Join(dst, s.path)
}
func (s *source) append(next string) *source {
return &source{base: s.base, path: path.Join(s.path, next)}
}
type option struct {
accurateJobs []*accurateJob
wildcardJobs []*wildcardJob
fromDevice *deviceOption
toDevice *deviceOption
createFlag int
withHash bool
logger *logrus.Logger
eventHanders []EventHandler
}
func newOption() *option {
return &option{
fromDevice: new(deviceOption),
toDevice: new(deviceOption),
createFlag: os.O_WRONLY | os.O_CREATE | os.O_EXCL,
}
}
func (o *option) check() error {
for _, job := range o.wildcardJobs {
if err := job.check(); err != nil {
return err
}
}
o.fromDevice.check()
o.toDevice.check()
if o.fromDevice.linear || o.toDevice.linear {
o.fromDevice.threads = 1
o.toDevice.threads = 1
}
if o.logger == nil {
o.logger = logrus.StandardLogger()
}
return nil
}
type Option func(*option) *option
type accurateJob struct {
src string
dsts []string
}
func AccurateJob(src string, dsts []string) Option {
return func(o *option) *option {
o.accurateJobs = append(o.accurateJobs, &accurateJob{src: path.Clean(src), dsts: dsts})
return o
}
}
func SetFromDevice(opts ...DeviceOption) Option {
return func(o *option) *option {
for _, opt := range opts {
if opt == nil {
continue
}
o.fromDevice = opt(o.fromDevice)
}
return o
}
}
func SetToDevice(opts ...DeviceOption) Option {
return func(o *option) *option {
for _, opt := range opts {
if opt == nil {
continue
}
o.toDevice = opt(o.toDevice)
}
return o
}
}
func Overwrite(b bool) Option {
return func(o *option) *option {
if b {
o.createFlag = os.O_WRONLY | os.O_CREATE | os.O_TRUNC
return o
}
o.createFlag = os.O_WRONLY | os.O_CREATE | os.O_EXCL
return o
}
}
func WithProgressBar() Option {
return WithEventHandler(NewProgressBar())
}
func WithHash(b bool) Option {
return func(o *option) *option {
o.withHash = b
return o
}
}
func WithLogger(logger *logrus.Logger) Option {
return func(o *option) *option {
o.logger = logger
return o
}
}
func WithEventHandler(h EventHandler) Option {
return func(o *option) *option {
o.eventHanders = append(o.eventHanders, h)
return o
}
}