read Dockerfile into memory before parsing, to facilitate regexp preprocessing

This commit is contained in:
Jason McVetta 2013-09-09 16:42:04 -07:00
parent 672f1e0683
commit 6921ca4813
1 changed files with 19 additions and 14 deletions

View File

@ -1,7 +1,6 @@
package docker package docker
import ( import (
"bufio"
"encoding/json" "encoding/json"
"fmt" "fmt"
"github.com/dotcloud/docker/utils" "github.com/dotcloud/docker/utils"
@ -459,6 +458,8 @@ func (b *buildFile) commit(id string, autoCmd []string, comment string) error {
return nil return nil
} }
var multilineRegex = regexp.MustCompile("\\.*\n")
func (b *buildFile) Build(context io.Reader) (string, error) { func (b *buildFile) Build(context io.Reader) (string, error) {
// FIXME: @creack any reason for using /tmp instead of ""? // FIXME: @creack any reason for using /tmp instead of ""?
// FIXME: @creack "name" is a terrible variable name // FIXME: @creack "name" is a terrible variable name
@ -471,22 +472,26 @@ func (b *buildFile) Build(context io.Reader) (string, error) {
} }
defer os.RemoveAll(name) defer os.RemoveAll(name)
b.context = name b.context = name
dockerfile, err := os.Open(path.Join(name, "Dockerfile")) filename := path.Join(name, "Dockerfile")
if err != nil { if _, err := os.Stat(filename); os.IsNotExist(err) {
return "", fmt.Errorf("Can't build a directory with no Dockerfile") return "", fmt.Errorf("Can't build a directory with no Dockerfile")
} }
// FIXME: "file" is also a terrible variable name ;) fileBytes, err := ioutil.ReadFile(filename)
file := bufio.NewReader(dockerfile) if err != nil {
return "", err
}
dockerfile := string(fileBytes)
// dockerfile = multilineRegex.ReplaceAllString(dockerfile, " ")
stepN := 0 stepN := 0
for { for _, line := range strings.Split(dockerfile, "\n") {
line, err := file.ReadString('\n') /* line, err := dockerfile.ReadString('\n')
if err != nil { if err != nil {
if err == io.EOF && line == "" { if err == io.EOF && line == "" {
break break
} else if err != io.EOF { } else if err != io.EOF {
return "", err return "", err
} }
} }*/
line = strings.Trim(strings.Replace(line, "\t", " ", -1), " \t\r\n") line = strings.Trim(strings.Replace(line, "\t", " ", -1), " \t\r\n")
// Skip comments and empty line // Skip comments and empty line
if len(line) == 0 || line[0] == '#' { if len(line) == 0 || line[0] == '#' {