-
Notifications
You must be signed in to change notification settings - Fork 1
/
walker.go
44 lines (35 loc) · 953 Bytes
/
walker.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
// Copyright © 2013-2018 Pierre Neidhardt <ambrevar@gmail.com>
// Use of this file is governed by the license that can be found in LICENSE.
package main
import (
"errors"
"strings"
"github.com/yookoala/realpath"
)
var errInputFile = errors.New("cannot process input file")
// walker feeds the output channel with files.
// Duplicates are discarded.
type walker struct {
visited map[string]bool
}
func (w *walker) Init() {
w.visited = map[string]bool{}
}
func (w *walker) Close() {}
func (w *walker) Run(fr *FileRecord) error {
if !options.Extensions[strings.ToLower(Ext(fr.input.path))] {
fr.debug.Printf("Unknown extension '%v'", Ext(fr.input.path))
return errInputFile
}
rpath, err := realpath.Realpath(fr.input.path)
if err != nil {
fr.error.Print("Cannot get real path:", err)
return errInputFile
}
if w.visited[rpath] {
fr.debug.Print("Duplicate file")
return errInputFile
}
w.visited[rpath] = true
return nil
}