-
Notifications
You must be signed in to change notification settings - Fork 0
/
create.go
55 lines (46 loc) · 1017 Bytes
/
create.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
package index
import (
"context"
"log"
"os"
"path/filepath"
"time"
"github.com/mxk/go-cli"
"github.com/mxk/fsx/index"
)
var _ = indexCli.Add(&cli.Cfg{
Name: "create|c",
Usage: "<index> <root>",
Summary: "Create a new file system index",
MinArgs: 2,
MaxArgs: 2,
New: func() cli.Cmd { return createCmd{} },
})
type createCmd struct{}
func (createCmd) Main(args []string) error {
root := filepath.Clean(args[1])
var m monitor
x, err := index.Scan(context.Background(), os.DirFS(root), m.err, m.report)
if err != nil {
return err
}
if err = x.Save(args[0]); err == nil && m.walkErr {
err = cli.ExitCode(1)
}
return err
}
type monitor struct {
walkErr bool
nextReport time.Duration
}
func (m *monitor) err(err error) {
m.walkErr = true
log.Println(err)
}
func (m *monitor) report(p *index.Progress) {
const rate = 5 * time.Minute
if p.Duration() >= max(time.Minute, m.nextReport) || p.IsFinal() {
log.Println(p)
m.nextReport = (p.Duration() + rate).Round(rate)
}
}