/
file.go
96 lines (81 loc) · 1.89 KB
/
file.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
package file
import (
"io"
"net/http"
"os"
"path/filepath"
"time"
"github.com/hashicorp/go-retryablehttp"
log "github.com/sirupsen/logrus"
)
const (
RetryDuration = 5 * time.Second
CannotReadErrMsg = "cannot read artifact from disk. Error: '%v'. Perhaps it resides in another repository?"
WaitMsg = "wait: '%v' before retrying"
)
func DownloadWithRetries(url string, auth ...string) (*http.Response, error) {
req, err := http.NewRequest("GET", url, nil)
if err != nil {
return nil, err
}
if len(auth) > 0 {
req.SetBasicAuth(auth[0], auth[1])
}
retryClient := retryablehttp.NewClient()
retryClient.Logger = nil
retryClient.RetryMax = 5
retryClient.RetryWaitMin = 10 * time.Second
retryClient.RetryWaitMax = 60 * time.Second
standardClient := retryClient.StandardClient()
/* #nosec */
resp, err := standardClient.Do(req)
if err != nil {
return nil, err
}
return resp, nil
}
func Exists(f string) (int64, bool) {
fi, err := os.Stat(f)
if err != nil {
return 0, false
}
return fi.Size(), true
}
func CreateIfDoesNotExistInvalidOrEmpty(url, f string, body io.ReadCloser, invalid bool) error {
var written int64
fileSize, fileExists := Exists(f)
if !fileExists || fileSize == 0 || invalid {
dst, err := os.Create(filepath.Clean(f))
if err != nil {
return err
}
defer func() {
if err := dst.Close(); err != nil {
panic(err)
}
}()
written, err = io.Copy(dst, body)
if err != nil {
return err
}
if err := dst.Sync(); err != nil {
return err
}
}
log.Debugf("downloaded: '%s' to: '%s'. Wrote: '%d' bytes", url, f, written)
return nil
}
func EmptyFile(f string) (emptyFile bool) {
emptyFile = false
fileSize, fileExists := Exists(f)
if !fileExists {
return emptyFile
}
if fileSize == 0 {
log.Errorf("file: '%s' size is 0", f)
log.Warnf(WaitMsg, RetryDuration)
time.Sleep(RetryDuration)
emptyFile = true
}
return emptyFile
}