Files
gluetun/internal/provider/ipvanish/updater/filename.go
Quentin McGaw ebd94723c1 chore(updater): incorporate FetchServers method in Provider interface
- Each provider interface can now fetch updated servers data
- Rename each provider updater subpackage name to `updater`
- Updater constructor does not take a settings struct
- Updater update method takes in a slice of provider strings
2022-06-09 23:47:41 +00:00

41 lines
915 B
Go

package updater
import (
"errors"
"fmt"
"strings"
"github.com/qdm12/gluetun/internal/constants"
"golang.org/x/text/cases"
)
var errCountryCodeUnknown = errors.New("country code is unknown")
func parseFilename(fileName, hostname string, titleCaser cases.Caser) (
country, city string, err error) {
const prefix = "ipvanish-"
s := strings.TrimPrefix(fileName, prefix)
const ext = ".ovpn"
host := strings.Split(hostname, ".")[0]
suffix := "-" + host + ext
s = strings.TrimSuffix(s, suffix)
parts := strings.Split(s, "-")
countryCodes := constants.CountryCodes()
countryCode := strings.ToLower(parts[0])
country, ok := countryCodes[countryCode]
if !ok {
return "", "", fmt.Errorf("%w: %s", errCountryCodeUnknown, countryCode)
}
country = titleCaser.String(country)
if len(parts) > 1 {
city = strings.Join(parts[1:], " ")
city = titleCaser.String(city)
}
return country, city, nil
}