PIA nextgen portforward (#242)
* Split provider/pia.go in piav3.go and piav4.go * Change port forwarding signature * Enable port forwarding parameter for PIA v4 * Fix VPN gateway IP obtention * Setup HTTP client for TLS with custom cert * Error message for regions not supporting pf
This commit is contained in:
@@ -4,6 +4,8 @@ import (
|
||||
"fmt"
|
||||
"net/http"
|
||||
"time"
|
||||
|
||||
"github.com/qdm12/gluetun/internal/logging"
|
||||
)
|
||||
|
||||
// GetMessage returns a message for the user describing if there is a newer version
|
||||
@@ -30,35 +32,12 @@ func GetMessage(version, commitShort string, client *http.Client) (message strin
|
||||
if tagName == version {
|
||||
return fmt.Sprintf("You are running the latest release %s", version), nil
|
||||
}
|
||||
timeSinceRelease := formatDuration(time.Since(releaseTime))
|
||||
timeSinceRelease := logging.FormatDuration(time.Since(releaseTime))
|
||||
return fmt.Sprintf("There is a new release %s (%s) created %s ago",
|
||||
tagName, name, timeSinceRelease),
|
||||
nil
|
||||
}
|
||||
|
||||
func formatDuration(duration time.Duration) string {
|
||||
switch {
|
||||
case duration < time.Minute:
|
||||
seconds := int(duration.Round(time.Second).Seconds())
|
||||
if seconds < 2 {
|
||||
return fmt.Sprintf("%d second", seconds)
|
||||
}
|
||||
return fmt.Sprintf("%d seconds", seconds)
|
||||
case duration <= time.Hour:
|
||||
minutes := int(duration.Round(time.Minute).Minutes())
|
||||
if minutes == 1 {
|
||||
return "1 minute"
|
||||
}
|
||||
return fmt.Sprintf("%d minutes", minutes)
|
||||
case duration < 48*time.Hour:
|
||||
hours := int(duration.Truncate(time.Hour).Hours())
|
||||
return fmt.Sprintf("%d hours", hours)
|
||||
default:
|
||||
days := int(duration.Truncate(time.Hour).Hours() / 24)
|
||||
return fmt.Sprintf("%d days", days)
|
||||
}
|
||||
}
|
||||
|
||||
func getLatestRelease(client *http.Client) (tagName, name string, time time.Time, err error) {
|
||||
releases, err := getGithubReleases(client)
|
||||
if err != nil {
|
||||
|
||||
@@ -1,64 +0,0 @@
|
||||
package version
|
||||
|
||||
import (
|
||||
"testing"
|
||||
"time"
|
||||
|
||||
"github.com/stretchr/testify/assert"
|
||||
)
|
||||
|
||||
func Test_formatDuration(t *testing.T) {
|
||||
t.Parallel()
|
||||
testCases := map[string]struct {
|
||||
duration time.Duration
|
||||
s string
|
||||
}{
|
||||
"zero": {
|
||||
s: "0 second",
|
||||
},
|
||||
"one second": {
|
||||
duration: time.Second,
|
||||
s: "1 second",
|
||||
},
|
||||
"59 seconds": {
|
||||
duration: 59 * time.Second,
|
||||
s: "59 seconds",
|
||||
},
|
||||
"1 minute": {
|
||||
duration: time.Minute,
|
||||
s: "1 minute",
|
||||
},
|
||||
"2 minutes": {
|
||||
duration: 2 * time.Minute,
|
||||
s: "2 minutes",
|
||||
},
|
||||
"1 hour": {
|
||||
duration: time.Hour,
|
||||
s: "60 minutes",
|
||||
},
|
||||
"2 hours": {
|
||||
duration: 2 * time.Hour,
|
||||
s: "2 hours",
|
||||
},
|
||||
"26 hours": {
|
||||
duration: 26 * time.Hour,
|
||||
s: "26 hours",
|
||||
},
|
||||
"28 hours": {
|
||||
duration: 28 * time.Hour,
|
||||
s: "28 hours",
|
||||
},
|
||||
"55 hours": {
|
||||
duration: 55 * time.Hour,
|
||||
s: "2 days",
|
||||
},
|
||||
}
|
||||
for name, testCase := range testCases {
|
||||
testCase := testCase
|
||||
t.Run(name, func(t *testing.T) {
|
||||
t.Parallel()
|
||||
s := formatDuration(testCase.duration)
|
||||
assert.Equal(t, testCase.s, s)
|
||||
})
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user