diff --git a/examples/dockerfile2llb/main.go b/examples/dockerfile2llb/main.go index 284c649cbab1..224aee47f898 100644 --- a/examples/dockerfile2llb/main.go +++ b/examples/dockerfile2llb/main.go @@ -2,9 +2,9 @@ package main import ( "context" + "encoding/json" "flag" "io" - "log" "os" "github.com/moby/buildkit/client/llb" @@ -12,20 +12,31 @@ 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 { - target string + target string + partialImageConfigFile string + partialMetadataFile string } func main() { + if err := xmain(); err != nil { + logrus.Fatal(err) + } +} + +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) if err != nil { - panic(err) + return err } caps := pb.Caps.CapSet(pb.Caps.All()) @@ -36,16 +47,36 @@ func main() { LLBCaps: &caps, }) if err != nil { - log.Printf("err: %+v", err) - panic(err) + return err } - _ = img - _ = bi - dt, err := state.Marshal(context.TODO()) if err != nil { - panic(err) + return err + } + 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 } - llb.WriteTo(dt, os.Stdout) + return os.WriteFile(f, b, 0o644) }