add ocis.yaml
This commit is contained in:
@@ -1,6 +1,10 @@
|
||||
package config
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"reflect"
|
||||
|
||||
gofig "github.com/gookit/config/v2"
|
||||
accounts "github.com/owncloud/ocis/accounts/pkg/config"
|
||||
glauth "github.com/owncloud/ocis/glauth/pkg/config"
|
||||
graphExplorer "github.com/owncloud/ocis/graph-explorer/pkg/config"
|
||||
@@ -124,3 +128,78 @@ func New() *Config {
|
||||
WebDAV: webdav.New(),
|
||||
}
|
||||
}
|
||||
|
||||
// TODO(refs) refactoir refactor this outside
|
||||
type mapping struct {
|
||||
EnvVars []string // name of the EnvVars var.
|
||||
Destination interface{} // memory address of the original config value to modify.
|
||||
}
|
||||
|
||||
// GetEnv fetches a list of known env variables for this extension. It is to be used by gookit, as it provides a list
|
||||
// with all the environment variables an extension supports.
|
||||
func GetEnv() []string {
|
||||
var r = make([]string, len(structMappings(&Config{})))
|
||||
for i := range structMappings(&Config{}) {
|
||||
r = append(r, structMappings(&Config{})[i].EnvVars...)
|
||||
}
|
||||
|
||||
return r
|
||||
}
|
||||
|
||||
// UnmapEnv loads values from the gooconf.Config argument and sets them in the expected destination.
|
||||
// TODO(refs) can we avoid repetition here?
|
||||
func (c *Config) UnmapEnv(gooconf *gofig.Config) error {
|
||||
vals := structMappings(c)
|
||||
for i := range vals {
|
||||
for j := range vals[i].EnvVars {
|
||||
// we need to guard against v != "" because this is the condition that checks that the value is set from the environment.
|
||||
// the `ok` guard is not enough, apparently.
|
||||
if v, ok := gooconf.GetValue(vals[i].EnvVars[j]); ok && v != "" {
|
||||
|
||||
// get the destination type from destination
|
||||
switch reflect.ValueOf(vals[i].Destination).Type().String() {
|
||||
case "*bool":
|
||||
r := gooconf.Bool(vals[i].EnvVars[j])
|
||||
*vals[i].Destination.(*bool) = r
|
||||
case "*string":
|
||||
r := gooconf.String(vals[i].EnvVars[j])
|
||||
*vals[i].Destination.(*string) = r
|
||||
case "*int":
|
||||
r := gooconf.Int(vals[i].EnvVars[j])
|
||||
*vals[i].Destination.(*int) = r
|
||||
case "*float64":
|
||||
// defaults to float64
|
||||
r := gooconf.Float(vals[i].EnvVars[j])
|
||||
*vals[i].Destination.(*float64) = r
|
||||
default:
|
||||
// it is unlikely we will ever get here. Let this serve more as a runtime check for when debugging.
|
||||
return fmt.Errorf("invalid type for env var: `%v`", vals[i].EnvVars[j])
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
// structMappings binds a set of environment variables to a destination on cfg.
|
||||
func structMappings(cfg *Config) []mapping {
|
||||
return []mapping{
|
||||
{
|
||||
EnvVars: []string{"OCIS_LOG_FILE"},
|
||||
Destination: &cfg.Log.Level,
|
||||
},
|
||||
{
|
||||
EnvVars: []string{"OCIS_LOG_LEVEL"},
|
||||
Destination: &cfg.Log.Level,
|
||||
},
|
||||
{
|
||||
EnvVars: []string{"OCIS_LOG_COLOR"},
|
||||
Destination: &cfg.Log.Color,
|
||||
},
|
||||
{
|
||||
EnvVars: []string{"OCIS_LOG_PRETTY"},
|
||||
Destination: &cfg.Log.Pretty,
|
||||
},
|
||||
}
|
||||
}
|
||||
|
||||
@@ -67,7 +67,7 @@ func sanitizeExtensions(set []string, ext []string, f func(a, b string) bool) []
|
||||
// is to solely modify `dst`, not dealing with the config structs; and do so in a thread safe manner.
|
||||
func BindSourcesToStructs(extension string, dst interface{}) (*gofig.Config, error) {
|
||||
sources := DefaultConfigSources(extension, supportedExtensions)
|
||||
cnf := gofig.NewWithOptions("proxy", gofig.ParseEnv)
|
||||
cnf := gofig.NewWithOptions(extension, gofig.ParseEnv)
|
||||
cnf.AddDriver(gooyaml.Driver)
|
||||
_ = cnf.LoadFiles(sources...)
|
||||
|
||||
|
||||
Reference in New Issue
Block a user