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 b2294d1cf1
commit 20bc2a2c44
No known key found for this signature in database
GPG key ID: A72BEC6CD3D0DED1
3 changed files with 8 additions and 7 deletions

View file

@ -22,7 +22,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")
snapshotAuthKey = flag.String("snapshotAuthKey", "", "authKey, which must be passed in query string to /snapshot* pages")
forceMergeAuthKey = flag.String("forceMergeAuthKey", "", "authKey, which must be passed in query string to /internal/force_merge pages")
forceFlushAuthKey = flag.String("forceFlushAuthKey", "", "authKey, which must be passed in query string to /internal/force_flush pages")

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)
}