From 7f763f70f21d33ab3be3c645c731f4fad3e3583d Mon Sep 17 00:00:00 2001 From: Akihiro Suda Date: Sun, 10 Apr 2022 19:17:59 +0900 Subject: [PATCH 1/2] examples/dockerfile2llb: improve error handling Signed-off-by: Akihiro Suda --- examples/dockerfile2llb/main.go | 17 +++++++++++------ 1 file changed, 11 insertions(+), 6 deletions(-) diff --git a/examples/dockerfile2llb/main.go b/examples/dockerfile2llb/main.go index 284c649cbab1..0f8982324887 100644 --- a/examples/dockerfile2llb/main.go +++ b/examples/dockerfile2llb/main.go @@ -4,7 +4,6 @@ import ( "context" "flag" "io" - "log" "os" "github.com/moby/buildkit/client/llb" @@ -12,6 +11,7 @@ import ( "github.com/moby/buildkit/frontend/dockerfile/dockerfile2llb" "github.com/moby/buildkit/solver/pb" "github.com/moby/buildkit/util/appcontext" + "github.com/sirupsen/logrus" ) type buildOpt struct { @@ -19,13 +19,19 @@ type buildOpt struct { } func main() { + if err := xmain(); err != nil { + logrus.Fatal(err) + } +} + +func xmain() error { var opt buildOpt flag.StringVar(&opt.target, "target", "", "target stage") flag.Parse() df, err := io.ReadAll(os.Stdin) if err != nil { - panic(err) + return err } caps := pb.Caps.CapSet(pb.Caps.All()) @@ -36,8 +42,7 @@ func main() { LLBCaps: &caps, }) if err != nil { - log.Printf("err: %+v", err) - panic(err) + return err } _ = img @@ -45,7 +50,7 @@ func main() { dt, err := state.Marshal(context.TODO()) if err != nil { - panic(err) + return err } - llb.WriteTo(dt, os.Stdout) + return llb.WriteTo(dt, os.Stdout) } From 1a56835475c3fd43f96935636b4a38edb39d173b Mon Sep 17 00:00:00 2001 From: Akihiro Suda Date: Sun, 10 Apr 2022 19:29:25 +0900 Subject: [PATCH 2/2] examples/dockerfile2llb: add --partial-image-config-file, --partial-metadata-file Signed-off-by: Akihiro Suda --- examples/dockerfile2llb/main.go | 36 ++++++++++++++++++++++++++++----- 1 file changed, 31 insertions(+), 5 deletions(-) diff --git a/examples/dockerfile2llb/main.go b/examples/dockerfile2llb/main.go index 0f8982324887..224aee47f898 100644 --- a/examples/dockerfile2llb/main.go +++ b/examples/dockerfile2llb/main.go @@ -2,6 +2,7 @@ package main import ( "context" + "encoding/json" "flag" "io" "os" @@ -15,7 +16,9 @@ import ( ) type buildOpt struct { - target string + target string + partialImageConfigFile string + partialMetadataFile string } func main() { @@ -27,6 +30,8 @@ func main() { func xmain() error { var opt buildOpt flag.StringVar(&opt.target, "target", "", "target stage") + flag.StringVar(&opt.partialImageConfigFile, "partial-image-config-file", "", "Output partial image config as a JSON file") + flag.StringVar(&opt.partialMetadataFile, "partial-metadata-file", "", "Output partial metadata sa a JSON file") flag.Parse() df, err := io.ReadAll(os.Stdin) @@ -45,12 +50,33 @@ func xmain() error { return err } - _ = img - _ = bi - dt, err := state.Marshal(context.TODO()) if err != nil { return err } - return llb.WriteTo(dt, os.Stdout) + if err := llb.WriteTo(dt, os.Stdout); err != nil { + return err + } + if opt.partialImageConfigFile != "" { + if err := writeJSON(opt.partialImageConfigFile, img); err != nil { + return err + } + } + if opt.partialMetadataFile != "" { + if err := writeJSON(opt.partialMetadataFile, bi); err != nil { + return err + } + } + return nil +} + +func writeJSON(f string, x interface{}) error { + b, err := json.Marshal(x) + if err != nil { + return err + } + if err := os.RemoveAll(f); err != nil { + return err + } + return os.WriteFile(f, b, 0o644) }