mirror of
https://github.com/moby/moby.git
synced 2022-11-09 12:21:53 -05:00
Strip comments before parsing line continuations
Fixes #3898 Docker-DCO-1.1-Signed-off-by: Michael Crosby <michael@crosbymichael.com> (github: crosbymichael)
This commit is contained in:
parent
3b5b2f8953
commit
67af7b3fb0
2 changed files with 29 additions and 6 deletions
|
@ -311,6 +311,16 @@ RUN [ "$(cat /testfile)" = 'test!' ]
|
||||||
},
|
},
|
||||||
nil,
|
nil,
|
||||||
},
|
},
|
||||||
|
{
|
||||||
|
`
|
||||||
|
FROM {IMAGE}
|
||||||
|
# what \
|
||||||
|
RUN mkdir /testing
|
||||||
|
RUN touch /testing/other
|
||||||
|
`,
|
||||||
|
nil,
|
||||||
|
nil,
|
||||||
|
},
|
||||||
}
|
}
|
||||||
|
|
||||||
// FIXME: test building with 2 successive overlapping ADD commands
|
// FIXME: test building with 2 successive overlapping ADD commands
|
||||||
|
|
|
@ -729,20 +729,19 @@ func (b *buildFile) Build(context io.Reader) (string, error) {
|
||||||
if len(fileBytes) == 0 {
|
if len(fileBytes) == 0 {
|
||||||
return "", ErrDockerfileEmpty
|
return "", ErrDockerfileEmpty
|
||||||
}
|
}
|
||||||
dockerfile := string(fileBytes)
|
var (
|
||||||
dockerfile = lineContinuation.ReplaceAllString(dockerfile, "")
|
dockerfile = lineContinuation.ReplaceAllString(stripComments(fileBytes), "")
|
||||||
stepN := 0
|
stepN = 0
|
||||||
|
)
|
||||||
for _, line := range strings.Split(dockerfile, "\n") {
|
for _, line := range strings.Split(dockerfile, "\n") {
|
||||||
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
|
if len(line) == 0 {
|
||||||
if len(line) == 0 || line[0] == '#' {
|
|
||||||
continue
|
continue
|
||||||
}
|
}
|
||||||
if err := b.BuildStep(fmt.Sprintf("%d", stepN), line); err != nil {
|
if err := b.BuildStep(fmt.Sprintf("%d", stepN), line); err != nil {
|
||||||
return "", err
|
return "", err
|
||||||
}
|
}
|
||||||
stepN += 1
|
stepN += 1
|
||||||
|
|
||||||
}
|
}
|
||||||
if b.image != "" {
|
if b.image != "" {
|
||||||
fmt.Fprintf(b.outStream, "Successfully built %s\n", utils.TruncateID(b.image))
|
fmt.Fprintf(b.outStream, "Successfully built %s\n", utils.TruncateID(b.image))
|
||||||
|
@ -779,6 +778,20 @@ func (b *buildFile) BuildStep(name, expression string) error {
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func stripComments(raw []byte) string {
|
||||||
|
var (
|
||||||
|
out []string
|
||||||
|
lines = strings.Split(string(raw), "\n")
|
||||||
|
)
|
||||||
|
for _, l := range lines {
|
||||||
|
if len(l) == 0 || l[0] == '#' {
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
out = append(out, l)
|
||||||
|
}
|
||||||
|
return strings.Join(out, "\n")
|
||||||
|
}
|
||||||
|
|
||||||
func NewBuildFile(srv *Server, outStream, errStream io.Writer, verbose, utilizeCache, rm bool, outOld io.Writer, sf *utils.StreamFormatter, auth *registry.AuthConfig, authConfigFile *registry.ConfigFile) BuildFile {
|
func NewBuildFile(srv *Server, outStream, errStream io.Writer, verbose, utilizeCache, rm bool, outOld io.Writer, sf *utils.StreamFormatter, auth *registry.AuthConfig, authConfigFile *registry.ConfigFile) BuildFile {
|
||||||
return &buildFile{
|
return &buildFile{
|
||||||
runtime: srv.runtime,
|
runtime: srv.runtime,
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue