mirror of
https://github.com/eosswedenorg/thalos
synced 2026-06-19 04:50:02 +02:00
app/config/file.go: define Read() that uses viper to read the config.
This commit is contained in:
parent
dbaa520160
commit
a0b88d1991
4 changed files with 75 additions and 13 deletions
|
|
@ -37,7 +37,7 @@ func TestNew(t *testing.T) {
|
|||
require.Equal(t, expected, New())
|
||||
}
|
||||
|
||||
func TestReadYAML(t *testing.T) {
|
||||
func TestRead(t *testing.T) {
|
||||
expected := Config{
|
||||
Name: "ship-reader-1",
|
||||
Api: "http://127.0.0.1:8080",
|
||||
|
|
@ -69,7 +69,7 @@ func TestReadYAML(t *testing.T) {
|
|||
}
|
||||
|
||||
cfg := Config{}
|
||||
err := cfg.ReadYAML([]byte(`
|
||||
err := cfg.Read([]byte(`
|
||||
name: "ship-reader-1"
|
||||
api: "http://127.0.0.1:8080"
|
||||
message_codec: "mojibake"
|
||||
|
|
@ -99,7 +99,7 @@ redis:
|
|||
require.Equal(t, expected, cfg)
|
||||
}
|
||||
|
||||
func TestReadYAMLShorthandShipUrl(t *testing.T) {
|
||||
func TestReadShorthandShipUrl(t *testing.T) {
|
||||
expected := Config{
|
||||
Name: "ship-reader-1",
|
||||
Api: "http://127.0.0.1:8080",
|
||||
|
|
@ -129,7 +129,7 @@ func TestReadYAMLShorthandShipUrl(t *testing.T) {
|
|||
|
||||
cfg := New()
|
||||
|
||||
err := cfg.ReadYAML([]byte(`
|
||||
err := cfg.Read([]byte(`
|
||||
name: "ship-reader-1"
|
||||
api: "http://127.0.0.1:8080"
|
||||
ship: "127.0.0.1:8089"
|
||||
|
|
|
|||
|
|
@ -1,7 +1,11 @@
|
|||
package config
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"os"
|
||||
|
||||
"github.com/mitchellh/mapstructure"
|
||||
"github.com/spf13/viper"
|
||||
)
|
||||
|
||||
// Read values from file
|
||||
|
|
@ -11,5 +15,23 @@ func (cfg *Config) ReadFile(filename string) error {
|
|||
return err
|
||||
}
|
||||
|
||||
return cfg.ReadYAML(bytes)
|
||||
return cfg.Read(bytes)
|
||||
}
|
||||
|
||||
func (cfg *Config) Read(in []byte) error {
|
||||
v := viper.New()
|
||||
v.SetConfigType("yaml")
|
||||
|
||||
if err := v.ReadConfig(bytes.NewBuffer(in)); err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
decoders := mapstructure.ComposeDecodeHookFunc(
|
||||
mapstructure.TextUnmarshallerHookFunc(),
|
||||
mapstructure.StringToTimeDurationHookFunc(),
|
||||
mapstructure.StringToSliceHookFunc(","),
|
||||
decodeShorthandShipConfig,
|
||||
)
|
||||
|
||||
return v.Unmarshal(cfg, viper.DecodeHook(decoders))
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue