Files
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

58 lines
1.6 KiB
Go

package updater
import (
"context"
"encoding/json"
"errors"
"fmt"
"github.com/qdm12/gluetun/internal/models"
"github.com/qdm12/gluetun/internal/provider/common"
)
type Provider interface {
Name() string
FetchServers(ctx context.Context, minServers int) (servers []models.Server, err error)
}
func (u *Updater) updateProvider(ctx context.Context, provider Provider,
minRatio float64,
) (err error) {
providerName := provider.Name()
existingServersCount := u.storage.GetServersCount(providerName)
minServers := int(minRatio * float64(existingServersCount))
servers, err := provider.FetchServers(ctx, minServers)
if err != nil {
if errors.Is(err, common.ErrNotEnoughServers) {
u.logger.Warn("note: if running the update manually, you can use the flag " +
"-minratio to allow the update to succeed with less servers found")
}
return fmt.Errorf("getting %s servers: %w", providerName, err)
}
for _, server := range servers {
err := server.HasMinimumInformation()
if err != nil {
serverJSON, jsonErr := json.Marshal(server)
if jsonErr != nil {
panic(jsonErr)
}
return fmt.Errorf("server %s has not enough information: %w", serverJSON, err)
}
}
if u.storage.ServersAreEqual(providerName, servers) {
return nil
}
// Note the servers variable must NOT BE MUTATED after this call,
// since the implementation does not deep copy the servers.
// TODO set in storage in provider updater directly, server by server,
// to avoid accumulating server data in memory.
err = u.storage.SetServers(providerName, servers)
if err != nil {
return fmt.Errorf("setting servers to storage: %w", err)
}
return nil
}