-
Notifications
You must be signed in to change notification settings - Fork 0
/
walk.go
59 lines (54 loc) · 964 Bytes
/
walk.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
package engine
import (
"fmt"
"io"
"log"
"os"
"path/filepath"
"syscall"
"github.com/atscan/atr/cli"
"github.com/atscan/atr/repo"
)
func WalkFiles(ctx *cli.Context, cb func(repo.RepoSnapshot, error)) error {
wd := ctx.WorkingDir
if wd != "." {
syscall.Chdir(wd)
}
dir := "."
if len(ctx.Args) > 0 {
dir = ctx.Args[0]
}
if dir == "" {
dir = "."
}
info, err := os.Stat(dir)
if err != nil {
fmt.Println(err)
return err
}
if !info.IsDir() {
cb(Load(ctx, dir))
return nil
}
err = filepath.Walk(dir,
func(fn string, info os.FileInfo, err error) error {
if err != nil {
return err
}
cb(Load(ctx, fn))
return nil
})
if err != nil {
log.Println(err)
}
return nil
}
func WalkStream(ctx *cli.Context, input io.Reader, cb func(repo.RepoSnapshot, error)) error {
ss, err := LoadFromStream(ctx, input, -1)
if err != nil {
log.Println("Cannot load from stream:", err)
return nil
}
cb(ss, nil)
return nil
}