diff --git a/src/engine/migrate_glyphs.go b/src/engine/migrate_glyphs.go index fa904f9c..bbb6358f 100644 --- a/src/engine/migrate_glyphs.go +++ b/src/engine/migrate_glyphs.go @@ -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 } diff --git a/src/font/download.go b/src/font/download.go index c2eb0c75..46effe87 100644 --- a/src/font/download.go +++ b/src/font/download.go @@ -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 } diff --git a/src/font/nerd.go b/src/font/nerd.go index b246bb7a..7646ec9b 100644 --- a/src/font/nerd.go +++ b/src/font/nerd.go @@ -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") } diff --git a/src/platform/httpclient.go b/src/platform/httpclient.go index 267751da..7280f5a3 100644 --- a/src/platform/httpclient.go +++ b/src/platform/httpclient.go @@ -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} ) diff --git a/src/platform/shell.go b/src/platform/shell.go index bda7534d..be5f33ea 100644 --- a/src/platform/shell.go +++ b/src/platform/shell.go @@ -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)