forked from TrueCloudLab/frostfs-node
[#493] cmd/node: Support duration type casting in config
Signed-off-by: Alex Vanin <alexey@nspcc.ru>
This commit is contained in:
parent
25a13d3611
commit
922d29ff88
4 changed files with 46 additions and 0 deletions
|
@ -1,6 +1,8 @@
|
|||
package config
|
||||
|
||||
import (
|
||||
"time"
|
||||
|
||||
"github.com/spf13/cast"
|
||||
)
|
||||
|
||||
|
@ -47,3 +49,22 @@ func String(c *Config, name string) string {
|
|||
func StringSafe(c *Config, name string) string {
|
||||
return cast.ToString(c.Value(name))
|
||||
}
|
||||
|
||||
// Duration reads configuration value
|
||||
// from c by name and casts it to time.Duration.
|
||||
//
|
||||
// Panics if value can not be casted.
|
||||
func Duration(c *Config, name string) time.Duration {
|
||||
x, err := cast.ToDurationE(c.Value(name))
|
||||
panicOnErr(err)
|
||||
|
||||
return x
|
||||
}
|
||||
|
||||
// DurationSafe reads configuration value
|
||||
// from c by name and casts it to time.Duration.
|
||||
//
|
||||
// Returns 0 if value can not be casted.
|
||||
func DurationSafe(c *Config, name string) time.Duration {
|
||||
return cast.ToDuration(c.Value(name))
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue