mirror of
https://github.com/JanDeDobbeleer/oh-my-posh.git
synced 2024-12-27 20:09:39 -08:00
parent
7d5a6b881e
commit
edf93c3883
|
@ -6,6 +6,8 @@ import (
|
|||
"net/http"
|
||||
"strconv"
|
||||
"time"
|
||||
|
||||
"github.com/jandedobbeleer/oh-my-posh/src/platform"
|
||||
)
|
||||
|
||||
type codePoints map[int]int
|
||||
|
@ -13,7 +15,6 @@ type codePoints map[int]int
|
|||
func getGlyphCodePoints() (codePoints, error) {
|
||||
var codePoints = make(codePoints)
|
||||
|
||||
client := &http.Client{}
|
||||
ctx, cncl := context.WithTimeout(context.Background(), time.Millisecond*time.Duration(5000))
|
||||
defer cncl()
|
||||
|
||||
|
@ -22,7 +23,7 @@ func getGlyphCodePoints() (codePoints, error) {
|
|||
return codePoints, &ConnectionError{reason: err.Error()}
|
||||
}
|
||||
|
||||
response, err := client.Do(request)
|
||||
response, err := platform.Client.Do(request)
|
||||
if err != nil {
|
||||
return codePoints, err
|
||||
}
|
||||
|
|
|
@ -8,10 +8,10 @@ import (
|
|||
"errors"
|
||||
"fmt"
|
||||
"io"
|
||||
"net"
|
||||
"net/http"
|
||||
"net/url"
|
||||
"time"
|
||||
|
||||
"github.com/jandedobbeleer/oh-my-posh/src/platform"
|
||||
)
|
||||
|
||||
func Download(fontPath string) ([]byte, error) {
|
||||
|
@ -38,20 +38,11 @@ func isZipFile(data []byte) bool {
|
|||
}
|
||||
|
||||
func getRemoteFile(location string) (data []byte, err error) {
|
||||
client := &http.Client{
|
||||
Transport: &http.Transport{
|
||||
Dial: (&net.Dialer{
|
||||
Timeout: 10 * time.Second,
|
||||
}).Dial,
|
||||
TLSHandshakeTimeout: 10 * time.Second,
|
||||
ResponseHeaderTimeout: 10 * time.Second,
|
||||
},
|
||||
}
|
||||
req, err := http.NewRequestWithContext(context.Background(), "GET", location, nil)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
resp, err := client.Do(req)
|
||||
resp, err := platform.Client.Do(req)
|
||||
if err != nil {
|
||||
return
|
||||
}
|
||||
|
|
|
@ -7,6 +7,8 @@ import (
|
|||
"net/http"
|
||||
"strings"
|
||||
"time"
|
||||
|
||||
"github.com/jandedobbeleer/oh-my-posh/src/platform"
|
||||
)
|
||||
|
||||
type release struct {
|
||||
|
@ -22,7 +24,6 @@ type Asset struct {
|
|||
func (a Asset) FilterValue() string { return a.Name }
|
||||
|
||||
func Nerds() ([]*Asset, error) {
|
||||
client := &http.Client{}
|
||||
ctx, cancelF := context.WithTimeout(context.Background(), time.Second*time.Duration(20))
|
||||
defer cancelF()
|
||||
req, err := http.NewRequestWithContext(ctx, "GET", "https://api.github.com/repos/ryanoasis/nerd-fonts/releases/latest", nil)
|
||||
|
@ -30,7 +31,7 @@ func Nerds() ([]*Asset, error) {
|
|||
return nil, err
|
||||
}
|
||||
req.Header.Add("Accept", "application/vnd.github.v3+json")
|
||||
response, err := client.Do(req)
|
||||
response, err := platform.Client.Do(req)
|
||||
if err != nil || response.StatusCode != http.StatusOK {
|
||||
return nil, errors.New("failed to get nerd fonts release")
|
||||
}
|
||||
|
|
|
@ -1,7 +1,9 @@
|
|||
package platform
|
||||
|
||||
import (
|
||||
"net"
|
||||
"net/http"
|
||||
"time"
|
||||
)
|
||||
|
||||
// Inspired by: https://www.thegreatcodeadventure.com/mocking-http-requests-in-golang/
|
||||
|
@ -11,5 +13,13 @@ type httpClient interface {
|
|||
}
|
||||
|
||||
var (
|
||||
client httpClient = &http.Client{}
|
||||
defaultTransport http.RoundTripper = &http.Transport{
|
||||
Proxy: http.ProxyFromEnvironment,
|
||||
Dial: (&net.Dialer{
|
||||
Timeout: 10 * time.Second,
|
||||
}).Dial,
|
||||
TLSHandshakeTimeout: 10 * time.Second,
|
||||
ResponseHeaderTimeout: 10 * time.Second,
|
||||
}
|
||||
Client httpClient = &http.Client{Transport: defaultTransport}
|
||||
)
|
||||
|
|
|
@ -688,7 +688,7 @@ func (env *Shell) HTTPRequest(targetURL string, body io.Reader, timeout int, req
|
|||
dump, _ := httputil.DumpRequestOut(request, true)
|
||||
env.Debug(string(dump))
|
||||
}
|
||||
response, err := client.Do(request)
|
||||
response, err := Client.Do(request)
|
||||
if err != nil {
|
||||
env.Error(err)
|
||||
return nil, env.unWrapError(err)
|
||||
|
|
Loading…
Reference in a new issue