mirror of
				https://gitea.com/Lydanne/buildx.git
				synced 2025-11-04 18:13:42 +08:00 
			
		
		
		
	history: add history import command
Signed-off-by: Tonis Tiigi <tonistiigi@gmail.com>
This commit is contained in:
		
							
								
								
									
										102
									
								
								commands/history/import.go
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										102
									
								
								commands/history/import.go
									
									
									
									
									
										Normal file
									
								
							@@ -0,0 +1,102 @@
 | 
				
			|||||||
 | 
					package history
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					import (
 | 
				
			||||||
 | 
						"context"
 | 
				
			||||||
 | 
						"encoding/json"
 | 
				
			||||||
 | 
						"fmt"
 | 
				
			||||||
 | 
						"io"
 | 
				
			||||||
 | 
						"net"
 | 
				
			||||||
 | 
						"net/http"
 | 
				
			||||||
 | 
						"os"
 | 
				
			||||||
 | 
						"strings"
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
						remoteutil "github.com/docker/buildx/driver/remote/util"
 | 
				
			||||||
 | 
						"github.com/docker/buildx/util/cobrautil/completion"
 | 
				
			||||||
 | 
						"github.com/docker/buildx/util/desktop"
 | 
				
			||||||
 | 
						"github.com/docker/cli/cli/command"
 | 
				
			||||||
 | 
						"github.com/pkg/browser"
 | 
				
			||||||
 | 
						"github.com/pkg/errors"
 | 
				
			||||||
 | 
						"github.com/spf13/cobra"
 | 
				
			||||||
 | 
					)
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					type importOptions struct {
 | 
				
			||||||
 | 
						file string
 | 
				
			||||||
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					func runImport(ctx context.Context, _ command.Cli, opts importOptions) error {
 | 
				
			||||||
 | 
						sock, err := desktop.BuildServerAddr()
 | 
				
			||||||
 | 
						if err != nil {
 | 
				
			||||||
 | 
							return err
 | 
				
			||||||
 | 
						}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
						tr := http.DefaultTransport.(*http.Transport).Clone()
 | 
				
			||||||
 | 
						tr.DialContext = func(ctx context.Context, _, _ string) (net.Conn, error) {
 | 
				
			||||||
 | 
							network, addr, ok := strings.Cut(sock, "://")
 | 
				
			||||||
 | 
							if !ok {
 | 
				
			||||||
 | 
								return nil, errors.Errorf("invalid endpoint address: %s", sock)
 | 
				
			||||||
 | 
							}
 | 
				
			||||||
 | 
							return remoteutil.DialContext(ctx, network, addr)
 | 
				
			||||||
 | 
						}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
						client := &http.Client{
 | 
				
			||||||
 | 
							Transport: tr,
 | 
				
			||||||
 | 
						}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
						var rdr io.Reader = os.Stdin
 | 
				
			||||||
 | 
						if opts.file != "" {
 | 
				
			||||||
 | 
							f, err := os.Open(opts.file)
 | 
				
			||||||
 | 
							if err != nil {
 | 
				
			||||||
 | 
								return errors.Wrap(err, "failed to open file")
 | 
				
			||||||
 | 
							}
 | 
				
			||||||
 | 
							defer f.Close()
 | 
				
			||||||
 | 
							rdr = f
 | 
				
			||||||
 | 
						}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
						req, err := http.NewRequestWithContext(ctx, http.MethodPost, "http://docker-desktop/upload", rdr)
 | 
				
			||||||
 | 
						if err != nil {
 | 
				
			||||||
 | 
							return errors.Wrap(err, "failed to create request")
 | 
				
			||||||
 | 
						}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
						resp, err := client.Do(req)
 | 
				
			||||||
 | 
						if err != nil {
 | 
				
			||||||
 | 
							return errors.Wrap(err, "failed to send request, check if Docker Desktop is running")
 | 
				
			||||||
 | 
						}
 | 
				
			||||||
 | 
						defer resp.Body.Close()
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
						if resp.StatusCode != http.StatusOK {
 | 
				
			||||||
 | 
							body, _ := io.ReadAll(resp.Body)
 | 
				
			||||||
 | 
							return errors.Errorf("failed to import build: %s", string(body))
 | 
				
			||||||
 | 
						}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
						var refs []string
 | 
				
			||||||
 | 
						dec := json.NewDecoder(resp.Body)
 | 
				
			||||||
 | 
						if err := dec.Decode(&refs); err != nil {
 | 
				
			||||||
 | 
							return errors.Wrap(err, "failed to decode response")
 | 
				
			||||||
 | 
						}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
						if len(refs) == 0 {
 | 
				
			||||||
 | 
							return errors.New("no build records found in the bundle")
 | 
				
			||||||
 | 
						}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
						url := desktop.BuildURL(fmt.Sprintf(".imported/_/%s", refs[0]))
 | 
				
			||||||
 | 
						return browser.OpenURL(url)
 | 
				
			||||||
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					func importCmd(dockerCli command.Cli, _ RootOptions) *cobra.Command {
 | 
				
			||||||
 | 
						var options importOptions
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
						cmd := &cobra.Command{
 | 
				
			||||||
 | 
							Use:   "import [OPTIONS] < bundle.dockerbuild",
 | 
				
			||||||
 | 
							Short: "Import a build into Docker Desktop",
 | 
				
			||||||
 | 
							Args:  cobra.NoArgs,
 | 
				
			||||||
 | 
							RunE: func(cmd *cobra.Command, args []string) error {
 | 
				
			||||||
 | 
								return runImport(cmd.Context(), dockerCli, options)
 | 
				
			||||||
 | 
							},
 | 
				
			||||||
 | 
							ValidArgsFunction: completion.Disable,
 | 
				
			||||||
 | 
						}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
						flags := cmd.Flags()
 | 
				
			||||||
 | 
						flags.StringVarP(&options.file, "file", "f", "", "Import from a file path")
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
						return cmd
 | 
				
			||||||
 | 
					}
 | 
				
			||||||
@@ -25,6 +25,7 @@ func RootCmd(rootcmd *cobra.Command, dockerCli command.Cli, opts RootOptions) *c
 | 
				
			|||||||
		inspectCmd(dockerCli, opts),
 | 
							inspectCmd(dockerCli, opts),
 | 
				
			||||||
		openCmd(dockerCli, opts),
 | 
							openCmd(dockerCli, opts),
 | 
				
			||||||
		traceCmd(dockerCli, opts),
 | 
							traceCmd(dockerCli, opts),
 | 
				
			||||||
 | 
							importCmd(dockerCli, opts),
 | 
				
			||||||
	)
 | 
						)
 | 
				
			||||||
 | 
					
 | 
				
			||||||
	return cmd
 | 
						return cmd
 | 
				
			||||||
 
 | 
				
			|||||||
@@ -7,6 +7,7 @@ Commands to work on build records
 | 
				
			|||||||
 | 
					
 | 
				
			||||||
| Name                                   | Description                                    |
 | 
					| Name                                   | Description                                    |
 | 
				
			||||||
|:---------------------------------------|:-----------------------------------------------|
 | 
					|:---------------------------------------|:-----------------------------------------------|
 | 
				
			||||||
 | 
					| [`import`](buildx_history_import.md)   | Import a build into Docker Desktop             |
 | 
				
			||||||
| [`inspect`](buildx_history_inspect.md) | Inspect a build                                |
 | 
					| [`inspect`](buildx_history_inspect.md) | Inspect a build                                |
 | 
				
			||||||
| [`logs`](buildx_history_logs.md)       | Print the logs of a build                      |
 | 
					| [`logs`](buildx_history_logs.md)       | Print the logs of a build                      |
 | 
				
			||||||
| [`ls`](buildx_history_ls.md)           | List build records                             |
 | 
					| [`ls`](buildx_history_ls.md)           | List build records                             |
 | 
				
			||||||
 
 | 
				
			|||||||
							
								
								
									
										16
									
								
								docs/reference/buildx_history_import.md
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										16
									
								
								docs/reference/buildx_history_import.md
									
									
									
									
									
										Normal file
									
								
							@@ -0,0 +1,16 @@
 | 
				
			|||||||
 | 
					# docker buildx history import
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					<!---MARKER_GEN_START-->
 | 
				
			||||||
 | 
					Import a build into Docker Desktop
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					### Options
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					| Name            | Type     | Default | Description                              |
 | 
				
			||||||
 | 
					|:----------------|:---------|:--------|:-----------------------------------------|
 | 
				
			||||||
 | 
					| `--builder`     | `string` |         | Override the configured builder instance |
 | 
				
			||||||
 | 
					| `-D`, `--debug` | `bool`   |         | Enable debug logging                     |
 | 
				
			||||||
 | 
					| `-f`, `--file`  | `string` |         | Import from a file path                  |
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					<!---MARKER_GEN_END-->
 | 
				
			||||||
 | 
					
 | 
				
			||||||
							
								
								
									
										21
									
								
								util/desktop/paths_darwin.go
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										21
									
								
								util/desktop/paths_darwin.go
									
									
									
									
									
										Normal file
									
								
							@@ -0,0 +1,21 @@
 | 
				
			|||||||
 | 
					package desktop
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					import (
 | 
				
			||||||
 | 
						"os"
 | 
				
			||||||
 | 
						"path/filepath"
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
						"github.com/pkg/errors"
 | 
				
			||||||
 | 
					)
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					const (
 | 
				
			||||||
 | 
						socketName = "docker-desktop-build.sock"
 | 
				
			||||||
 | 
						socketPath = "Library/Containers/com.docker.docker/Data"
 | 
				
			||||||
 | 
					)
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					func BuildServerAddr() (string, error) {
 | 
				
			||||||
 | 
						dir, err := os.UserHomeDir()
 | 
				
			||||||
 | 
						if err != nil {
 | 
				
			||||||
 | 
							return "", errors.Wrap(err, "failed to get user home directory")
 | 
				
			||||||
 | 
						}
 | 
				
			||||||
 | 
						return "unix://" + filepath.Join(dir, socketPath, socketName), nil
 | 
				
			||||||
 | 
					}
 | 
				
			||||||
							
								
								
									
										21
									
								
								util/desktop/paths_linux.go
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										21
									
								
								util/desktop/paths_linux.go
									
									
									
									
									
										Normal file
									
								
							@@ -0,0 +1,21 @@
 | 
				
			|||||||
 | 
					package desktop
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					import (
 | 
				
			||||||
 | 
						"os"
 | 
				
			||||||
 | 
						"path/filepath"
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
						"github.com/pkg/errors"
 | 
				
			||||||
 | 
					)
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					const (
 | 
				
			||||||
 | 
						socketName = "docker-desktop-build.sock"
 | 
				
			||||||
 | 
						socketPath = ".docker/desktop"
 | 
				
			||||||
 | 
					)
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					func BuildServerAddr() (string, error) {
 | 
				
			||||||
 | 
						dir, err := os.UserHomeDir()
 | 
				
			||||||
 | 
						if err != nil {
 | 
				
			||||||
 | 
							return "", errors.Wrap(err, "failed to get user home directory")
 | 
				
			||||||
 | 
						}
 | 
				
			||||||
 | 
						return "unix://" + filepath.Join(dir, socketPath, socketName), nil
 | 
				
			||||||
 | 
					}
 | 
				
			||||||
							
								
								
									
										13
									
								
								util/desktop/paths_unsupported.go
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										13
									
								
								util/desktop/paths_unsupported.go
									
									
									
									
									
										Normal file
									
								
							@@ -0,0 +1,13 @@
 | 
				
			|||||||
 | 
					//go:build !windows && !darwin && !linux
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					package desktop
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					import (
 | 
				
			||||||
 | 
						"runtime"
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
						"github.com/pkg/errors"
 | 
				
			||||||
 | 
					)
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					func BuildServerAddr() (string, error) {
 | 
				
			||||||
 | 
						return "", errors.Errorf("Docker Desktop unsupported on %s", runtime.GOOS)
 | 
				
			||||||
 | 
					}
 | 
				
			||||||
							
								
								
									
										5
									
								
								util/desktop/paths_windows.go
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										5
									
								
								util/desktop/paths_windows.go
									
									
									
									
									
										Normal file
									
								
							@@ -0,0 +1,5 @@
 | 
				
			|||||||
 | 
					package desktop
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					func BuildServerAddr() (string, error) {
 | 
				
			||||||
 | 
						return "npipe:////./pipe/dockerDesktopBuildServer", nil
 | 
				
			||||||
 | 
					}
 | 
				
			||||||
		Reference in New Issue
	
	Block a user