-
Notifications
You must be signed in to change notification settings - Fork 175
/
procfile.go
74 lines (66 loc) · 1.43 KB
/
procfile.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
package main
import (
"bufio"
"fmt"
"io"
"math"
"os"
"regexp"
)
var procfileEntryRegexp = regexp.MustCompile("^([A-Za-z0-9_-]+):\\s*(.+)$")
type ProcfileEntry struct {
Name string
Command string
}
type Procfile struct {
Entries []ProcfileEntry
}
func ReadProcfile(filename string) (*Procfile, error) {
fd, err := os.Open(filename)
if err != nil {
return nil, err
}
defer fd.Close()
return parseProcfile(fd)
}
func (pf *Procfile) HasProcess(name string) (exists bool) {
for _, entry := range pf.Entries {
if name == entry.Name {
return true
}
}
return false
}
func (pf *Procfile) LongestProcessName(concurrency map[string]int) (longest int) {
longest = 6 // length of forego
for _, entry := range pf.Entries {
thisLen := len(entry.Name)
// The "."
thisLen += 1
if c, ok := concurrency[entry.Name]; ok {
// Add the number of digits
thisLen += int(math.Log10(float64(c))) + 1
} else {
// The index number after the dot.
thisLen += 1
}
if thisLen > longest {
longest = thisLen
}
}
return
}
func parseProcfile(r io.Reader) (*Procfile, error) {
pf := new(Procfile)
scanner := bufio.NewScanner(r)
for scanner.Scan() {
parts := procfileEntryRegexp.FindStringSubmatch(scanner.Text())
if len(parts) > 0 {
pf.Entries = append(pf.Entries, ProcfileEntry{parts[1], parts[2]})
}
}
if err := scanner.Err(); err != nil {
return nil, fmt.Errorf("Reading Procfile: %s", err)
}
return pf, nil
}