Skip to content

Commit

Permalink
builder: Log errors to file
Browse files Browse the repository at this point in the history
Signed-off-by: Andrea Luzzardi <[email protected]>
  • Loading branch information
aluzzardi committed Oct 30, 2018
1 parent 1a9ebe2 commit 2f51b17
Showing 1 changed file with 30 additions and 1 deletion.
31 changes: 30 additions & 1 deletion pkg/builder/builder.go
Original file line number Diff line number Diff line change
@@ -1,9 +1,13 @@
package builder

import (
"bytes"
"context"
"io"
"io/ioutil"
"os/exec"

"github.com/blocklayerhq/chainkit/pkg/ui"
)

// Builder is a wrapper around `docker build` which provides a better UX.
Expand Down Expand Up @@ -41,12 +45,18 @@ func (b *Builder) Build(ctx context.Context, opts BuildOpts) error {
}
defer errReader.Close()

// Combine stdout and stderr into a single reader.
cmdReader := io.MultiReader(outReader, errReader)

// Keep the build output as a buffer.
// We'll need it to log build errors.
var output bytes.Buffer
tee := io.TeeReader(cmdReader, &output)

errCh := make(chan error)
go func() {
defer close(errCh)
errCh <- b.parser.Parse(cmdReader, opts)
errCh <- b.parser.Parse(tee, opts)
}()
err = cmd.Start()
if err != nil {
Expand All @@ -55,12 +65,31 @@ func (b *Builder) Build(ctx context.Context, opts BuildOpts) error {

err = cmd.Wait()
if err != nil {
b.buildLog(output)
return err
}

if err := <-errCh; err != nil {
b.buildLog(output)
return err
}

return nil
}

func (b *Builder) buildLog(output bytes.Buffer) error {
logfile, err := ioutil.TempFile("", "chainkit-build.*.log")
if err != nil {
return err
}
defer logfile.Close()

if _, err := logfile.Write(output.Bytes()); err != nil {
return err
}

ui.Error("A complete log of this build can be found in:")
ui.Error(" %s", logfile.Name())

return nil
}

0 comments on commit 2f51b17

Please sign in to comment.