Skip to content

Commit

Permalink
Various cleanups and bugfixes for heredocs
Browse files Browse the repository at this point in the history
Signed-off-by: Justin Chadwell <[email protected]>
  • Loading branch information
jedevc committed May 25, 2021
1 parent c68bfcf commit fa64578
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 8 deletions.
15 changes: 10 additions & 5 deletions frontend/dockerfile/instructions/parse.go
Original file line number Diff line number Diff line change
Expand Up @@ -248,13 +248,16 @@ func parseAdd(req parseRequest) (*AddCommand, error) {
return nil, err
}

if req.heredoc != nil && parser.IsHeredoc(req.args[0]) {
if req.heredoc != nil {
if len(req.args) != 2 {
return nil, errExactlyTwoArguments("ADD")
}
if parser.IsHeredoc(req.args[1]) {
return nil, errBadHeredoc("ADD", "a destination")
}
if !parser.IsHeredoc(req.args[0]) {
panic("First argument must be a heredoc, or somehow the parsing has gone wrong!")
}

return &AddCommand{
withNameAndCode: newWithNameAndCode(req),
Expand Down Expand Up @@ -285,13 +288,16 @@ func parseCopy(req parseRequest) (*CopyCommand, error) {
return nil, err
}

if req.heredoc != nil && parser.IsHeredoc(req.args[0]) {
if req.heredoc != nil {
if len(req.args) != 2 {
return nil, errExactlyTwoArguments("COPY")
}
if parser.IsHeredoc(req.args[1]) {
return nil, errBadHeredoc("COPY", "a destination")
}
if !parser.IsHeredoc(req.args[0]) {
panic("First argument must be a heredoc, or somehow the parsing has gone wrong!")
}

return &CopyCommand{
withNameAndCode: newWithNameAndCode(req),
Expand Down Expand Up @@ -394,9 +400,8 @@ func parseWorkdir(req parseRequest) (*WorkdirCommand, error) {

func parseShellDependentCommand(req parseRequest, emptyAsNil bool, allowHeredoc bool) (ShellDependantCmdLine, error) {
if allowHeredoc && req.heredoc != nil {
if len(req.args) != 1 || !parser.IsHeredoc(req.args[0]) {
// FIXME: this is not a good error message
return ShellDependantCmdLine{}, errors.Errorf("Bad heredoc shell command")
if !(len(req.args) == 1 && parser.IsHeredoc(req.args[0])) {
return ShellDependantCmdLine{}, errExactlyOneArgument(req.command + " with heredoc")
}

cmds := make([]strslice.StrSlice, len(req.heredoc.Lines))
Expand Down
10 changes: 7 additions & 3 deletions frontend/dockerfile/parser/parser.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ type Node struct {
Value string // actual content
Next *Node // the next item in the current sexp
Children []*Node // the children of this sexp
Heredoc *Heredoc // TODO
Heredoc *Heredoc // extra heredoc content attachment
Attributes map[string]bool // special attributes for this node
Original string // original line used before parsing
Flags []string // only top Node should have this set
Expand Down Expand Up @@ -332,19 +332,23 @@ func Parse(rwc io.Reader) (*Result, error) {
heredocExpand = false
}

var heredocLine string
var heredocLines []string
for scanner.Scan() {
heredocLine := scanner.Text()
heredocLine = scanner.Text()
if heredocChomp {
heredocLine = strings.TrimLeftFunc(heredocLine, unicode.IsSpace)
}
currentLine++

if heredocLine == heredocName {
break
}
heredocLines = append(heredocLines, heredocLine)
}
// TODO: handle unclosed heredoc
if heredocLine != heredocName {
return nil, withLocation(errors.New("unterminated heredoc"), startLine, currentLine)
}

heredoc = &Heredoc{
Name: heredocName,
Expand Down

0 comments on commit fa64578

Please sign in to comment.