|
| 1 | +package history |
| 2 | + |
| 3 | +import ( |
| 4 | + "context" |
| 5 | + "encoding/json" |
| 6 | + "fmt" |
| 7 | + "io" |
| 8 | + "net" |
| 9 | + "net/http" |
| 10 | + "os" |
| 11 | + "strings" |
| 12 | + |
| 13 | + remoteutil "github.com/docker/buildx/driver/remote/util" |
| 14 | + "github.com/docker/buildx/util/cobrautil/completion" |
| 15 | + "github.com/docker/buildx/util/desktop" |
| 16 | + "github.com/docker/cli/cli/command" |
| 17 | + "github.com/pkg/browser" |
| 18 | + "github.com/pkg/errors" |
| 19 | + "github.com/spf13/cobra" |
| 20 | +) |
| 21 | + |
| 22 | +type importOptions struct { |
| 23 | + file string |
| 24 | +} |
| 25 | + |
| 26 | +func runImport(ctx context.Context, _ command.Cli, opts importOptions) error { |
| 27 | + sock, err := desktop.BuildServerAddr() |
| 28 | + if err != nil { |
| 29 | + return err |
| 30 | + } |
| 31 | + |
| 32 | + tr := http.DefaultTransport.(*http.Transport).Clone() |
| 33 | + tr.DialContext = func(ctx context.Context, _, _ string) (net.Conn, error) { |
| 34 | + network, addr, ok := strings.Cut(sock, "://") |
| 35 | + if !ok { |
| 36 | + return nil, errors.Errorf("invalid endpoint address: %s", sock) |
| 37 | + } |
| 38 | + return remoteutil.DialContext(ctx, network, addr) |
| 39 | + } |
| 40 | + |
| 41 | + client := &http.Client{ |
| 42 | + Transport: tr, |
| 43 | + } |
| 44 | + |
| 45 | + var rdr io.Reader = os.Stdin |
| 46 | + if opts.file != "" { |
| 47 | + f, err := os.Open(opts.file) |
| 48 | + if err != nil { |
| 49 | + return errors.Wrap(err, "failed to open file") |
| 50 | + } |
| 51 | + defer f.Close() |
| 52 | + rdr = f |
| 53 | + } |
| 54 | + |
| 55 | + req, err := http.NewRequestWithContext(ctx, http.MethodPost, "http://docker-desktop/upload", rdr) |
| 56 | + if err != nil { |
| 57 | + return errors.Wrap(err, "failed to create request") |
| 58 | + } |
| 59 | + |
| 60 | + resp, err := client.Do(req) |
| 61 | + if err != nil { |
| 62 | + return errors.Wrap(err, "failed to send request, check if Docker Desktop is running") |
| 63 | + } |
| 64 | + defer resp.Body.Close() |
| 65 | + |
| 66 | + if resp.StatusCode != http.StatusOK { |
| 67 | + body, _ := io.ReadAll(resp.Body) |
| 68 | + return errors.Errorf("failed to import build: %s", string(body)) |
| 69 | + } |
| 70 | + |
| 71 | + var refs []string |
| 72 | + dec := json.NewDecoder(resp.Body) |
| 73 | + if err := dec.Decode(&refs); err != nil { |
| 74 | + return errors.Wrap(err, "failed to decode response") |
| 75 | + } |
| 76 | + |
| 77 | + if len(refs) == 0 { |
| 78 | + return errors.New("no build records found in the bundle") |
| 79 | + } |
| 80 | + |
| 81 | + url := desktop.BuildURL(fmt.Sprintf(".imported/_/%s", refs[0])) |
| 82 | + return browser.OpenURL(url) |
| 83 | +} |
| 84 | + |
| 85 | +func importCmd(dockerCli command.Cli, _ RootOptions) *cobra.Command { |
| 86 | + var options importOptions |
| 87 | + |
| 88 | + cmd := &cobra.Command{ |
| 89 | + Use: "import [OPTIONS] < bundle.dockerbuild", |
| 90 | + Short: "Import a build into Docker Desktop", |
| 91 | + Args: cobra.NoArgs, |
| 92 | + RunE: func(cmd *cobra.Command, args []string) error { |
| 93 | + return runImport(cmd.Context(), dockerCli, options) |
| 94 | + }, |
| 95 | + ValidArgsFunction: completion.Disable, |
| 96 | + } |
| 97 | + |
| 98 | + flags := cmd.Flags() |
| 99 | + flags.StringVarP(&options.file, "file", "f", "", "Import from a file path") |
| 100 | + |
| 101 | + return cmd |
| 102 | +} |
0 commit comments