chore(all): use casers instead of strings.Title
- Add `golang.org/x/text` dependency - Update code to use `cases.Title(language.English)`
This commit is contained in:
@@ -6,11 +6,12 @@ import (
|
||||
"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) (
|
||||
func parseFilename(fileName, hostname string, titleCaser cases.Caser) (
|
||||
country, city string, err error) {
|
||||
const prefix = "ipvanish-"
|
||||
s := strings.TrimPrefix(fileName, prefix)
|
||||
@@ -28,11 +29,11 @@ func parseFilename(fileName, hostname string) (
|
||||
if !ok {
|
||||
return "", "", fmt.Errorf("%w: %s", errCountryCodeUnknown, countryCode)
|
||||
}
|
||||
country = strings.Title(country)
|
||||
country = titleCaser.String(country)
|
||||
|
||||
if len(parts) > 1 {
|
||||
city = strings.Join(parts[1:], " ")
|
||||
city = strings.Title(city)
|
||||
city = titleCaser.String(city)
|
||||
}
|
||||
|
||||
return country, city, nil
|
||||
|
||||
@@ -6,6 +6,8 @@ import (
|
||||
|
||||
"github.com/stretchr/testify/assert"
|
||||
"github.com/stretchr/testify/require"
|
||||
"golang.org/x/text/cases"
|
||||
"golang.org/x/text/language"
|
||||
)
|
||||
|
||||
func Test_parseFilename(t *testing.T) {
|
||||
@@ -39,7 +41,8 @@ func Test_parseFilename(t *testing.T) {
|
||||
t.Run(name, func(t *testing.T) {
|
||||
t.Parallel()
|
||||
|
||||
country, city, err := parseFilename(testCase.fileName, testCase.hostname)
|
||||
titleCaser := cases.Title(language.English)
|
||||
country, city, err := parseFilename(testCase.fileName, testCase.hostname, titleCaser)
|
||||
|
||||
if testCase.err != nil {
|
||||
require.Error(t, err)
|
||||
|
||||
@@ -12,6 +12,8 @@ import (
|
||||
"github.com/qdm12/gluetun/internal/updater/openvpn"
|
||||
"github.com/qdm12/gluetun/internal/updater/resolver"
|
||||
"github.com/qdm12/gluetun/internal/updater/unzip"
|
||||
"golang.org/x/text/cases"
|
||||
"golang.org/x/text/language"
|
||||
)
|
||||
|
||||
var ErrNotEnoughServers = errors.New("not enough servers found")
|
||||
@@ -30,6 +32,7 @@ func GetServers(ctx context.Context, unzipper unzip.Unzipper,
|
||||
|
||||
hts := make(hostToServer)
|
||||
|
||||
titleCaser := cases.Title(language.English)
|
||||
for fileName, content := range contents {
|
||||
if !strings.HasSuffix(fileName, ".ovpn") {
|
||||
continue // not an OpenVPN file
|
||||
@@ -54,7 +57,7 @@ func GetServers(ctx context.Context, unzipper unzip.Unzipper,
|
||||
continue
|
||||
}
|
||||
|
||||
country, city, err := parseFilename(fileName, hostname)
|
||||
country, city, err := parseFilename(fileName, hostname, titleCaser)
|
||||
if err != nil {
|
||||
// treat error as warning and go to next file
|
||||
warning := err.Error() + " in " + fileName
|
||||
|
||||
Reference in New Issue
Block a user