This repository has been archived by the owner on Aug 22, 2022. It is now read-only.
-
-
Notifications
You must be signed in to change notification settings - Fork 60
/
walk.go
96 lines (77 loc) · 1.5 KB
/
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
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
package parser
import (
"encoding/json"
"go/token"
"os"
"path/filepath"
"strings"
"github.com/markbates/pkger/here"
)
var _ Decl = WalkDecl{}
type WalkDecl struct {
file *File
pos token.Position
value string
}
func (d WalkDecl) String() string {
b, _ := json.Marshal(d)
return string(b)
}
func (d WalkDecl) MarshalJSON() ([]byte, error) {
return json.Marshal(map[string]interface{}{
"type": "pkger.Walk",
"file": d.file,
"pos": d.pos,
"value": d.value,
})
}
func (d WalkDecl) File() (*File, error) {
if d.file == nil {
return nil, os.ErrNotExist
}
return d.file, nil
}
func (d WalkDecl) Position() (token.Position, error) {
return d.pos, nil
}
func (d WalkDecl) Value() (string, error) {
if d.value == "" {
return "", os.ErrNotExist
}
return d.value, nil
}
func (d WalkDecl) Files(virtual map[string]string) ([]*File, error) {
var files []*File
her := d.file.Here
pt := d.file.Path
root := filepath.Join(her.Module.Dir, pt.Name)
err := filepath.Walk(root, func(path string, info os.FileInfo, err error) error {
if err != nil {
return err
}
if info.IsDir() {
her, err = here.Dir(path)
if err != nil {
return err
}
}
n := strings.TrimPrefix(path, her.Module.Dir)
pt, err := her.Parse(n)
if err != nil {
return err
}
if _, ok := virtual[n]; ok {
if info.IsDir() {
return filepath.SkipDir
}
return nil
}
files = append(files, &File{
Abs: path,
Path: pt,
Here: her,
})
return nil
})
return files, err
}