From 6999d3dcb6061f04caec2df7e12b9d268e540c41 Mon Sep 17 00:00:00 2001 From: Tonis Tiigi Date: Wed, 27 Mar 2019 13:43:40 -0700 Subject: [PATCH] fix and improve outputs parsing Signed-off-by: Tonis Tiigi --- build/output.go | 45 +++++++++++++++++++++++++++++---------------- 1 file changed, 29 insertions(+), 16 deletions(-) diff --git a/build/output.go b/build/output.go index bb61a33b..7f946664 100644 --- a/build/output.go +++ b/build/output.go @@ -5,6 +5,7 @@ import ( "os" "strings" + "github.com/containerd/console" "github.com/moby/buildkit/client" "github.com/pkg/errors" ) @@ -20,9 +21,9 @@ func ParseOutputs(inp []string) ([]client.ExportEntry, error) { if err != nil { return nil, err } - if len(fields) == 1 && fields[0] == s { + if len(fields) == 1 && fields[0] == s && !strings.HasPrefix(s, "type=") { outs = append(outs, client.ExportEntry{ - Type: "local", + Type: client.ExporterLocal, OutputDir: s, }) continue @@ -51,32 +52,44 @@ func ParseOutputs(inp []string) ([]client.ExportEntry, error) { // handle client side switch out.Type { - case "local": + case client.ExporterLocal: dest, ok := out.Attrs["dest"] if !ok { return nil, errors.Errorf("dest is required for local output") } out.OutputDir = dest delete(out.Attrs, "dest") - case "oci", "dest": + case client.ExporterOCI, client.ExporterDocker: dest, ok := out.Attrs["dest"] if !ok { - if out.Type != "docker" { - return nil, errors.Errorf("dest is required for %s output", out.Type) - } - } else { - if dest == "-" { - out.Output = os.Stdout + if out.Type != client.ExporterDocker { + dest = "-" } else { - f, err := os.Open(dest) - if err != nil { - out.Output = f - } + return nil, errors.Errorf("loading to docker currently not implemented, specify dest file or -") } - delete(out.Attrs, "dest") } + if dest == "-" { + if _, err := console.ConsoleFromFile(os.Stdout); err == nil { + return nil, errors.Errorf("output file is required for %s exporter. refusing to write to console", out.Type) + } + out.Output = os.Stdout + } else if dest != "" { + fi, err := os.Stat(dest) + if err != nil && !os.IsNotExist(err) { + return nil, errors.Wrapf(err, "invalid destination file: %s", dest) + } + if err == nil && fi.IsDir() { + return nil, errors.Errorf("destination file %s is a directory", dest) + } + f, err := os.Create(dest) + if err != nil { + return nil, errors.Errorf("failed to open %s", err) + } + out.Output = f + } + delete(out.Attrs, "dest") case "registry": - out.Type = "iamge" + out.Type = client.ExporterImage out.Attrs["push"] = "true" }