Use config.ServiceConfig across API

This commit is contained in:
Philipp Wolfer 2023-12-07 23:44:58 +01:00
parent 091b3c2f49
commit 9c363cc06d
No known key found for this signature in database
GPG key ID: 8FDF744D4919943B
27 changed files with 137 additions and 99 deletions

View file

@ -18,6 +18,7 @@ package config
import (
"fmt"
"github.com/spf13/cast"
"github.com/spf13/viper"
)
@ -30,11 +31,11 @@ type ServiceConfig struct {
func NewServiceConfig(name string, config *viper.Viper) ServiceConfig {
service := ServiceConfig{
Name: name,
Backend: viper.GetString("backend"),
Backend: config.GetString("backend"),
ConfigValues: make(map[string]any),
}
for key, val := range viper.AllSettings() {
for key, val := range config.AllSettings() {
if key != "backend" {
service.ConfigValues[key] = val
}
@ -43,6 +44,19 @@ func NewServiceConfig(name string, config *viper.Viper) ServiceConfig {
return service
}
func (c *ServiceConfig) GetString(key string) string {
return cast.ToString(c.ConfigValues[key])
}
func (c *ServiceConfig) GetBool(key string) bool {
return cast.ToBool(c.ConfigValues[key])
}
func (c *ServiceConfig) IsSet(key string) bool {
_, ok := c.ConfigValues[key]
return ok
}
func (c *ServiceConfig) Save() error {
key := "service." + c.Name
viper.Set(key+".backend", c.Backend)