-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathparser.go
67 lines (54 loc) · 1.08 KB
/
parser.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
package spm
import (
"bufio"
"bytes"
"errors"
"io"
"strings"
)
type Parser struct {
r io.Reader
}
func NewParser(r io.Reader) *Parser {
return &Parser{r: r}
}
func (p *Parser) Parse() (jobs []Job, err error) {
reader := bufio.NewReader(p.r)
for {
job := Job{}
var lines []byte
for {
line, _, err := reader.ReadLine()
if err == io.EOF {
return jobs, nil
}
if err != nil {
return jobs, err
}
// trim leading and trailing spaces
line = bytes.TrimSpace(line)
if len(line) == 0 {
continue
} else if line[0] == '#' {
continue
} else if len(line) > 0 && line[len(line)-1] == '\\' {
lines = append(lines, line[:len(line)-1]...)
continue
} else {
lines = append(lines, line...)
}
sp := strings.SplitN(string(lines), ":", 2)
if len(sp) < 2 {
return jobs, errors.New("spm: missing command")
}
job.Name = sp[0]
commandsStr := sp[1]
if job.Name == "" {
return jobs, errors.New("spm: invalid name")
}
job.Command = strings.Trim(commandsStr, " ")
jobs = append(jobs, job)
break
}
}
}