-
Notifications
You must be signed in to change notification settings - Fork 43
/
dl.go
164 lines (135 loc) · 3.5 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
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
package main
import (
"crypto/tls"
"encoding/json"
"errors"
"fmt"
"io"
"net/http"
"os"
"strings"
"time"
pb "github.com/schollz/progressbar/v3"
"github.com/zyedidia/eget/home"
)
func tokenFrom(s string) (string, error) {
if strings.HasPrefix(s, "@") {
f, err := home.Expand(s[1:])
if err != nil {
return "", err
}
b, err := os.ReadFile(f)
return strings.TrimRight(string(b), "\r\n"), nil
}
return s, nil
}
var ErrNoToken = errors.New("no github token")
func getGithubToken() (string, error) {
if os.Getenv("EGET_GITHUB_TOKEN") != "" {
return tokenFrom(os.Getenv("EGET_GITHUB_TOKEN"))
}
if os.Getenv("GITHUB_TOKEN") != "" {
return tokenFrom(os.Getenv("GITHUB_TOKEN"))
}
return "", ErrNoToken
}
func SetAuthHeader(req *http.Request) *http.Request {
token, err := getGithubToken()
if err != nil && !errors.Is(err, ErrNoToken) {
fmt.Fprintln(os.Stderr, "warning: not using github token:", err)
}
if req.URL.Scheme == "https" && req.Host == "api.github.com" && err == nil {
if opts.DisableSSL {
fmt.Fprintln(os.Stderr, "error: cannot use GitHub token if SSL verification is disabled")
os.Exit(1)
}
req.Header.Set("Authorization", fmt.Sprintf("token %s", token))
}
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,
TLSClientConfig: &tls.Config{InsecureSkipVerify: opts.DisableSSL},
}}
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 {
now := time.Now()
rtime := r.ResetTime()
if rtime.Before(now) {
return fmt.Sprintf("Limit: %d, Remaining: %d, Reset: %v", r.Limit, r.Remaining, rtime)
} else {
return fmt.Sprintf(
"Limit: %d, Remaining: %d, Reset: %v (%v)",
r.Limit, r.Remaining, rtime, rtime.Sub(now).Round(time.Second),
)
}
}
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()
if resp.StatusCode != http.StatusOK {
body, err := io.ReadAll(resp.Body)
if err != nil {
return err
}
return fmt.Errorf("download error: %d: %s", resp.StatusCode, body)
}
bar := getbar(resp.ContentLength)
_, err = io.Copy(io.MultiWriter(out, bar), resp.Body)
return err
}