Maint: move duration formatting to qdm12/golibs
This commit is contained in:
@@ -1,31 +0,0 @@
|
||||
package format
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"time"
|
||||
)
|
||||
|
||||
func Duration(duration time.Duration) string {
|
||||
switch {
|
||||
case duration < time.Minute:
|
||||
seconds := int(duration.Round(time.Second).Seconds())
|
||||
const two = 2
|
||||
if seconds < two {
|
||||
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:
|
||||
const hoursInDay = 24
|
||||
days := int(duration.Truncate(time.Hour).Hours() / hoursInDay)
|
||||
return fmt.Sprintf("%d days", days)
|
||||
}
|
||||
}
|
||||
@@ -1,64 +0,0 @@
|
||||
package format
|
||||
|
||||
import (
|
||||
"testing"
|
||||
"time"
|
||||
|
||||
"github.com/stretchr/testify/assert"
|
||||
)
|
||||
|
||||
func Test_Duration(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 := Duration(testCase.duration)
|
||||
assert.Equal(t, testCase.s, s)
|
||||
})
|
||||
}
|
||||
}
|
||||
@@ -17,7 +17,7 @@ import (
|
||||
|
||||
"github.com/qdm12/gluetun/internal/constants"
|
||||
"github.com/qdm12/gluetun/internal/firewall"
|
||||
"github.com/qdm12/gluetun/internal/format"
|
||||
"github.com/qdm12/golibs/format"
|
||||
"github.com/qdm12/golibs/logging"
|
||||
)
|
||||
|
||||
@@ -61,7 +61,7 @@ func (p *PIA) PortForward(ctx context.Context, client *http.Client,
|
||||
logger.Warn("Forwarded port data expired on " +
|
||||
data.Expiration.Format(time.RFC1123) + ", getting another one")
|
||||
} else {
|
||||
logger.Info("Forwarded port data expires in " + format.Duration(durationToExpiration))
|
||||
logger.Info("Forwarded port data expires in " + format.FriendlyDuration(durationToExpiration))
|
||||
}
|
||||
}
|
||||
|
||||
@@ -77,7 +77,7 @@ func (p *PIA) PortForward(ctx context.Context, client *http.Client,
|
||||
durationToExpiration = data.Expiration.Sub(p.timeNow())
|
||||
}
|
||||
logger.Info("Port forwarded is " + strconv.Itoa(int(data.Port)) +
|
||||
" expiring in " + format.Duration(durationToExpiration))
|
||||
" expiring in " + format.FriendlyDuration(durationToExpiration))
|
||||
|
||||
// First time binding
|
||||
tryUntilSuccessful(ctx, logger, func() error {
|
||||
@@ -139,7 +139,7 @@ func (p *PIA) PortForward(ctx context.Context, client *http.Client,
|
||||
}
|
||||
durationToExpiration := data.Expiration.Sub(p.timeNow())
|
||||
logger.Info("Port forwarded is " + strconv.Itoa(int(data.Port)) +
|
||||
" expiring in " + format.Duration(durationToExpiration))
|
||||
" expiring in " + format.FriendlyDuration(durationToExpiration))
|
||||
if err := fw.RemoveAllowedPort(ctx, oldPort); err != nil {
|
||||
logger.Error(err.Error())
|
||||
}
|
||||
|
||||
@@ -9,8 +9,8 @@ import (
|
||||
"net/http"
|
||||
"time"
|
||||
|
||||
"github.com/qdm12/gluetun/internal/format"
|
||||
"github.com/qdm12/gluetun/internal/models"
|
||||
"github.com/qdm12/golibs/format"
|
||||
)
|
||||
|
||||
// GetMessage returns a message for the user describing if there is a newer version
|
||||
@@ -38,7 +38,7 @@ func GetMessage(ctx context.Context, buildInfo models.BuildInformation,
|
||||
if tagName == buildInfo.Version {
|
||||
return fmt.Sprintf("You are running the latest release %s", buildInfo.Version), nil
|
||||
}
|
||||
timeSinceRelease := format.Duration(time.Since(releaseTime))
|
||||
timeSinceRelease := format.FriendlyDuration(time.Since(releaseTime))
|
||||
return fmt.Sprintf("There is a new release %s (%s) created %s ago",
|
||||
tagName, name, timeSinceRelease),
|
||||
nil
|
||||
|
||||
Reference in New Issue
Block a user