Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

examples/dockerfile2llb: add --partial-image-config-file, --partial-metadata-file #2793

Merged
merged 2 commits into from
Apr 12, 2022
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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)
}