Files
gluetun/internal/updater/openvpn/fetch.go
T
Quentin McGaw 4a78989d9d chore: do not use sentinel errors when unneeded
- main reason being it's a burden to always define sentinel errors at global scope, wrap them with `%w` instead of using a string directly
- only use sentinel errors when it has to be checked using `errors.Is`
- replace all usage of these sentinel errors in `fmt.Errorf` with direct strings that were in the sentinel error
- exclude the sentinel error definition requirement from .golangci.yml
- update unit tests to use ContainersError instead of ErrorIs so it stays as a "not a change detector test" without requiring a sentinel error
2026-05-02 03:29:46 +00:00

44 lines
817 B
Go

package openvpn
import (
"context"
"fmt"
"io"
"net/http"
)
func FetchFile(ctx context.Context, client *http.Client, url string) (
host string, err error,
) {
b, err := fetchData(ctx, client, url)
if err != nil {
return "", err
}
const rejectIP = true
const rejectDomain = false
hosts := extractRemoteHosts(b, rejectIP, rejectDomain)
if len(hosts) == 0 {
return "", fmt.Errorf("remote host not found for url %s", url)
}
return hosts[0], nil
}
func fetchData(ctx context.Context, client *http.Client, url string) (
b []byte, err error,
) {
request, err := http.NewRequestWithContext(ctx, http.MethodGet, url, nil)
if err != nil {
return nil, err
}
response, err := client.Do(request)
if err != nil {
return nil, err
}
defer response.Body.Close()
return io.ReadAll(response.Body)
}