Skip to content

Commit 6f08838

Browse files
committed
history: add multi-file/stdin import
Signed-off-by: Tonis Tiigi <[email protected]>
1 parent 963b9ca commit 6f08838

File tree

1 file changed

+51
-25
lines changed

1 file changed

+51
-25
lines changed

commands/history/import.go

+51-25
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ import (
2020
)
2121

2222
type importOptions struct {
23-
file string
23+
file []string
2424
}
2525

2626
func runImport(ctx context.Context, dockerCli command.Cli, opts importOptions) error {
@@ -42,51 +42,77 @@ func runImport(ctx context.Context, dockerCli command.Cli, opts importOptions) e
4242
Transport: tr,
4343
}
4444

45-
var rdr io.Reader = os.Stdin
46-
if opts.file != "" {
47-
f, err := os.Open(opts.file)
45+
var urls []string
46+
47+
if len(opts.file) == 0 {
48+
u, err := importFrom(ctx, client, os.Stdin)
4849
if err != nil {
49-
return errors.Wrap(err, "failed to open file")
50+
return err
51+
}
52+
urls = append(urls, u...)
53+
} else {
54+
for _, fn := range opts.file {
55+
var f *os.File
56+
var rdr io.Reader = os.Stdin
57+
if fn != "-" {
58+
f, err = os.Open(fn)
59+
if err != nil {
60+
return errors.Wrapf(err, "failed to open file %s", fn)
61+
}
62+
rdr = f
63+
}
64+
u, err := importFrom(ctx, client, rdr)
65+
if err != nil {
66+
return err
67+
}
68+
urls = append(urls, u...)
69+
if f != nil {
70+
f.Close()
71+
}
72+
}
73+
}
74+
75+
if len(urls) == 0 {
76+
return errors.New("no build records found in the bundle")
77+
}
78+
79+
for i, url := range urls {
80+
fmt.Fprintln(dockerCli.Err(), url)
81+
if i == 0 {
82+
err = browser.OpenURL(url)
5083
}
51-
defer f.Close()
52-
rdr = f
5384
}
85+
return err
86+
}
5487

88+
func importFrom(ctx context.Context, c *http.Client, rdr io.Reader) ([]string, error) {
5589
req, err := http.NewRequestWithContext(ctx, http.MethodPost, "http://docker-desktop/upload", rdr)
5690
if err != nil {
57-
return errors.Wrap(err, "failed to create request")
91+
return nil, errors.Wrap(err, "failed to create request")
5892
}
5993

60-
resp, err := client.Do(req)
94+
resp, err := c.Do(req)
6195
if err != nil {
62-
return errors.Wrap(err, "failed to send request, check if Docker Desktop is running")
96+
return nil, errors.Wrap(err, "failed to send request, check if Docker Desktop is running")
6397
}
6498
defer resp.Body.Close()
6599

66100
if resp.StatusCode != http.StatusOK {
67101
body, _ := io.ReadAll(resp.Body)
68-
return errors.Errorf("failed to import build: %s", string(body))
102+
return nil, errors.Errorf("failed to import build: %s", string(body))
69103
}
70104

71105
var refs []string
72106
dec := json.NewDecoder(resp.Body)
73107
if err := dec.Decode(&refs); err != nil {
74-
return errors.Wrap(err, "failed to decode response")
108+
return nil, errors.Wrap(err, "failed to decode response")
75109
}
76110

77-
if len(refs) == 0 {
78-
return errors.New("no build records found in the bundle")
111+
var urls []string
112+
for _, ref := range refs {
113+
urls = append(urls, desktop.BuildURL(fmt.Sprintf(".imported/_/%s", ref)))
79114
}
80-
81-
for i, ref := range refs {
82-
url := desktop.BuildURL(fmt.Sprintf(".imported/_/%s", ref))
83-
fmt.Fprintln(dockerCli.Err(), url)
84-
if i == 0 {
85-
err = browser.OpenURL(url)
86-
}
87-
}
88-
89-
return err
115+
return urls, err
90116
}
91117

92118
func importCmd(dockerCli command.Cli, _ RootOptions) *cobra.Command {
@@ -103,7 +129,7 @@ func importCmd(dockerCli command.Cli, _ RootOptions) *cobra.Command {
103129
}
104130

105131
flags := cmd.Flags()
106-
flags.StringVarP(&options.file, "file", "f", "", "Import from a file path")
132+
flags.StringArrayVarP(&options.file, "file", "f", nil, "Import from a file path")
107133

108134
return cmd
109135
}

0 commit comments

Comments
 (0)