@@ -11,7 +11,6 @@ import (
1111 "net/url"
1212 "os"
1313 "os/exec"
14- "path/filepath"
1514 "slices"
1615 "strings"
1716 "time"
@@ -180,7 +179,7 @@ func makeRequest(ctx context.Context, r *Request, c *client.Client, req *http.Re
180179 p .Flush ()
181180 }
182181
183- body , err := formatResponse (ctx , r , resp , r . PrinterHandle . Stdout () )
182+ body , err := formatResponse (ctx , r , resp )
184183 if err != nil {
185184 return 0 , err
186185 }
@@ -196,51 +195,23 @@ func makeRequest(ctx context.Context, r *Request, c *client.Client, req *http.Re
196195 return exitCode , nil
197196}
198197
199- func formatResponse (ctx context.Context , r * Request , resp * http.Response , p * core. Printer ) (io.Reader , error ) {
198+ func formatResponse (ctx context.Context , r * Request , resp * http.Response ) (io.Reader , error ) {
200199 output , err := getOutputValue (r , resp .Header )
201200 if err != nil {
202201 return nil , err
203202 }
204203
205204 if output != "" && r .Output != "-" {
206- f , err := os .Create (output )
207- if err != nil {
208- return nil , err
209- }
210- defer f .Close ()
211- name , err := filepath .Abs (f .Name ())
212- if err != nil {
213- return nil , err
214- }
215-
216- // Optionally show a progress bar/spinner on stderr.
217- var body io.Reader = resp .Body
218- if r .Verbosity > core .VSilent && core .IsStderrTerm {
219- p := r .PrinterHandle .Stderr ()
220- contentLength := resp .ContentLength
221- if contentLength > 0 {
222- pb := newProgressBar (resp .Body , p , contentLength )
223- defer func () { pb .Close (name , err ) }()
224- body = pb
225- } else {
226- ps := newProgressSpinner (resp .Body , p )
227- defer func () { ps .Close (name , err ) }()
228- body = ps
229- }
230- }
231-
232- if _ , err = io .Copy (f , body ); err != nil {
233- return nil , err
234- }
235-
236- err = f .Sync ()
237- return nil , err
205+ size := resp .ContentLength
206+ p := r .PrinterHandle .Stderr ()
207+ return nil , writeOutputToFile (output , resp .Body , size , p , r .Verbosity )
238208 }
239209
240210 if r .Format == core .FormatOff || (! core .IsStdoutTerm && r .Format != core .FormatOn ) {
241211 return resp .Body , nil
242212 }
243213
214+ p := r .PrinterHandle .Stdout ()
244215 contentType := getContentType (resp .Header )
245216 switch contentType {
246217 case TypeUnknown :
@@ -387,67 +358,6 @@ func addHeader(headers []core.KeyVal, h core.KeyVal) []core.KeyVal {
387358 return slices .Insert (headers , i , h )
388359}
389360
390- func getOutputValue (r * Request , hdrs http.Header ) (string , error ) {
391- if r .Output != "" {
392- // Output was provided directly.
393- return r .Output , nil
394- }
395- if ! r .OutputDir {
396- // Remote output option wasn't provided, return an empty string.
397- return "" , nil
398- }
399-
400- // Attempt to get filename from the Content-Disposition header first.
401- cdName := getContentDispositionFilename (hdrs )
402- if cdName != "" {
403- return cdName , nil
404- }
405-
406- // Get the final path component as the file name.
407- path := r .URL .Path
408- if ! strings .HasPrefix (path , "/" ) {
409- path = "/" + path
410- }
411- for path != "" {
412- var after string
413- path , after , _ = cutLast (path , "/" )
414- if after != "" {
415- return after , nil
416- }
417-
418- }
419-
420- // Fallback to the hostname as the file path and emit a warning.
421- host := r .URL .Hostname ()
422- if host != "" {
423- return host , nil
424- }
425-
426- return "" , errNoInferFilePath {}
427- }
428-
429- func getContentDispositionFilename (hdrs http.Header ) string {
430- cd := hdrs .Get ("Content-Disposition" )
431- if cd == "" {
432- return ""
433- }
434-
435- _ , params , err := mime .ParseMediaType (cd )
436- if err != nil {
437- return ""
438- }
439-
440- return params ["filename" ]
441- }
442-
443- func cutLast (s , sep string ) (string , string , bool ) {
444- idx := strings .LastIndex (s , sep )
445- if idx < 0 {
446- return s , "" , false
447- }
448- return s [:idx ], s [idx + 1 :], true
449- }
450-
451361// isCertificateErr returns true if the error has to do with TLS cert validation.
452362func isCertificateErr (err error ) bool {
453363 var urlErr * url.Error
@@ -477,20 +387,3 @@ func printInsecureMsg(p *core.Printer) {
477387 p .Reset ()
478388 p .WriteString ("'.\n " )
479389}
480-
481- type errNoInferFilePath struct {}
482-
483- func (err errNoInferFilePath ) Error () string {
484- return "unable to infer a file name for the output\n \n To specify an exact path, try '--output <PATH>'"
485- }
486-
487- func (err errNoInferFilePath ) PrintTo (p * core.Printer ) {
488- p .WriteString ("unable to infer a file name for the output\n \n " )
489-
490- p .WriteString ("To specify an exact path, try '" )
491- p .Set (core .Bold )
492- p .WriteString ("--output" )
493- p .Reset ()
494- p .WriteString (" <PATH>" )
495- p .WriteString ("'" )
496- }
0 commit comments