|
| 1 | +package history |
| 2 | + |
| 3 | +import ( |
| 4 | + "context" |
| 5 | + "io" |
| 6 | + "os" |
| 7 | + "slices" |
| 8 | + |
| 9 | + "github.com/containerd/console" |
| 10 | + "github.com/containerd/platforms" |
| 11 | + "github.com/docker/buildx/builder" |
| 12 | + "github.com/docker/buildx/localstate" |
| 13 | + "github.com/docker/buildx/util/cobrautil/completion" |
| 14 | + "github.com/docker/buildx/util/confutil" |
| 15 | + "github.com/docker/buildx/util/desktop/bundle" |
| 16 | + "github.com/docker/cli/cli/command" |
| 17 | + "github.com/moby/buildkit/client" |
| 18 | + "github.com/pkg/errors" |
| 19 | + "github.com/spf13/cobra" |
| 20 | +) |
| 21 | + |
| 22 | +type exportOptions struct { |
| 23 | + builder string |
| 24 | + refs []string |
| 25 | + output string |
| 26 | + all bool |
| 27 | +} |
| 28 | + |
| 29 | +func runExport(ctx context.Context, dockerCli command.Cli, opts exportOptions) error { |
| 30 | + b, err := builder.New(dockerCli, builder.WithName(opts.builder)) |
| 31 | + if err != nil { |
| 32 | + return err |
| 33 | + } |
| 34 | + |
| 35 | + nodes, err := b.LoadNodes(ctx, builder.WithData()) |
| 36 | + if err != nil { |
| 37 | + return err |
| 38 | + } |
| 39 | + for _, node := range nodes { |
| 40 | + if node.Err != nil { |
| 41 | + return node.Err |
| 42 | + } |
| 43 | + } |
| 44 | + |
| 45 | + if len(opts.refs) == 0 { |
| 46 | + opts.refs = []string{""} |
| 47 | + } |
| 48 | + |
| 49 | + var res []historyRecord |
| 50 | + for _, ref := range opts.refs { |
| 51 | + recs, err := queryRecords(ctx, ref, nodes, &queryOptions{ |
| 52 | + CompletedOnly: true, |
| 53 | + }) |
| 54 | + if err != nil { |
| 55 | + return err |
| 56 | + } |
| 57 | + |
| 58 | + if len(recs) == 0 { |
| 59 | + if ref == "" { |
| 60 | + return errors.New("no records found") |
| 61 | + } |
| 62 | + return errors.Errorf("no record found for ref %q", ref) |
| 63 | + } |
| 64 | + |
| 65 | + if ref == "" { |
| 66 | + slices.SortFunc(recs, func(a, b historyRecord) int { |
| 67 | + return b.CreatedAt.AsTime().Compare(a.CreatedAt.AsTime()) |
| 68 | + }) |
| 69 | + } |
| 70 | + |
| 71 | + if opts.all { |
| 72 | + res = append(res, recs...) |
| 73 | + break |
| 74 | + } else { |
| 75 | + res = append(res, recs[0]) |
| 76 | + } |
| 77 | + } |
| 78 | + |
| 79 | + ls, err := localstate.New(confutil.NewConfig(dockerCli)) |
| 80 | + if err != nil { |
| 81 | + return err |
| 82 | + } |
| 83 | + |
| 84 | + visited := map[*builder.Node]struct{}{} |
| 85 | + var clients []*client.Client |
| 86 | + for _, rec := range res { |
| 87 | + if _, ok := visited[rec.node]; ok { |
| 88 | + continue |
| 89 | + } |
| 90 | + c, err := rec.node.Driver.Client(ctx) |
| 91 | + if err != nil { |
| 92 | + return err |
| 93 | + } |
| 94 | + clients = append(clients, c) |
| 95 | + } |
| 96 | + |
| 97 | + toExport := make([]*bundle.Record, 0, len(res)) |
| 98 | + for _, rec := range res { |
| 99 | + var defaultPlatform string |
| 100 | + if p := rec.node.Platforms; len(p) > 0 { |
| 101 | + defaultPlatform = platforms.FormatAll(platforms.Normalize(p[0])) |
| 102 | + } |
| 103 | + |
| 104 | + var stg *localstate.StateGroup |
| 105 | + st, _ := ls.ReadRef(rec.node.Builder, rec.node.Name, rec.Ref) |
| 106 | + if st != nil && st.GroupRef != "" { |
| 107 | + stg, err = ls.ReadGroup(st.GroupRef) |
| 108 | + if err != nil { |
| 109 | + return err |
| 110 | + } |
| 111 | + } |
| 112 | + |
| 113 | + toExport = append(toExport, &bundle.Record{ |
| 114 | + BuildHistoryRecord: rec.BuildHistoryRecord, |
| 115 | + DefaultPlatform: defaultPlatform, |
| 116 | + LocalState: st, |
| 117 | + StateGroup: stg, |
| 118 | + }) |
| 119 | + } |
| 120 | + |
| 121 | + var w io.Writer = os.Stdout |
| 122 | + if opts.output != "" { |
| 123 | + f, err := os.Create(opts.output) |
| 124 | + if err != nil { |
| 125 | + return errors.Wrapf(err, "failed to create output file %q", opts.output) |
| 126 | + } |
| 127 | + defer f.Close() |
| 128 | + w = f |
| 129 | + } else { |
| 130 | + if _, err := console.ConsoleFromFile(os.Stdout); err == nil { |
| 131 | + return errors.Errorf("refusing to write to console, use --output to specify a file") |
| 132 | + } |
| 133 | + } |
| 134 | + |
| 135 | + return bundle.Export(ctx, clients, w, toExport) |
| 136 | +} |
| 137 | + |
| 138 | +func exportCmd(dockerCli command.Cli, rootOpts RootOptions) *cobra.Command { |
| 139 | + var options exportOptions |
| 140 | + |
| 141 | + cmd := &cobra.Command{ |
| 142 | + Use: "export [OPTIONS] [REF]", |
| 143 | + Short: "Export a build into Docker Desktop bundle", |
| 144 | + RunE: func(cmd *cobra.Command, args []string) error { |
| 145 | + if options.all && len(args) > 0 { |
| 146 | + return errors.New("cannot specify refs when using --all") |
| 147 | + } |
| 148 | + options.refs = args |
| 149 | + options.builder = *rootOpts.Builder |
| 150 | + return runExport(cmd.Context(), dockerCli, options) |
| 151 | + }, |
| 152 | + ValidArgsFunction: completion.Disable, |
| 153 | + } |
| 154 | + |
| 155 | + flags := cmd.Flags() |
| 156 | + flags.StringVarP(&options.output, "output", "o", "", "Output file path") |
| 157 | + flags.BoolVar(&options.all, "all", false, "Export all records for the builder") |
| 158 | + |
| 159 | + return cmd |
| 160 | +} |
0 commit comments