From 92bcef0b1c2a5ecaf1e68ac61312bbd368281ae5 Mon Sep 17 00:00:00 2001 From: Quentin McGaw Date: Fri, 26 Feb 2021 13:21:55 +0000 Subject: [PATCH] Maintenance: unique choices from hardcoded servers --- internal/constants/cyberghost.go | 31 +++++++-------------- internal/constants/mullvad.go | 46 +++++++++++--------------------- internal/constants/nordvpn.go | 2 +- internal/constants/pia.go | 2 +- internal/constants/privado.go | 2 +- internal/constants/purevpn.go | 6 ++--- internal/constants/surfshark.go | 2 +- internal/constants/torguard.go | 6 ++--- internal/constants/unique.go | 21 +++++++++++++++ internal/constants/vyprvpn.go | 2 +- internal/constants/windscribe.go | 6 ++--- 11 files changed, 60 insertions(+), 66 deletions(-) create mode 100644 internal/constants/unique.go diff --git a/internal/constants/cyberghost.go b/internal/constants/cyberghost.go index 0974f621..97df6223 100644 --- a/internal/constants/cyberghost.go +++ b/internal/constants/cyberghost.go @@ -2,7 +2,6 @@ package constants import ( "net" - "sort" "github.com/qdm12/gluetun/internal/models" ) @@ -13,31 +12,21 @@ const ( ) func CyberghostRegionChoices() (choices []string) { - uniqueChoices := map[string]struct{}{} - for _, server := range CyberghostServers() { - uniqueChoices[server.Region] = struct{}{} + servers := CyberghostServers() + choices = make([]string, len(servers)) + for i := range servers { + choices[i] = servers[i].Region } - for choice := range uniqueChoices { - choices = append(choices, choice) - } - sort.Slice(choices, func(i, j int) bool { - return choices[i] < choices[j] - }) - return choices + return makeUnique(choices) } func CyberghostGroupChoices() (choices []string) { - uniqueChoices := map[string]struct{}{} - for _, server := range CyberghostServers() { - uniqueChoices[server.Group] = struct{}{} + servers := CyberghostServers() + choices = make([]string, len(servers)) + for i := range servers { + choices[i] = servers[i].Group } - for choice := range uniqueChoices { - choices = append(choices, choice) - } - sort.Slice(choices, func(i, j int) bool { - return choices[i] < choices[j] - }) - return choices + return makeUnique(choices) } //nolint:lll diff --git a/internal/constants/mullvad.go b/internal/constants/mullvad.go index 6f26ab23..0304cdba 100644 --- a/internal/constants/mullvad.go +++ b/internal/constants/mullvad.go @@ -2,7 +2,6 @@ package constants import ( "net" - "sort" "github.com/qdm12/gluetun/internal/models" ) @@ -13,45 +12,30 @@ const ( ) func MullvadCountryChoices() (choices []string) { - uniqueChoices := map[string]struct{}{} - for _, server := range MullvadServers() { - uniqueChoices[server.Country] = struct{}{} + servers := MullvadServers() + choices = make([]string, len(servers)) + for i := range servers { + choices[i] = servers[i].Country } - for choice := range uniqueChoices { - choices = append(choices, choice) - } - sort.Slice(choices, func(i, j int) bool { - return choices[i] < choices[j] - }) - return choices + return makeUnique(choices) } func MullvadCityChoices() (choices []string) { - uniqueChoices := map[string]struct{}{} - for _, server := range MullvadServers() { - uniqueChoices[server.City] = struct{}{} + servers := MullvadServers() + choices = make([]string, len(servers)) + for i := range servers { + choices[i] = servers[i].City } - for choice := range uniqueChoices { - choices = append(choices, choice) - } - sort.Slice(choices, func(i, j int) bool { - return choices[i] < choices[j] - }) - return choices + return makeUnique(choices) } func MullvadISPChoices() (choices []string) { - uniqueChoices := map[string]struct{}{} - for _, server := range MullvadServers() { - uniqueChoices[server.ISP] = struct{}{} + servers := MullvadServers() + choices = make([]string, len(servers)) + for i := range servers { + choices[i] = servers[i].ISP } - for choice := range uniqueChoices { - choices = append(choices, choice) - } - sort.Slice(choices, func(i, j int) bool { - return choices[i] < choices[j] - }) - return choices + return makeUnique(choices) } //nolint:dupl,lll diff --git a/internal/constants/nordvpn.go b/internal/constants/nordvpn.go index c489fe44..0b246310 100644 --- a/internal/constants/nordvpn.go +++ b/internal/constants/nordvpn.go @@ -18,7 +18,7 @@ func NordvpnRegionChoices() (choices []string) { for i := range servers { choices[i] = servers[i].Region } - return choices + return makeUnique(choices) } //nolint:gomnd diff --git a/internal/constants/pia.go b/internal/constants/pia.go index 77cf23cf..88864deb 100644 --- a/internal/constants/pia.go +++ b/internal/constants/pia.go @@ -22,7 +22,7 @@ func PIAGeoChoices() (choices []string) { for i := range servers { choices[i] = servers[i].Region } - return choices + return makeUnique(choices) } //nolint:lll diff --git a/internal/constants/privado.go b/internal/constants/privado.go index 682aefcd..666c70d6 100644 --- a/internal/constants/privado.go +++ b/internal/constants/privado.go @@ -17,7 +17,7 @@ func PrivadoHostnameChoices() (choices []string) { for i := range servers { choices[i] = servers[i].Hostname } - return choices + return makeUnique(choices) } func PrivadoServers() []models.PrivadoServer { diff --git a/internal/constants/purevpn.go b/internal/constants/purevpn.go index d5adb471..8001abdd 100644 --- a/internal/constants/purevpn.go +++ b/internal/constants/purevpn.go @@ -20,7 +20,7 @@ func PurevpnRegionChoices() (choices []string) { for i := range servers { choices[i] = servers[i].Region } - return choices + return makeUnique(choices) } func PurevpnCountryChoices() (choices []string) { @@ -29,7 +29,7 @@ func PurevpnCountryChoices() (choices []string) { for i := range servers { choices[i] = servers[i].Country } - return choices + return makeUnique(choices) } func PurevpnCityChoices() (choices []string) { @@ -38,7 +38,7 @@ func PurevpnCityChoices() (choices []string) { for i := range servers { choices[i] = servers[i].City } - return choices + return makeUnique(choices) } //nolint:lll diff --git a/internal/constants/surfshark.go b/internal/constants/surfshark.go index e9c9e14b..4b199ac6 100644 --- a/internal/constants/surfshark.go +++ b/internal/constants/surfshark.go @@ -18,7 +18,7 @@ func SurfsharkRegionChoices() (choices []string) { for i := range servers { choices[i] = servers[i].Region } - return choices + return makeUnique(choices) } //nolint:lll diff --git a/internal/constants/torguard.go b/internal/constants/torguard.go index 698aebfe..6ea8f788 100644 --- a/internal/constants/torguard.go +++ b/internal/constants/torguard.go @@ -18,7 +18,7 @@ func TorguardCountryChoices() (choices []string) { for i := range servers { choices[i] = servers[i].Country } - return choices + return makeUnique(choices) } func TorguardCityChoices() (choices []string) { @@ -27,7 +27,7 @@ func TorguardCityChoices() (choices []string) { for i := range servers { choices[i] = servers[i].City } - return choices + return makeUnique(choices) } func TorguardHostnamesChoices() (choices []string) { @@ -36,7 +36,7 @@ func TorguardHostnamesChoices() (choices []string) { for i := range servers { choices[i] = servers[i].Hostname } - return choices + return makeUnique(choices) } //nolint:lll diff --git a/internal/constants/unique.go b/internal/constants/unique.go new file mode 100644 index 00000000..fbe14ebf --- /dev/null +++ b/internal/constants/unique.go @@ -0,0 +1,21 @@ +package constants + +import "sort" + +func makeUnique(slice []string) (uniques []string) { + set := make(map[string]struct{}, len(slice)) + for _, element := range slice { + set[element] = struct{}{} + } + + uniques = make([]string, 0, len(set)) + for element := range set { + uniques = append(uniques, element) + } + + sort.Slice(uniques, func(i, j int) bool { + return uniques[i] < uniques[j] + }) + + return uniques +} diff --git a/internal/constants/vyprvpn.go b/internal/constants/vyprvpn.go index b36feb08..338cbd55 100644 --- a/internal/constants/vyprvpn.go +++ b/internal/constants/vyprvpn.go @@ -17,7 +17,7 @@ func VyprvpnRegionChoices() (choices []string) { for i := range servers { choices[i] = servers[i].Region } - return choices + return makeUnique(choices) } func VyprvpnServers() []models.VyprvpnServer { diff --git a/internal/constants/windscribe.go b/internal/constants/windscribe.go index 380eeefc..45740227 100644 --- a/internal/constants/windscribe.go +++ b/internal/constants/windscribe.go @@ -18,7 +18,7 @@ func WindscribeRegionChoices() (choices []string) { for i := range servers { choices[i] = servers[i].Region } - return choices + return makeUnique(choices) } func WindscribeCityChoices() (choices []string) { @@ -27,7 +27,7 @@ func WindscribeCityChoices() (choices []string) { for i := range servers { choices[i] = servers[i].City } - return choices + return makeUnique(choices) } func WindscribeHostnameChoices() (choices []string) { @@ -36,7 +36,7 @@ func WindscribeHostnameChoices() (choices []string) { for i := range servers { choices[i] = servers[i].Hostname } - return choices + return makeUnique(choices) } func WindscribeServers() []models.WindscribeServer {