-
Notifications
You must be signed in to change notification settings - Fork 0
/
scanner.go
94 lines (80 loc) · 1.72 KB
/
scanner.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
package main
import (
"bufio"
"bytes"
"errors"
"fmt"
"io"
"os"
"regexp"
)
// scanLines reads lines from an input reader and applies a callback function to
// each line. It is used for processing the contents of the procfile.
func scanLines(r io.Reader, callback func([]byte) bool) error {
var (
err error
line []byte
isPrefix bool
)
reader := bufio.NewReader(r)
buf := new(bytes.Buffer)
for {
line, isPrefix, err = reader.ReadLine()
if err != nil {
break
}
buf.Write(line)
if !isPrefix {
if !callback(buf.Bytes()) {
return nil
}
buf.Reset()
}
}
if err != io.EOF && err != io.ErrClosedPipe {
return err
}
return nil
}
// readProcfile opens the procfile and uses parseProcfile to parse it.
func readProcfile(filename string) ([]entry, error) {
file, err := os.Open(filename)
if err != nil {
return nil, err
}
defer file.Close()
return parseProcfile(file)
}
// parseProcfile reads and parses the procfile, returning a slice of entries.
func parseProcfile(r io.Reader) (entries []entry, err error) {
re, _ := regexp.Compile(`^([\w-]+):\s+(.+)$`)
names := make(map[string]bool)
dup := ""
err = scanLines(r, func(b []byte) bool {
if len(b) == 0 {
return true
}
params := re.FindStringSubmatch(string(b))
if len(params) != 3 {
return true
}
name, cmd := params[1], params[2]
if names[name] {
dup = name
return false
}
names[name] = true
entries = append(entries, entry{name: name, cmd: cmd})
return true
})
if err != nil {
return nil, err
}
if dup != "" {
return nil, fmt.Errorf("Duplicate process name %s in Procfile.dev", dup)
}
if len(entries) == 0 {
return nil, errors.New("No entries found in Procfile.dev")
}
return entries, nil
}