@@ -20,7 +20,7 @@ import (
20
20
)
21
21
22
22
type importOptions struct {
23
- file string
23
+ file [] string
24
24
}
25
25
26
26
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
42
42
Transport : tr ,
43
43
}
44
44
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 )
48
49
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 )
50
83
}
51
- defer f .Close ()
52
- rdr = f
53
84
}
85
+ return err
86
+ }
54
87
88
+ func importFrom (ctx context.Context , c * http.Client , rdr io.Reader ) ([]string , error ) {
55
89
req , err := http .NewRequestWithContext (ctx , http .MethodPost , "http://docker-desktop/upload" , rdr )
56
90
if err != nil {
57
- return errors .Wrap (err , "failed to create request" )
91
+ return nil , errors .Wrap (err , "failed to create request" )
58
92
}
59
93
60
- resp , err := client .Do (req )
94
+ resp , err := c .Do (req )
61
95
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" )
63
97
}
64
98
defer resp .Body .Close ()
65
99
66
100
if resp .StatusCode != http .StatusOK {
67
101
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 ))
69
103
}
70
104
71
105
var refs []string
72
106
dec := json .NewDecoder (resp .Body )
73
107
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" )
75
109
}
76
110
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 )))
79
114
}
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
90
116
}
91
117
92
118
func importCmd (dockerCli command.Cli , _ RootOptions ) * cobra.Command {
@@ -103,7 +129,7 @@ func importCmd(dockerCli command.Cli, _ RootOptions) *cobra.Command {
103
129
}
104
130
105
131
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" )
107
133
108
134
return cmd
109
135
}
0 commit comments