@@ -14,14 +14,16 @@ import (
14
14
"github.com/docker/buildx/util/confutil"
15
15
"github.com/docker/buildx/util/desktop/bundle"
16
16
"github.com/docker/cli/cli/command"
17
+ "github.com/moby/buildkit/client"
17
18
"github.com/pkg/errors"
18
19
"github.com/spf13/cobra"
19
20
)
20
21
21
22
type exportOptions struct {
22
23
builder string
23
- ref string
24
+ refs [] string
24
25
output string
26
+ all bool
25
27
}
26
28
27
29
func runExport (ctx context.Context , dockerCli command.Cli , opts exportOptions ) error {
@@ -40,40 +42,60 @@ func runExport(ctx context.Context, dockerCli command.Cli, opts exportOptions) e
40
42
}
41
43
}
42
44
43
- recs , err := queryRecords (ctx , opts .ref , nodes , & queryOptions {
44
- CompletedOnly : true ,
45
- })
46
- if err != nil {
47
- return err
45
+ if len (opts .refs ) == 0 {
46
+ opts .refs = []string {"" }
48
47
}
49
48
50
- if len (recs ) == 0 {
51
- if opts .ref == "" {
52
- return errors .New ("no records found" )
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
53
56
}
54
- return errors .Errorf ("no record found for ref %q" , opts .ref )
55
- }
56
57
57
- if opts .ref == "" {
58
- slices .SortFunc (recs , func (a , b historyRecord ) int {
59
- return b .CreatedAt .AsTime ().Compare (a .CreatedAt .AsTime ())
60
- })
61
- }
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
+ }
62
64
63
- recs = recs [:1 ]
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
+ }
64
78
65
79
ls , err := localstate .New (confutil .NewConfig (dockerCli ))
66
80
if err != nil {
67
81
return err
68
82
}
69
83
70
- c , err := recs [0 ].node .Driver .Client (ctx )
71
- if err != nil {
72
- return err
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 )
73
95
}
74
96
75
- toExport := make ([]* bundle.Record , 0 , len (recs ))
76
- for _ , rec := range recs {
97
+ toExport := make ([]* bundle.Record , 0 , len (res ))
98
+ for _ , rec := range res {
77
99
var defaultPlatform string
78
100
if p := rec .node .Platforms ; len (p ) > 0 {
79
101
defaultPlatform = platforms .FormatAll (platforms .Normalize (p [0 ]))
@@ -110,7 +132,7 @@ func runExport(ctx context.Context, dockerCli command.Cli, opts exportOptions) e
110
132
}
111
133
}
112
134
113
- return bundle .Export (ctx , c , w , toExport )
135
+ return bundle .Export (ctx , clients , w , toExport )
114
136
}
115
137
116
138
func exportCmd (dockerCli command.Cli , rootOpts RootOptions ) * cobra.Command {
@@ -119,11 +141,11 @@ func exportCmd(dockerCli command.Cli, rootOpts RootOptions) *cobra.Command {
119
141
cmd := & cobra.Command {
120
142
Use : "export [OPTIONS] [REF]" ,
121
143
Short : "Export a build into Docker Desktop bundle" ,
122
- Args : cobra .MaximumNArgs (1 ),
123
144
RunE : func (cmd * cobra.Command , args []string ) error {
124
- if len (args ) > 0 {
125
- options . ref = args [ 0 ]
145
+ if options . all && len (args ) > 0 {
146
+ return errors . New ( "cannot specify refs when using --all" )
126
147
}
148
+ options .refs = args
127
149
options .builder = * rootOpts .Builder
128
150
return runExport (cmd .Context (), dockerCli , options )
129
151
},
@@ -132,6 +154,7 @@ func exportCmd(dockerCli command.Cli, rootOpts RootOptions) *cobra.Command {
132
154
133
155
flags := cmd .Flags ()
134
156
flags .StringVarP (& options .output , "output" , "o" , "" , "Output file path" )
157
+ flags .BoolVar (& options .all , "all" , false , "Export all records for the builder" )
135
158
136
159
return cmd
137
160
}
0 commit comments