history: add multi-file/stdin import

Signed-off-by: Tonis Tiigi <tonistiigi@gmail.com>
This commit is contained in:
Tonis Tiigi 2025-03-05 11:07:53 -08:00
parent 963b9ca30d
commit cadf4a5893
No known key found for this signature in database
GPG Key ID: AFA9DE5F8AB7AF39
2 changed files with 65 additions and 39 deletions

View File

@ -20,7 +20,7 @@ import (
) )
type importOptions struct { type importOptions struct {
file string file []string
} }
func runImport(ctx context.Context, dockerCli command.Cli, opts importOptions) error { func runImport(ctx context.Context, dockerCli command.Cli, opts importOptions) error {
@ -42,53 +42,79 @@ func runImport(ctx context.Context, dockerCli command.Cli, opts importOptions) e
Transport: tr, Transport: tr,
} }
var rdr io.Reader = os.Stdin var urls []string
if opts.file != "" {
f, err := os.Open(opts.file) if len(opts.file) == 0 {
u, err := importFrom(ctx, client, os.Stdin)
if err != nil { if err != nil {
return errors.Wrap(err, "failed to open file") return err
}
urls = append(urls, u...)
} else {
for _, fn := range opts.file {
var f *os.File
var rdr io.Reader = os.Stdin
if fn != "-" {
f, err = os.Open(fn)
if err != nil {
return errors.Wrapf(err, "failed to open file %s", fn)
}
rdr = f
}
u, err := importFrom(ctx, client, rdr)
if err != nil {
return err
}
urls = append(urls, u...)
if f != nil {
f.Close()
}
} }
defer f.Close()
rdr = f
} }
req, err := http.NewRequestWithContext(ctx, http.MethodPost, "http://docker-desktop/upload", rdr) if len(urls) == 0 {
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") return errors.New("no build records found in the bundle")
} }
for i, ref := range refs { for i, url := range urls {
url := desktop.BuildURL(fmt.Sprintf(".imported/_/%s", ref))
fmt.Fprintln(dockerCli.Err(), url) fmt.Fprintln(dockerCli.Err(), url)
if i == 0 { if i == 0 {
err = browser.OpenURL(url) err = browser.OpenURL(url)
} }
} }
return err return err
} }
func importFrom(ctx context.Context, c *http.Client, rdr io.Reader) ([]string, error) {
req, err := http.NewRequestWithContext(ctx, http.MethodPost, "http://docker-desktop/upload", rdr)
if err != nil {
return nil, errors.Wrap(err, "failed to create request")
}
resp, err := c.Do(req)
if err != nil {
return nil, 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 nil, 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 nil, errors.Wrap(err, "failed to decode response")
}
var urls []string
for _, ref := range refs {
urls = append(urls, desktop.BuildURL(fmt.Sprintf(".imported/_/%s", ref)))
}
return urls, err
}
func importCmd(dockerCli command.Cli, _ RootOptions) *cobra.Command { func importCmd(dockerCli command.Cli, _ RootOptions) *cobra.Command {
var options importOptions var options importOptions
@ -103,7 +129,7 @@ func importCmd(dockerCli command.Cli, _ RootOptions) *cobra.Command {
} }
flags := cmd.Flags() flags := cmd.Flags()
flags.StringVarP(&options.file, "file", "f", "", "Import from a file path") flags.StringArrayVarP(&options.file, "file", "f", nil, "Import from a file path")
return cmd return cmd
} }

View File

@ -5,11 +5,11 @@ Import a build into Docker Desktop
### Options ### Options
| Name | Type | Default | Description | | Name | Type | Default | Description |
|:----------------|:---------|:--------|:-----------------------------------------| |:----------------|:--------------|:--------|:-----------------------------------------|
| `--builder` | `string` | | Override the configured builder instance | | `--builder` | `string` | | Override the configured builder instance |
| `-D`, `--debug` | `bool` | | Enable debug logging | | `-D`, `--debug` | `bool` | | Enable debug logging |
| `-f`, `--file` | `string` | | Import from a file path | | `-f`, `--file` | `stringArray` | | Import from a file path |
<!---MARKER_GEN_END--> <!---MARKER_GEN_END-->