mirror of
https://github.com/outbackdingo/debos.git
synced 2026-01-27 10:18:47 +00:00
This change allows to import and use core modules from "debos" package and actions from "actions" package. Add prefix 'debos.' to all symbols imported from core "debos" package to satisfy syntax check. Package "actions" shouldn't keep any common function or structure: - function 'DownloadHttpUrl()' is moved from 'DownloadAction' to "debos" package. New file 'net.go' has been added to keep a common network functions in core part of debos. - move common functions for extracting files from 'UnpackAction' to 'debos' package. File 'archiver.go' has been added for common operations on archives. Signed-off-by: Denis Pynkin <denis.pynkin@collabora.com>
46 lines
951 B
Go
46 lines
951 B
Go
package debos
|
|
|
|
import (
|
|
"fmt"
|
|
"io"
|
|
"log"
|
|
"net/http"
|
|
"os"
|
|
)
|
|
|
|
// Function for downloading single file object with http(s) protocol
|
|
func DownloadHttpUrl(url, filename string) error {
|
|
log.Printf("Download started: '%s' -> '%s'\n", url, filename)
|
|
|
|
// TODO: Proxy support?
|
|
|
|
// Check if file object already exists.
|
|
fi, err := os.Stat(filename)
|
|
if !os.IsNotExist(err) && !fi.Mode().IsRegular() {
|
|
return fmt.Errorf("Failed to download '%s': '%s' exists and it is not a regular file\n", url, filename)
|
|
}
|
|
|
|
resp, err := http.Get(url)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
defer resp.Body.Close()
|
|
|
|
if resp.StatusCode != http.StatusOK {
|
|
return fmt.Errorf("Url '%s' returned status code %d (%s)\n", url, resp.StatusCode, http.StatusText(resp.StatusCode))
|
|
}
|
|
|
|
// Output file
|
|
output, err := os.Create(filename)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
defer output.Close()
|
|
|
|
if _, err := io.Copy(output, resp.Body); err != nil {
|
|
return err
|
|
}
|
|
|
|
return nil
|
|
}
|