Skip to content

Commit

Permalink
Improve progress and history messages for heredoc-related commands
Browse files Browse the repository at this point in the history
Signed-off-by: Justin Chadwell <me@jedevc.com>
  • Loading branch information
jedevc committed Jun 30, 2021
1 parent 921b0de commit 2471e76
Showing 1 changed file with 39 additions and 7 deletions.
46 changes: 39 additions & 7 deletions frontend/dockerfile/dockerfile2llb/convert.go
Original file line number Diff line number Diff line change
Expand Up @@ -654,9 +654,11 @@ func dispatchEnv(d *dispatchState, c *instructions.EnvCommand) error {
func dispatchRun(d *dispatchState, c *instructions.RunCommand, proxy *llb.ProxyEnv, sources []*dispatchState, dopt dispatchOpt) error {
var opt []llb.RunOption

customname := c.String()

var args []string = c.CmdLine
if len(c.Files) > 0 {
if len(args) != 1 {
if len(args) != 1 || !c.PrependShell {
return fmt.Errorf("parsing produced an invalid run command: %v", args)
}

Expand All @@ -674,12 +676,15 @@ func dispatchRun(d *dispatchState, c *instructions.RunCommand, proxy *llb.ProxyE
if c.Files[0].Chomp {
data = parser.ChompHeredocContent(data)
}
st := llb.Scratch().Dir(sourcePath).File(llb.Mkfile(f, 0755, []byte(data)))
st := llb.Scratch().Dir(sourcePath).File(
llb.Mkfile(f, 0755, []byte(data)),
WithInternalName("preparing inline document"),
)

mount := llb.AddMount(destPath, st, llb.SourcePath(sourcePath), llb.Readonly)
opt = append(opt, mount)

args[0] = path.Join(destPath, f)
args = []string{path.Join(destPath, f)}
} else {
// Just a simple heredoc, so just run the contents in the
// shell: this creates the effect of a "fake"-heredoc, so that
Expand All @@ -690,14 +695,17 @@ func dispatchRun(d *dispatchState, c *instructions.RunCommand, proxy *llb.ProxyE
if c.Files[0].Chomp {
data = parser.ChompHeredocContent(data)
}
args[0] = data
args = []string{data}
}
customname += fmt.Sprintf(" (%s)", summarizeHeredoc(c.Files[0].Data))
} else {
// More complex heredoc, so reconstitute it, and pass it to the
// shell to handle.
full := args[0]
for _, file := range c.Files {
args[0] += "\n" + file.Data + file.Name
full += "\n" + file.Data + file.Name
}
args = []string{full}
}
}
if c.PrependShell {
Expand Down Expand Up @@ -746,7 +754,7 @@ func dispatchRun(d *dispatchState, c *instructions.RunCommand, proxy *llb.ProxyE
if err != nil {
return err
}
opt = append(opt, llb.WithCustomName(prefixCommand(d, uppercaseCmd(processCmdEnv(&shlex, c.String(), env)), d.prefixPlatform, pl)))
opt = append(opt, llb.WithCustomName(prefixCommand(d, uppercaseCmd(processCmdEnv(&shlex, customname, env)), d.prefixPlatform, pl)))
for _, h := range dopt.extraHosts {
opt = append(opt, llb.AddExtraHost(h.Host, h.IP))
}
Expand Down Expand Up @@ -873,9 +881,14 @@ func dispatchCopyFileOp(d *dispatchState, c instructions.SourcesAndDest, sourceS
}

for _, src := range c.SourceContents {
commitMessage.WriteString(" <<" + src.Path)

data := src.Data
f := src.Path
st := llb.Scratch().Dir("/").File(llb.Mkfile(f, 0664, []byte(data)))
st := llb.Scratch().File(
llb.Mkfile(f, 0664, []byte(data)),
WithInternalName("preparing inline document"),
)

opts := append([]llb.CopyOption{&llb.CopyInfo{
Mode: mode,
Expand Down Expand Up @@ -1512,3 +1525,22 @@ func location(sm *llb.SourceMap, locations []parser.Range) llb.ConstraintsOpt {
}
return sm.Location(loc)
}

func summarizeHeredoc(doc string) string {
lines := strings.Split(strings.TrimSpace(doc), "\n")

summary := ""
var i int
for i = 0; i < len(lines) && len(summary) < 72; i++ {
summary += strings.TrimSpace(lines[i])
if i != len(lines)-1 {
summary += ` \\ `
}
}

if i != len(lines) {
summary += "..."
}

return summary
}

0 comments on commit 2471e76

Please sign in to comment.