-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
125 lines (104 loc) · 3.15 KB
/
main.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
package main
import (
"context"
"flag"
"os"
"os/signal"
"github.com/klauspost/cpuid/v2"
"github.com/samuelncui/acp"
"github.com/sirupsen/logrus"
)
var (
withProgressBar = flag.Bool("p", true, "display progress bar")
notOverwrite = flag.Bool("n", false, "not overwrite exist file")
// continueReport = flag.String("c", "", "continue with previous report, for auto fill circumstances")
noTarget = flag.Bool("notarget", false, "do not have target, use as dir index tool")
reportPath = flag.String("report", "", "json report storage path")
reportIndent = flag.Bool("report-indent", false, "json report with indent")
fromLinear = flag.Bool("from-linear", false, "copy from linear device, such like tape drive")
toLinear = flag.Bool("to-linear", false, "copy to linear device, such like tape drive")
targetPaths []string
)
func init() {
flag.Func("target", "use target flag to give multi target path", func(s string) error {
targetPaths = append(targetPaths, s)
return nil
})
}
func main() {
ctx, cancel := context.WithCancel(context.Background())
cpuid.Flags()
flag.Parse()
cpuid.Detect()
sources := flag.Args()
if len(sources) == 0 {
logrus.Fatalf("cannot found source path")
}
if !*noTarget && len(targetPaths) == 0 {
targetPaths = append(targetPaths, sources[len(sources)-1])
sources = sources[:len(sources)-1]
}
if len(sources) == 0 {
logrus.Fatalf("cannot found source path")
}
signals := make(chan os.Signal, 1)
signal.Notify(signals, os.Interrupt)
go func() {
for sig := range signals {
if sig != os.Interrupt {
continue
}
cancel()
}
}()
opts := make([]acp.Option, 0, 8)
opts = append(opts, acp.WildcardJob(acp.Source(sources...), acp.Target(targetPaths...)))
// if *continueReport != "" {
// f, err := os.Open(*continueReport)
// if err != nil {
// logrus.Fatalf("cannot open continue report file, %s", err)
// }
// r := new(acp.Report)
// if err := json.NewDecoder(f).Decode(r); err != nil {
// logrus.Fatalf("decode continue report file, %s", err)
// }
// for _, s := range r.NoSpaceSources {
// logrus.Infof("restore unfinished: base= '%s' relative_path= '%v'", s.Base, s.RelativePaths)
// opts = append(opts, acp.AccurateSource(s.Base, s.RelativePaths...))
// }
// }
opts = append(opts, acp.WithHash(*reportPath != ""))
opts = append(opts, acp.Overwrite(!*notOverwrite))
if *withProgressBar {
opts = append(opts, acp.WithProgressBar())
}
if *fromLinear {
opts = append(opts, acp.SetFromDevice(acp.LinearDevice(true)))
}
if *toLinear {
opts = append(opts, acp.SetToDevice(acp.LinearDevice(true)))
}
if *reportPath != "" {
handler, getter := acp.NewReportGetter()
opts = append(opts, acp.WithEventHandler(handler))
defer func() {
if *reportPath == "" {
return
}
report := getter()
r, err := os.Create(*reportPath)
if err != nil {
logrus.Warnf("open report fail, path= '%s', err= %w", *reportPath, err)
logrus.Infof("report: %s", report)
return
}
defer r.Close()
r.Write([]byte(report.ToJSONString(*reportIndent)))
}()
}
c, err := acp.New(ctx, opts...)
if err != nil {
logrus.Fatalf("unexpected exit: %s", err)
}
c.Wait()
}