Skip to content
This repository has been archived by the owner on Jun 13, 2021. It is now read-only.

Commit

Permalink
Create a global variable to hold output file
Browse files Browse the repository at this point in the history
With a function scoped `os.File`, next time the GC passes
the instance is collected, calling the finalizer and triggering
the invalidation of the FD, that cannot be used anymore.

Signed-off-by: Ulysses Souza <ulysses.souza@docker.com>
  • Loading branch information
Ulysses Souza committed Nov 26, 2019
1 parent d4fa5f0 commit df09d31
Showing 1 changed file with 22 additions and 7 deletions.
29 changes: 22 additions & 7 deletions internal/commands/build/build.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ import (
"github.com/docker/cli/cli"
"github.com/docker/cli/cli/command"
compose "github.com/docker/cli/cli/compose/types"
"github.com/docker/cli/cli/streams"
"github.com/docker/cnab-to-oci/remotes"
"github.com/docker/distribution/reference"
"github.com/moby/buildkit/client"
Expand All @@ -49,6 +50,8 @@ type buildOptions struct {
const buildExample = `- $ docker app build .
- $ docker app build --file myapp.dockerapp --tag myrepo/myapp:1.0.0 .`

var outputFile *os.File

func Cmd(dockerCli command.Cli) *cobra.Command {
var opts buildOptions
cmd := &cobra.Command{
Expand Down Expand Up @@ -136,6 +139,22 @@ func runBuild(dockerCli command.Cli, contextPath string, opt buildOptions) error
return err
}

func getOutputFile(realOut *streams.Out, quiet bool) (*os.File, error) {
if outputFile != nil {
return outputFile, nil
}
if quiet {
var err error
outputFile, err = os.Create(os.DevNull)
if err != nil {
return nil, err
}
return outputFile, nil
}
outputFile = os.NewFile(realOut.FD(), os.Stdout.Name())
return outputFile, nil
}

func buildImageUsingBuildx(app *types.App, contextPath string, opt buildOptions, dockerCli command.Cli) (*bundle.Bundle, error) {
buildopts, pulledServices, err := parseCompose(app, contextPath, opt)
if err != nil {
Expand All @@ -160,13 +179,9 @@ func buildImageUsingBuildx(app *types.App, contextPath string, opt buildOptions,
},
}

var out *os.File
if opt.quiet {
if out, err = os.Create(os.DevNull); err != nil {
return nil, err
}
} else {
out = os.NewFile(dockerCli.Out().FD(), "/dev/stdout")
out, err := getOutputFile(dockerCli.Out(), opt.quiet)
if err != nil {
return nil, err
}

pw := progress.NewPrinter(ctx, out, opt.progress)
Expand Down

0 comments on commit df09d31

Please sign in to comment.