mirror of
https://github.com/wahyd4/forego.git
synced 2026-08-09 05:07:05 +10:00
update with suggestions from @kr
This commit is contained in:
+10
-16
@@ -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
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user