chore(updater): common GetServers signature

- Log warnings when running outside of CLI mode
- Remove updater CLI bool setting
- Warnings are logged in updating functions
This commit is contained in:
Quentin McGaw
2022-05-28 20:58:50 +00:00
parent 381089ebdf
commit 90c6c8485b
50 changed files with 896 additions and 325 deletions

View File

@@ -6,26 +6,22 @@ import (
"context"
"errors"
"fmt"
"net/http"
"strings"
"github.com/qdm12/gluetun/internal/models"
"github.com/qdm12/gluetun/internal/updater/openvpn"
"github.com/qdm12/gluetun/internal/updater/resolver"
"github.com/qdm12/gluetun/internal/updater/unzip"
)
var ErrNotEnoughServers = errors.New("not enough servers found")
func GetServers(ctx context.Context, unzipper unzip.Unzipper,
client *http.Client, presolver resolver.Parallel, minServers int) (
servers []models.Server, warnings []string, err error) {
func (u *Updater) GetServers(ctx context.Context, minServers int) (
servers []models.Server, err error) {
const url = "https://privado.io/apps/ovpn_configs.zip"
contents, err := unzipper.FetchAndExtract(ctx, url)
contents, err := u.unzipper.FetchAndExtract(ctx, url)
if err != nil {
return nil, nil, err
return nil, err
} else if len(contents) < minServers {
return nil, nil, fmt.Errorf("%w: %d and expected at least %d",
return nil, fmt.Errorf("%w: %d and expected at least %d",
ErrNotEnoughServers, len(contents), minServers)
}
@@ -38,12 +34,11 @@ func GetServers(ctx context.Context, unzipper unzip.Unzipper,
host, warning, err := openvpn.ExtractHost(content)
if warning != "" {
warnings = append(warnings, warning)
u.warner.Warn(warning)
}
if err != nil {
// treat error as warning and go to next file
warning := err.Error() + " in " + fileName
warnings = append(warnings, warning)
u.warner.Warn(err.Error() + " in " + fileName)
continue
}
@@ -51,26 +46,28 @@ func GetServers(ctx context.Context, unzipper unzip.Unzipper,
}
if len(hts) < minServers {
return nil, warnings, fmt.Errorf("%w: %d and expected at least %d",
return nil, fmt.Errorf("%w: %d and expected at least %d",
ErrNotEnoughServers, len(hts), minServers)
}
hosts := hts.toHostsSlice()
hostToIPs, newWarnings, err := resolveHosts(ctx, presolver, hosts, minServers)
warnings = append(warnings, newWarnings...)
hostToIPs, warnings, err := resolveHosts(ctx, u.presolver, hosts, minServers)
for _, warning := range warnings {
u.warner.Warn(warning)
}
if err != nil {
return nil, warnings, err
return nil, err
}
hts.adaptWithIPs(hostToIPs)
servers = hts.toServersSlice()
if err := setLocationInfo(ctx, client, servers); err != nil {
return nil, warnings, err
if err := setLocationInfo(ctx, u.client, servers); err != nil {
return nil, err
}
sortServers(servers)
return servers, warnings, nil
return servers, nil
}