chore(deps): remove qdm12/golibs dependency
- Implement friendly duration formatting locally
This commit is contained in:
@@ -1,16 +1,9 @@
|
||||
package cli
|
||||
|
||||
import "github.com/qdm12/golibs/logging"
|
||||
|
||||
type noopLogger struct{}
|
||||
|
||||
func newNoopLogger() *noopLogger {
|
||||
return new(noopLogger)
|
||||
}
|
||||
|
||||
func (l *noopLogger) Debug(string) {}
|
||||
func (l *noopLogger) Info(string) {}
|
||||
func (l *noopLogger) Warn(string) {}
|
||||
func (l *noopLogger) Error(string) {}
|
||||
func (l *noopLogger) PatchLevel(logging.Level) {}
|
||||
func (l *noopLogger) PatchPrefix(string) {}
|
||||
func (l *noopLogger) Info(string) {}
|
||||
|
||||
34
internal/format/duration.go
Normal file
34
internal/format/duration.go
Normal file
@@ -0,0 +1,34 @@
|
||||
package format
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"time"
|
||||
)
|
||||
|
||||
// FriendlyDuration formats a duration in an approximate, human friendly duration.
|
||||
// For example 55 hours will result in "2 days".
|
||||
func FriendlyDuration(duration time.Duration) string {
|
||||
const twoDays = 48 * time.Hour
|
||||
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 < twoDays:
|
||||
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)
|
||||
}
|
||||
}
|
||||
66
internal/format/duration_test.go
Normal file
66
internal/format/duration_test.go
Normal file
@@ -0,0 +1,66 @@
|
||||
package format
|
||||
|
||||
import (
|
||||
"testing"
|
||||
"time"
|
||||
|
||||
"github.com/stretchr/testify/assert"
|
||||
)
|
||||
|
||||
func Test_FriendlyDuration(t *testing.T) {
|
||||
t.Parallel()
|
||||
testCases := map[string]struct {
|
||||
duration time.Duration
|
||||
friendly string
|
||||
}{
|
||||
"zero": {
|
||||
friendly: "0 second",
|
||||
},
|
||||
"one_second": {
|
||||
duration: time.Second,
|
||||
friendly: "1 second",
|
||||
},
|
||||
"59_seconds": {
|
||||
duration: 59 * time.Second,
|
||||
friendly: "59 seconds",
|
||||
},
|
||||
"1_minute": {
|
||||
duration: time.Minute,
|
||||
friendly: "1 minute",
|
||||
},
|
||||
"2_minutes": {
|
||||
duration: 2 * time.Minute,
|
||||
friendly: "2 minutes",
|
||||
},
|
||||
"1_hour": {
|
||||
duration: time.Hour,
|
||||
friendly: "60 minutes",
|
||||
},
|
||||
"2_hours": {
|
||||
duration: 2 * time.Hour,
|
||||
friendly: "2 hours",
|
||||
},
|
||||
"26_hours": {
|
||||
duration: 26 * time.Hour,
|
||||
friendly: "26 hours",
|
||||
},
|
||||
"28_hours": {
|
||||
duration: 28 * time.Hour,
|
||||
friendly: "28 hours",
|
||||
},
|
||||
"55_hours": {
|
||||
duration: 55 * time.Hour,
|
||||
friendly: "2 days",
|
||||
},
|
||||
}
|
||||
for name, testCase := range testCases {
|
||||
testCase := testCase
|
||||
t.Run(name, func(t *testing.T) {
|
||||
t.Parallel()
|
||||
|
||||
s := FriendlyDuration(testCase.duration)
|
||||
|
||||
assert.Equal(t, testCase.friendly, s)
|
||||
})
|
||||
}
|
||||
}
|
||||
@@ -16,8 +16,8 @@ import (
|
||||
"strings"
|
||||
"time"
|
||||
|
||||
"github.com/qdm12/gluetun/internal/format"
|
||||
"github.com/qdm12/gluetun/internal/provider/utils"
|
||||
"github.com/qdm12/golibs/format"
|
||||
)
|
||||
|
||||
var (
|
||||
|
||||
@@ -5,8 +5,8 @@ import (
|
||||
"time"
|
||||
|
||||
"github.com/qdm12/gluetun/internal/constants/providers"
|
||||
"github.com/qdm12/gluetun/internal/format"
|
||||
"github.com/qdm12/gluetun/internal/models"
|
||||
"github.com/qdm12/golibs/format"
|
||||
)
|
||||
|
||||
func (s *Storage) mergeServers(hardcoded, persisted models.AllServers) models.AllServers {
|
||||
|
||||
@@ -8,8 +8,8 @@ import (
|
||||
"sort"
|
||||
"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
|
||||
|
||||
Reference in New Issue
Block a user