fix and improve outputs parsing

Signed-off-by: Tonis Tiigi <tonistiigi@gmail.com>
pull/15/head
Tonis Tiigi 6 years ago
parent 325febc86c
commit 6999d3dcb6

@ -5,6 +5,7 @@ import (
"os" "os"
"strings" "strings"
"github.com/containerd/console"
"github.com/moby/buildkit/client" "github.com/moby/buildkit/client"
"github.com/pkg/errors" "github.com/pkg/errors"
) )
@ -20,9 +21,9 @@ func ParseOutputs(inp []string) ([]client.ExportEntry, error) {
if err != nil { if err != nil {
return nil, err 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{ outs = append(outs, client.ExportEntry{
Type: "local", Type: client.ExporterLocal,
OutputDir: s, OutputDir: s,
}) })
continue continue
@ -51,32 +52,44 @@ func ParseOutputs(inp []string) ([]client.ExportEntry, error) {
// handle client side // handle client side
switch out.Type { switch out.Type {
case "local": case client.ExporterLocal:
dest, ok := out.Attrs["dest"] dest, ok := out.Attrs["dest"]
if !ok { if !ok {
return nil, errors.Errorf("dest is required for local output") return nil, errors.Errorf("dest is required for local output")
} }
out.OutputDir = dest out.OutputDir = dest
delete(out.Attrs, "dest") delete(out.Attrs, "dest")
case "oci", "dest": case client.ExporterOCI, client.ExporterDocker:
dest, ok := out.Attrs["dest"] dest, ok := out.Attrs["dest"]
if !ok { if !ok {
if out.Type != "docker" { if out.Type != client.ExporterDocker {
return nil, errors.Errorf("dest is required for %s output", out.Type) dest = "-"
}
} else { } else {
return nil, errors.Errorf("loading to docker currently not implemented, specify dest file or -")
}
}
if 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 out.Output = os.Stdout
} else { } else if dest != "" {
f, err := os.Open(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 { if err != nil {
out.Output = f return nil, errors.Errorf("failed to open %s", err)
} }
out.Output = f
} }
delete(out.Attrs, "dest") delete(out.Attrs, "dest")
}
case "registry": case "registry":
out.Type = "iamge" out.Type = client.ExporterImage
out.Attrs["push"] = "true" out.Attrs["push"] = "true"
} }

Loading…
Cancel
Save