update with suggestions from @kr

This commit is contained in:
David Dollar
2013-08-22 12:35:51 -07:00
parent 56ab8069ca
commit 5afcfc2f95
2 changed files with 11 additions and 17 deletions
+10 -16
View File
@@ -1,17 +1,15 @@
package main
import (
"bytes"
"bufio"
"fmt"
"github.com/kr/pretty"
"io"
"io/ioutil"
"os"
"regexp"
)
const (
procfileEntryRegexp = "^([A-Za-z0-9_]+):\\s*(.+)$"
)
var procfileEntryRegexp = regexp.MustCompile("^([A-Za-z0-9_]+):\\s*(.+)$")
type ProcfileEntry struct {
name string
@@ -24,7 +22,7 @@ type Procfile struct {
var _ = pretty.Println // lol
func OpenProcfile(filename string) (*Procfile, error) {
func ReadProcfile(filename string) (*Procfile, error) {
fd, err := os.Open(filename)
if err != nil {
return nil, err
@@ -35,17 +33,13 @@ func OpenProcfile(filename string) (*Procfile, error) {
func parseProcfile(r io.Reader) (*Procfile, error) {
pf := new(Procfile)
b, err := ioutil.ReadAll(r)
if err != nil {
return nil, err
scanner := bufio.NewScanner(r)
for scanner.Scan() {
parts := procfileEntryRegexp.FindStringSubmatch(scanner.Text())
pf.entries = append(pf.entries, ProcfileEntry{parts[1], parts[2]})
}
lines := bytes.Split(b, []byte("\n"))
for _, line := range lines {
r := regexp.MustCompile(procfileEntryRegexp)
parts := r.FindStringSubmatch(string(line))
if parts != nil {
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
}
+1 -1
View File
@@ -28,7 +28,7 @@ func init() {
}
func runStart(cmd *Command, args []string) {
pf, err := OpenProcfile(flagProcfile)
pf, err := ReadProcfile(flagProcfile)
if err != nil {
fmt.Println("ERROR:", err)
return