Skip to content

Commit

Permalink
Merge pull request moby#2793 from AkihiroSuda/examples-dockefile2llb-…
Browse files Browse the repository at this point in the history
…metadata

examples/dockerfile2llb: add `--partial-image-config-file`, `--partial-metadata-file`
  • Loading branch information
tonistiigi authored Apr 12, 2022
2 parents 4249d86 + 1a56835 commit c2f909e
Showing 1 changed file with 41 additions and 10 deletions.
51 changes: 41 additions & 10 deletions examples/dockerfile2llb/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,30 +2,41 @@ package main

import (
"context"
"encoding/json"
"flag"
"io"
"log"
"os"

"github.com/moby/buildkit/client/llb"
"github.com/moby/buildkit/client/llb/imagemetaresolver"
"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())
Expand 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)
}

0 comments on commit c2f909e

Please sign in to comment.