/
app.go
115 lines (103 loc) · 2.88 KB
/
app.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
package cmd
import (
"os"
"runtime/pprof"
"strings"
"time"
"github.com/cockroachdb/errors"
"github.com/rs/zerolog/log"
"github.com/spf13/afero"
"github.com/urfave/cli/v2"
"github.com/vbauerster/mpb/v8"
"github.com/vbauerster/mpb/v8/decor"
"github.com/fanyang89/eaglexport/eaglexport"
)
func NewApp() *cli.App {
return &cli.App{
Name: "eagle-export",
Usage: "Export your eagle library to NAS",
Commands: []*cli.Command{
cmdExport,
},
}
}
var cmdExport = &cli.Command{
Name: "export",
Flags: []cli.Flag{
flagLibraryDir, flagDestDir, flagGroupBySmartFolder, flagOverwrite, flagForce,
flagSmbUser, flagSmbPassword, flagHistoryFile,
},
Action: func(c *cli.Context) error {
var err error
dst := c.String("dst")
var fs afero.Fs
if strings.HasPrefix(dst, "smb://") {
address, share, root, ok := parseSmbConnectionString(dst)
if !ok {
return errors.New("invalid smb connection string")
}
fs, err = eaglexport.NewSmbFs(address, share, eaglexport.SmbFsOption{
User: c.String("smb-user"),
Password: c.String("smb-password"),
})
if err != nil {
return errors.Wrap(err, "create smbfs failed")
}
defer func() {
err := fs.(*eaglexport.SmbFs).Close()
if err != nil {
log.Error().Err(err).Msg("close smbfs failed")
}
}()
dst = root
} else {
fs = afero.NewOsFs()
}
historyFile := c.String("history-file")
var history *eaglexport.History
if historyFile != "" {
history, err = eaglexport.NewHistory(historyFile)
if err != nil {
return err
}
history.Load()
}
lib := eaglexport.NewLibrary(c.String("library"), fs, history)
p := mpb.New(mpb.WithRefreshRate(180 * time.Millisecond))
speedBar := p.New(0,
mpb.BarStyle(),
mpb.PrependDecorators(
decor.Counters(decor.SizeB1024(0), "% .2f / % .2f"),
),
mpb.AppendDecorators(
decor.EwmaETA(decor.ET_STYLE_GO, 30),
decor.Name(" ] "),
decor.EwmaSpeed(decor.SizeB1024(0), "% .2f", 60),
))
itemBar := p.New(0, mpb.BarStyle().Rbound("|"),
mpb.PrependDecorators(
decor.Name("item ", decor.WC{C: decor.DindentRight | decor.DextraSpace}),
decor.CountersNoUnit("%d / %d ", decor.WCSyncWidth),
decor.OnComplete(decor.AverageETA(decor.ET_STYLE_GO), "done"),
),
mpb.AppendDecorators(decor.Percentage()))
option := eaglexport.ExportOption{
SpeedBar: speedBar,
ItemBar: itemBar,
Overwrite: c.Bool("overwrite"),
Force: c.Bool("force"),
GroupBySmartFolder: c.Bool("group-by-smart-folder"),
}
profileFile, err := os.OpenFile("cpu.profile", os.O_CREATE|os.O_RDWR|os.O_TRUNC, 0644)
if err != nil {
log.Error().Err(err).Msg("open file failed")
}
defer profileFile.Close()
err = pprof.StartCPUProfile(profileFile)
if err != nil {
log.Error().Err(err).Msg("start CPU profile failed")
}
defer pprof.StopCPUProfile()
return lib.Export(dst, option)
},
}