1
0
mirror of https://github.com/SAP/jenkins-library.git synced 2024-12-14 11:03:09 +02:00

Support nested folders when performing a http download into a file (#1206)

This commit is contained in:
Marcus Holl 2020-02-25 16:52:12 +01:00 committed by GitHub
parent 190a4708a2
commit 4835fbffab
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 9 additions and 4 deletions

View File

@ -1,11 +1,11 @@
package http
import (
"github.com/pkg/errors"
"io"
"net/http"
"os"
"github.com/pkg/errors"
"path/filepath"
)
//Downloader ...
@ -26,7 +26,12 @@ func (c *Client) DownloadRequest(method, url, filename string, header http.Heade
return errors.Wrapf(err, "HTTP %v request to %v failed with error", method, url)
}
defer response.Body.Close()
parent := filepath.Dir(filename)
if len(parent) > 0 {
if err = os.MkdirAll(parent, 0775); err != nil {
return err
}
}
fileHandler, err := os.Create(filename)
if err != nil {
return errors.Wrapf(err, "unable to create file %v", filename)

View File

@ -30,7 +30,7 @@ func TestDownloadRequest(t *testing.T) {
}
// clean up tmp dir
defer os.RemoveAll(workingDir)
targetFile := filepath.Join(workingDir, "abc.xml")
targetFile := filepath.Join(workingDir, "abc/123/abc.xml")
// function under test
err = client.DownloadFile(server.URL, targetFile, nil, nil)