forked from TrueCloudLab/frostfs-node
[#493] cmd/node: Implement a basic configuration component
Create `config` package nearby storage node application. Implement `Config` as a wrapper over `viper.Viper` that provides the minimum functionality required by the application. The constructor allows you to read the config from the file. Methods are provided for reading subsections and values from the config tree. Helper functions are implemented to cast a value to native Go types. Signed-off-by: Leonard Lyubich <leonard@nspcc.ru>
This commit is contained in:
parent
d34de558f0
commit
7e11bf9a55
11 changed files with 260 additions and 0 deletions
30
cmd/neofs-node/config/cast.go
Normal file
30
cmd/neofs-node/config/cast.go
Normal file
|
@ -0,0 +1,30 @@
|
|||
package config
|
||||
|
||||
import (
|
||||
"github.com/spf13/cast"
|
||||
)
|
||||
|
||||
func panicOnErr(err error) {
|
||||
if err != nil {
|
||||
panic(err)
|
||||
}
|
||||
}
|
||||
|
||||
// StringSlice reads configuration value
|
||||
// from c by name and casts it to []string.
|
||||
//
|
||||
// Panics if value can not be casted.
|
||||
func StringSlice(c *Config, name string) []string {
|
||||
x, err := cast.ToStringSliceE(c.Value(name))
|
||||
panicOnErr(err)
|
||||
|
||||
return x
|
||||
}
|
||||
|
||||
// StringSliceSafe reads configuration value
|
||||
// from c by name and casts it to []string.
|
||||
//
|
||||
// Returns nil if value can not be casted.
|
||||
func StringSliceSafe(c *Config, name string) []string {
|
||||
return cast.ToStringSlice(c.Value(name))
|
||||
}
|
Loading…
Add table
Add a link
Reference in a new issue