-
Notifications
You must be signed in to change notification settings - Fork 3
/
payload.go
80 lines (69 loc) · 1.79 KB
/
payload.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
package go_bagit
import (
"fmt"
"io/fs"
"os"
"path/filepath"
"regexp"
)
type Payload map[string]os.FileInfo
type PayloadMatch struct {
Path string
FileInfo os.FileInfo
}
func loadPayload(bag *Bag) error {
dataDir := filepath.Join(bag.Path, "data")
err := filepath.Walk(dataDir, func(path string, info fs.FileInfo, err error) error {
if path != dataDir {
bag.Payload[path] = info
}
return nil
})
if err != nil {
return err
}
return nil
}
func (p Payload) GetFileInPayload(filename string) (PayloadMatch, error) {
for path, fi := range p {
if fi.Name() == filename && !fi.IsDir() {
return PayloadMatch{path, fi}, nil
}
}
return PayloadMatch{}, fmt.Errorf("Payload did not match %s", filename)
}
func (p Payload) GetDirInPayload(dirName string) (PayloadMatch, error) {
for path, fi := range p {
if fi.Name() == dirName && fi.IsDir() {
return PayloadMatch{path, fi}, nil
}
}
return PayloadMatch{}, fmt.Errorf("Payload did not match %s", dirName)
}
func (p Payload) FindFilesInPayload(matcher *regexp.Regexp) []PayloadMatch {
payloadMatch := []PayloadMatch{}
for path, fi := range p {
if matcher.MatchString(path) && !fi.IsDir() {
payloadMatch = append(payloadMatch, PayloadMatch{path, fi})
}
}
return payloadMatch
}
func (p Payload) FindDirsInPayload(matcher *regexp.Regexp) []PayloadMatch {
payloadMatch := []PayloadMatch{}
for path, fi := range p {
if matcher.MatchString(path) && fi.IsDir() {
payloadMatch = append(payloadMatch, PayloadMatch{path, fi})
}
}
return payloadMatch
}
func (p Payload) FindAllInPayload(matcher *regexp.Regexp) []PayloadMatch {
payloadMatches := []PayloadMatch{}
for path, fi := range p {
if matcher.MatchString(path) {
payloadMatches = append(payloadMatches, PayloadMatch{path, fi})
}
}
return payloadMatches
}