Maint: move duration formatting to qdm12/golibs
This commit is contained in:
2
go.mod
2
go.mod
@@ -6,7 +6,7 @@ require (
|
|||||||
github.com/fatih/color v1.12.0
|
github.com/fatih/color v1.12.0
|
||||||
github.com/golang/mock v1.6.0
|
github.com/golang/mock v1.6.0
|
||||||
github.com/qdm12/dns v1.10.0
|
github.com/qdm12/dns v1.10.0
|
||||||
github.com/qdm12/golibs v0.0.0-20210721223530-ec1d3fe6dc99
|
github.com/qdm12/golibs v0.0.0-20210723191510-d1078ef6fa9d
|
||||||
github.com/qdm12/goshutdown v0.1.0
|
github.com/qdm12/goshutdown v0.1.0
|
||||||
github.com/qdm12/gosplash v0.1.0
|
github.com/qdm12/gosplash v0.1.0
|
||||||
github.com/qdm12/ss-server v0.2.0
|
github.com/qdm12/ss-server v0.2.0
|
||||||
|
|||||||
4
go.sum
4
go.sum
@@ -67,8 +67,8 @@ github.com/qdm12/dns v1.10.0 h1:WX5QQ5+2h34xfhfxJTmvyURbs9XE4qNrEGtyNeq38Bw=
|
|||||||
github.com/qdm12/dns v1.10.0/go.mod h1:fqZoDf3VzddnKBMNI/OzZUp5H4dO0VBw1fp4qPkolOg=
|
github.com/qdm12/dns v1.10.0/go.mod h1:fqZoDf3VzddnKBMNI/OzZUp5H4dO0VBw1fp4qPkolOg=
|
||||||
github.com/qdm12/golibs v0.0.0-20210603202746-e5494e9c2ebb/go.mod h1:15RBzkun0i8XB7ADIoLJWp9ITRgsz3LroEI2FiOXLRg=
|
github.com/qdm12/golibs v0.0.0-20210603202746-e5494e9c2ebb/go.mod h1:15RBzkun0i8XB7ADIoLJWp9ITRgsz3LroEI2FiOXLRg=
|
||||||
github.com/qdm12/golibs v0.0.0-20210716185557-66793f4ddd80/go.mod h1:15RBzkun0i8XB7ADIoLJWp9ITRgsz3LroEI2FiOXLRg=
|
github.com/qdm12/golibs v0.0.0-20210716185557-66793f4ddd80/go.mod h1:15RBzkun0i8XB7ADIoLJWp9ITRgsz3LroEI2FiOXLRg=
|
||||||
github.com/qdm12/golibs v0.0.0-20210721223530-ec1d3fe6dc99 h1:2OKHAR0SK8BtTtWCRNoSn58eh+iVDA3Cwq4i2CnD3i4=
|
github.com/qdm12/golibs v0.0.0-20210723191510-d1078ef6fa9d h1:bBLNwxvlU4LXrSkDXW+S6czLLUSN42ERA/KdX19qIbU=
|
||||||
github.com/qdm12/golibs v0.0.0-20210721223530-ec1d3fe6dc99/go.mod h1:6aRbg4Z/bTbm9JfxsGXfWKHi7zsOvPfUTK1S5HuAFKg=
|
github.com/qdm12/golibs v0.0.0-20210723191510-d1078ef6fa9d/go.mod h1:6aRbg4Z/bTbm9JfxsGXfWKHi7zsOvPfUTK1S5HuAFKg=
|
||||||
github.com/qdm12/goshutdown v0.1.0 h1:lmwnygdXtnr2pa6VqfR/bm8077/BnBef1+7CP96B7Sw=
|
github.com/qdm12/goshutdown v0.1.0 h1:lmwnygdXtnr2pa6VqfR/bm8077/BnBef1+7CP96B7Sw=
|
||||||
github.com/qdm12/goshutdown v0.1.0/go.mod h1:/LP3MWLqI+wGH/ijfaUG+RHzBbKXIiVKnrg5vXOCf6Q=
|
github.com/qdm12/goshutdown v0.1.0/go.mod h1:/LP3MWLqI+wGH/ijfaUG+RHzBbKXIiVKnrg5vXOCf6Q=
|
||||||
github.com/qdm12/gosplash v0.1.0 h1:Sfl+zIjFZFP7b0iqf2l5UkmEY97XBnaKkH3FNY6Gf7g=
|
github.com/qdm12/gosplash v0.1.0 h1:Sfl+zIjFZFP7b0iqf2l5UkmEY97XBnaKkH3FNY6Gf7g=
|
||||||
|
|||||||
@@ -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/constants"
|
||||||
"github.com/qdm12/gluetun/internal/firewall"
|
"github.com/qdm12/gluetun/internal/firewall"
|
||||||
"github.com/qdm12/gluetun/internal/format"
|
"github.com/qdm12/golibs/format"
|
||||||
"github.com/qdm12/golibs/logging"
|
"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 " +
|
logger.Warn("Forwarded port data expired on " +
|
||||||
data.Expiration.Format(time.RFC1123) + ", getting another one")
|
data.Expiration.Format(time.RFC1123) + ", getting another one")
|
||||||
} else {
|
} 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())
|
durationToExpiration = data.Expiration.Sub(p.timeNow())
|
||||||
}
|
}
|
||||||
logger.Info("Port forwarded is " + strconv.Itoa(int(data.Port)) +
|
logger.Info("Port forwarded is " + strconv.Itoa(int(data.Port)) +
|
||||||
" expiring in " + format.Duration(durationToExpiration))
|
" expiring in " + format.FriendlyDuration(durationToExpiration))
|
||||||
|
|
||||||
// First time binding
|
// First time binding
|
||||||
tryUntilSuccessful(ctx, logger, func() error {
|
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())
|
durationToExpiration := data.Expiration.Sub(p.timeNow())
|
||||||
logger.Info("Port forwarded is " + strconv.Itoa(int(data.Port)) +
|
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 {
|
if err := fw.RemoveAllowedPort(ctx, oldPort); err != nil {
|
||||||
logger.Error(err.Error())
|
logger.Error(err.Error())
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -9,8 +9,8 @@ import (
|
|||||||
"net/http"
|
"net/http"
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
"github.com/qdm12/gluetun/internal/format"
|
|
||||||
"github.com/qdm12/gluetun/internal/models"
|
"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
|
// 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 {
|
if tagName == buildInfo.Version {
|
||||||
return fmt.Sprintf("You are running the latest release %s", buildInfo.Version), nil
|
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",
|
return fmt.Sprintf("There is a new release %s (%s) created %s ago",
|
||||||
tagName, name, timeSinceRelease),
|
tagName, name, timeSinceRelease),
|
||||||
nil
|
nil
|
||||||
|
|||||||
Reference in New Issue
Block a user