/
debug_llb.go
85 lines (65 loc) · 1.88 KB
/
debug_llb.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
package main
import (
"os"
"github.com/NiR-/zbuild/pkg/builder"
"github.com/NiR-/zbuild/pkg/llbutils"
"github.com/NiR-/zbuild/pkg/registry"
"github.com/moby/buildkit/client/llb"
"github.com/sirupsen/logrus"
"github.com/spf13/cobra"
)
var debugFlags = struct {
file string
stage string
context string
asJSON bool
}{}
const debugDescription = `Output LLB DAG in binary or JSON format.
This command alone is not really useful. To have a readable output, you have to
either pipe its output to ` + "`buildctl debug dump-llb`" + `:
$ zbuild debug-llb | buildctl debug dump-llb | jq -C . | less -R
Or you can generate JSON dumps like the ones used by most zbuild testcases
using:
$ zbuild debug-llb --json
You can also pipe this last command into ` + "`zbuild llbgraph`" + `:
$ zbuild debug-llb --json | zbuild llbgraph
`
func newDebugLLBCmd() *cobra.Command {
cmd := &cobra.Command{
Use: "debug-llb",
Hidden: true,
Short: "Output LLB DAG in binary or JSON format.",
Long: debugDescription,
Run: HandleDebugLLBCmd,
}
AddFileFlag(cmd, &debugFlags.file)
AddContextFlag(cmd, &debugFlags.context)
AddStageFlag(cmd, &debugFlags.stage)
cmd.Flags().BoolVar(&debugFlags.asJSON, "json", false, "Output the LLB DAG in JSON format")
return cmd
}
func HandleDebugLLBCmd(cmd *cobra.Command, args []string) {
b := builder.Builder{
Registry: registry.Registry,
}
solver := newLocalSolver(debugFlags.context)
state, err := b.Debug(solver, debugFlags.file, debugFlags.stage)
if err != nil {
logrus.Fatalf("%+v", err)
}
if debugFlags.asJSON {
out, err := llbutils.StateToJSON(state)
if err != nil {
logrus.Fatalf("%+v", err)
}
if _, err := os.Stdout.Write(out); err != nil {
logrus.Fatalf("%+v", err)
}
return
}
out, err := state.Marshal(llb.LinuxAmd64)
if err != nil {
logrus.Fatalf("%+v", err)
}
llb.WriteTo(out, os.Stdout) //nolint:errcheck
}