lib/flagutil: re-use Duration.Set() call in NewDuration

This commit is contained in:
Aliaksandr Valialkin 2022-05-02 10:12:05 +03:00
parent 25e54d2b50
commit a436836402
No known key found for this signature in database
GPG key ID: A72BEC6CD3D0DED1
3 changed files with 8 additions and 7 deletions

View file

@ -24,7 +24,7 @@ import (
)
var (
retentionPeriod = flagutil.NewDuration("retentionPeriod", 1, "Data with timestamps outside the retentionPeriod is automatically deleted")
retentionPeriod = flagutil.NewDuration("retentionPeriod", "1", "Data with timestamps outside the retentionPeriod is automatically deleted")
httpListenAddr = flag.String("httpListenAddr", ":8482", "Address to listen for http connections")
storageDataPath = flag.String("storageDataPath", "vmstorage-data", "Path to storage data")
vminsertAddr = flag.String("vminsertAddr", ":8400", "TCP address to accept connections from vminsert services")

View file

@ -12,14 +12,14 @@ import (
// NewDuration returns new `duration` flag with the given name, defaultValue and description.
//
// DefaultValue is in months.
func NewDuration(name string, defaultValue float64, description string) *Duration {
func NewDuration(name string, defaultValue string, description string) *Duration {
description += "\nThe following optional suffixes are supported: h (hour), d (day), w (week), y (year). If suffix isn't set, then the duration is counted in months"
d := Duration{
Msecs: int64(defaultValue * msecsPerMonth),
valueString: fmt.Sprintf("%g", defaultValue),
d := &Duration{}
if err := d.Set(defaultValue); err != nil {
panic(fmt.Sprintf("BUG: can not parse default value %s for flag %s", defaultValue, name))
}
flag.Var(&d, name, description)
return &d
flag.Var(d, name, description)
return d
}
// Duration is a flag for holding duration.

View file

@ -56,5 +56,6 @@ func TestDurationSetSuccess(t *testing.T) {
f("1h", 3600*1000)
f("1.5d", 1.5*24*3600*1000)
f("2.3W", 2.3*7*24*3600*1000)
f("1w", 7*24*3600*1000)
f("0.25y", 0.25*365*24*3600*1000)
}