-
Notifications
You must be signed in to change notification settings - Fork 0
/
fetch.go
46 lines (43 loc) · 1009 Bytes
/
fetch.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
package bago
import (
"bufio"
"fmt"
"io"
"path/filepath"
"regexp"
"strings"
)
type fetch []fetchEntry
type fetchEntry struct {
url string
size string
path EncPath
}
func (f *fetch) parse(reader io.Reader) error {
*f = nil
lineNum := 0
emptyLineRE := regexp.MustCompile(`^\s*$`)
fetchRE := regexp.MustCompile(`^(\S+)\s+(\S+)\s+(.*)$`)
scanner := bufio.NewScanner(reader)
for scanner.Scan() {
lineNum++
line := scanner.Text()
if emptyLineRE.MatchString(line) {
continue // ignore empty lines
}
match := fetchRE.FindStringSubmatch(line)
if len(match) < 4 {
return fmt.Errorf("Syntax error at line: %d", lineNum)
}
entry := fetchEntry{}
entry.url = strings.Trim(match[1], ` `)
entry.size = strings.Trim(match[2], ` `)
match[3] = strings.Trim(match[3], ` `)
entry.path = EncPath(filepath.Clean(match[3]))
if strings.HasPrefix(string(entry.path), `..`) {
return fmt.Errorf("Out of scope path at line: %d", lineNum)
}
*f = append(*f, entry)
}
return nil
}