diff --git a/src/font/download.go b/src/font/download.go index 7264b8bf..388a78c3 100644 --- a/src/font/download.go +++ b/src/font/download.go @@ -4,11 +4,13 @@ package font // component library. import ( + "context" "errors" "fmt" "io" "net/http" "net/url" + "time" ) func Download(fontPath string) ([]byte, error) { @@ -35,9 +37,14 @@ func isZipFile(data []byte) bool { } func getRemoteFile(location string) (data []byte, err error) { - var client = http.Client{} - - resp, err := client.Get(location) + client := &http.Client{} + ctx, cancelF := context.WithTimeout(context.Background(), time.Second*time.Duration(60)) + defer cancelF() + req, err := http.NewRequestWithContext(ctx, "GET", location, nil) + if err != nil { + return nil, err + } + resp, err := client.Do(req) if err != nil { return } diff --git a/src/font/nerd.go b/src/font/nerd.go index c7427bda..b246bb7a 100644 --- a/src/font/nerd.go +++ b/src/font/nerd.go @@ -1,10 +1,12 @@ package font import ( + "context" "encoding/json" "errors" "net/http" "strings" + "time" ) type release struct { @@ -21,7 +23,9 @@ func (a Asset) FilterValue() string { return a.Name } func Nerds() ([]*Asset, error) { client := &http.Client{} - req, err := http.NewRequest("GET", "https://api.github.com/repos/ryanoasis/nerd-fonts/releases/latest", nil) + 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) if err != nil { return nil, err }