forked from zyedidia/eget
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdl.go
121 lines (93 loc) · 2.57 KB
/
dl.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
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
package main
import (
"encoding/json"
"fmt"
"io"
"net/http"
"os"
"time"
pb "github.com/schollz/progressbar/v3"
)
func SetAuthHeader(req *http.Request) *http.Request {
hasGithubTokenEnv := os.Getenv("GITHUB_TOKEN") != ""
hasEgetGithubTokenEnv := os.Getenv("EGET_GITHUB_TOKEN") != ""
hasTokenEnvVar := hasGithubTokenEnv || hasEgetGithubTokenEnv
githubEnvToken := ""
if hasEgetGithubTokenEnv && githubEnvToken == "" {
githubEnvToken = os.Getenv("EGET_GITHUB_TOKEN")
}
if hasGithubTokenEnv && githubEnvToken == "" {
githubEnvToken = os.Getenv("GITHUB_TOKEN")
}
if req.URL.Scheme == "https" && req.Host == "api.github.com" && hasTokenEnvVar {
req.Header.Set("Authorization", fmt.Sprintf("token %s", githubEnvToken))
}
return req
}
func Get(url string) (*http.Response, error) {
req, err := http.NewRequest("GET", url, nil)
if err != nil {
return nil, err
}
req = SetAuthHeader(req)
proxyClient := &http.Client{Transport: &http.Transport{Proxy: http.ProxyFromEnvironment}}
return proxyClient.Do(req)
}
type RateLimitJson struct {
Resources map[string]RateLimit
}
type RateLimit struct {
Limit int
Remaining int
Reset int64
}
func (r RateLimit) ResetTime() time.Time {
return time.Unix(r.Reset, 0)
}
func (r RateLimit) String() string {
return fmt.Sprintf("Limit: %d, Remaining: %d, Reset: %v", r.Limit, r.Remaining, r.ResetTime())
}
func GetRateLimit() (RateLimit, error) {
url := "https://api.github.com/rate_limit"
req, err := http.NewRequest("GET", url, nil)
if err != nil {
return RateLimit{}, err
}
req = SetAuthHeader(req)
req.Header.Set("Accept", "application/vnd.github.v3+json")
resp, err := http.DefaultClient.Do(req)
if err != nil {
return RateLimit{}, err
}
defer resp.Body.Close()
b, err := io.ReadAll(resp.Body)
if err != nil {
return RateLimit{}, err
}
var parsed RateLimitJson
err = json.Unmarshal(b, &parsed)
return parsed.Resources["core"], err
}
// Download the file at 'url' and write the http response body to 'out'. The
// 'getbar' function allows the caller to construct a progress bar given the
// size of the file being downloaded, and the download will write to the
// returned progress bar.
func Download(url string, out io.Writer, getbar func(size int64) *pb.ProgressBar) error {
if IsLocalFile(url) {
f, err := os.Open(url)
if err != nil {
return err
}
defer f.Close()
_, err = io.Copy(out, f)
return err
}
resp, err := Get(url)
if err != nil {
return err
}
defer resp.Body.Close()
bar := getbar(resp.ContentLength)
_, err = io.Copy(io.MultiWriter(out, bar), resp.Body)
return err
}