Skip to content

Commit

Permalink
examples/dockerfile2llb: add --image-json-file, --metadata-file
Browse files Browse the repository at this point in the history
Signed-off-by: Akihiro Suda <akihiro.suda.cz@hco.ntt.co.jp>
  • Loading branch information
AkihiroSuda committed Apr 10, 2022
1 parent 7f763f7 commit 24249f3
Showing 1 changed file with 31 additions and 5 deletions.
36 changes: 31 additions & 5 deletions examples/dockerfile2llb/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package main

import (
"context"
"encoding/json"
"flag"
"io"
"os"
Expand All @@ -15,7 +16,9 @@ import (
)

type buildOpt struct {
target string
target string
imageJSONFile string
metadataFile string
}

func main() {
Expand All @@ -27,6 +30,8 @@ func main() {
func xmain() error {
var opt buildOpt
flag.StringVar(&opt.target, "target", "", "target stage")
flag.StringVar(&opt.imageJSONFile, "image-json-file", "", "Output image config to a file as JSON")
flag.StringVar(&opt.metadataFile, "metadata-file", "", "Output build metadata (e.g., image digest) to a file as JSON")
flag.Parse()

df, err := io.ReadAll(os.Stdin)
Expand All @@ -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.imageJSONFile != "" {
if err := writeJSON(opt.imageJSONFile, img); err != nil {
return err
}
}
if opt.metadataFile != "" {
if err := writeJSON(opt.metadataFile, 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)
}

0 comments on commit 24249f3

Please sign in to comment.