mirror of
https://github.com/opencloud-eu/opencloud.git
synced 2026-05-08 04:20:59 -05:00
infere go type from destination
This commit is contained in:
@@ -2,12 +2,12 @@ package config
|
|||||||
|
|
||||||
import (
|
import (
|
||||||
"fmt"
|
"fmt"
|
||||||
|
"reflect"
|
||||||
|
|
||||||
gofig "github.com/gookit/config/v2"
|
gofig "github.com/gookit/config/v2"
|
||||||
)
|
)
|
||||||
|
|
||||||
type mapping struct {
|
type mapping struct {
|
||||||
goType string // expected type, used for decoding. It is the field dynamic type.
|
|
||||||
env []string // name of the env var.
|
env []string // name of the env var.
|
||||||
destination interface{} // memory address of the original config value to modify.
|
destination interface{} // memory address of the original config value to modify.
|
||||||
}
|
}
|
||||||
@@ -31,17 +31,19 @@ func (c *Config) UnmapEnv(gooconf *gofig.Config) error {
|
|||||||
// we need to guard against v != "" because this is the condition that checks that the value is set from the environment.
|
// 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.
|
// the `ok` guard is not enough, apparently.
|
||||||
if v, ok := gooconf.GetValue(vals[i].env[j]); ok && v != "" {
|
if v, ok := gooconf.GetValue(vals[i].env[j]); ok && v != "" {
|
||||||
switch vals[i].goType {
|
|
||||||
case "bool":
|
// get the destination type from destination
|
||||||
|
switch reflect.ValueOf(vals[i].destination).Type().String() {
|
||||||
|
case "*bool":
|
||||||
r := gooconf.Bool(vals[i].env[j])
|
r := gooconf.Bool(vals[i].env[j])
|
||||||
*vals[i].destination.(*bool) = r
|
*vals[i].destination.(*bool) = r
|
||||||
case "string":
|
case "*string":
|
||||||
r := gooconf.String(vals[i].env[j])
|
r := gooconf.String(vals[i].env[j])
|
||||||
*vals[i].destination.(*string) = r
|
*vals[i].destination.(*string) = r
|
||||||
case "int":
|
case "*int":
|
||||||
r := gooconf.Int(vals[i].env[j])
|
r := gooconf.Int(vals[i].env[j])
|
||||||
*vals[i].destination.(*int) = r
|
*vals[i].destination.(*int) = r
|
||||||
case "float":
|
case "*float64":
|
||||||
// defaults to float64
|
// defaults to float64
|
||||||
r := gooconf.Float(vals[i].env[j])
|
r := gooconf.Float(vals[i].env[j])
|
||||||
*vals[i].destination.(*float64) = r
|
*vals[i].destination.(*float64) = r
|
||||||
|
|||||||
@@ -3,25 +3,199 @@ package config
|
|||||||
// structMappings binds a set of environment variables to a destination on cfg.
|
// structMappings binds a set of environment variables to a destination on cfg.
|
||||||
func structMappings(cfg *Config) []mapping {
|
func structMappings(cfg *Config) []mapping {
|
||||||
return []mapping{
|
return []mapping{
|
||||||
|
// Logging
|
||||||
{
|
{
|
||||||
goType: "bool",
|
|
||||||
env: []string{"PROXY_ENABLE_BASIC_AUTH"},
|
|
||||||
destination: &cfg.EnableBasicAuth,
|
|
||||||
},
|
|
||||||
{
|
|
||||||
goType: "string",
|
|
||||||
env: []string{"PROXY_LOG_LEVEL", "OCIS_LOG_LEVEL"},
|
env: []string{"PROXY_LOG_LEVEL", "OCIS_LOG_LEVEL"},
|
||||||
destination: &cfg.Log.Level,
|
destination: &cfg.Log.Level,
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
goType: "bool",
|
|
||||||
env: []string{"PROXY_LOG_COLOR", "OCIS_LOG_COLOR"},
|
env: []string{"PROXY_LOG_COLOR", "OCIS_LOG_COLOR"},
|
||||||
destination: &cfg.Log.Color,
|
destination: &cfg.Log.Color,
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
goType: "bool",
|
|
||||||
env: []string{"PROXY_LOG_PRETTY", "OCIS_LOG_PRETTY"},
|
env: []string{"PROXY_LOG_PRETTY", "OCIS_LOG_PRETTY"},
|
||||||
destination: &cfg.Log.Pretty,
|
destination: &cfg.Log.Pretty,
|
||||||
},
|
},
|
||||||
|
{
|
||||||
|
env: []string{"PROXY_LOG_FILE", "OCIS_LOG_FILE"},
|
||||||
|
destination: &cfg.Log.File,
|
||||||
|
},
|
||||||
|
|
||||||
|
// Basic auth
|
||||||
|
{
|
||||||
|
env: []string{"PROXY_ENABLE_BASIC_AUTH"},
|
||||||
|
destination: &cfg.EnableBasicAuth,
|
||||||
|
},
|
||||||
|
|
||||||
|
// Debug (health)
|
||||||
|
{
|
||||||
|
env: []string{"PROXY_DEBUG_ADDR"},
|
||||||
|
destination: &cfg.Debug.Addr,
|
||||||
|
},
|
||||||
|
|
||||||
|
{
|
||||||
|
env: []string{"PROXY_CONFIG_FILE"},
|
||||||
|
destination: &cfg.File,
|
||||||
|
},
|
||||||
|
|
||||||
|
// Tracing
|
||||||
|
{
|
||||||
|
env: []string{"PROXY_TRACING_ENABLED", "OCIS_TRACING_ENABLED"},
|
||||||
|
destination: &cfg.Tracing.Enabled,
|
||||||
|
},
|
||||||
|
{
|
||||||
|
env: []string{"PROXY_TRACING_TYPE", "OCIS_TRACING_TYPE"},
|
||||||
|
destination: &cfg.Tracing.Type,
|
||||||
|
},
|
||||||
|
{
|
||||||
|
env: []string{"PROXY_TRACING_ENDPOINT", "OCIS_TRACING_ENDPOINT"},
|
||||||
|
destination: &cfg.Tracing.Endpoint,
|
||||||
|
},
|
||||||
|
{
|
||||||
|
env: []string{"PROXY_TRACING_COLLECTOR", "OCIS_TRACING_COLLECTOR"},
|
||||||
|
destination: &cfg.Tracing.Collector,
|
||||||
|
},
|
||||||
|
{
|
||||||
|
env: []string{"PROXY_TRACING_SERVICE"},
|
||||||
|
destination: &cfg.Tracing.Service,
|
||||||
|
},
|
||||||
|
|
||||||
|
// Debug
|
||||||
|
{
|
||||||
|
env: []string{"PROXY_DEBUG_ADDR"},
|
||||||
|
destination: &cfg.Debug.Addr,
|
||||||
|
},
|
||||||
|
{
|
||||||
|
env: []string{"PROXY_DEBUG_TOKEN"},
|
||||||
|
destination: &cfg.Debug.Token,
|
||||||
|
},
|
||||||
|
{
|
||||||
|
env: []string{"PROXY_DEBUG_PPROF"},
|
||||||
|
destination: &cfg.Debug.Pprof,
|
||||||
|
},
|
||||||
|
{
|
||||||
|
env: []string{"PROXY_DEBUG_ZPAGES"},
|
||||||
|
destination: &cfg.Debug.Zpages,
|
||||||
|
},
|
||||||
|
|
||||||
|
// HTTP
|
||||||
|
{
|
||||||
|
env: []string{"PROXY_HTTP_ADDR"},
|
||||||
|
destination: &cfg.HTTP.Addr,
|
||||||
|
},
|
||||||
|
{
|
||||||
|
env: []string{"PROXY_HTTP_ROOT"},
|
||||||
|
destination: &cfg.HTTP.Root,
|
||||||
|
},
|
||||||
|
|
||||||
|
// Service
|
||||||
|
{
|
||||||
|
env: []string{"PROXY_SERVICE_NAMESPACE"},
|
||||||
|
destination: &cfg.Service.Name,
|
||||||
|
},
|
||||||
|
{
|
||||||
|
env: []string{"PROXY_SERVICE_NAME"},
|
||||||
|
destination: &cfg.Service.Namespace,
|
||||||
|
},
|
||||||
|
{
|
||||||
|
env: nil,
|
||||||
|
destination: nil,
|
||||||
|
},
|
||||||
|
{
|
||||||
|
env: nil,
|
||||||
|
destination: nil,
|
||||||
|
},
|
||||||
|
{
|
||||||
|
env: nil,
|
||||||
|
destination: nil,
|
||||||
|
},
|
||||||
|
{
|
||||||
|
env: nil,
|
||||||
|
destination: nil,
|
||||||
|
},
|
||||||
|
{
|
||||||
|
env: nil,
|
||||||
|
destination: nil,
|
||||||
|
},
|
||||||
|
{
|
||||||
|
env: nil,
|
||||||
|
destination: nil,
|
||||||
|
},
|
||||||
|
{
|
||||||
|
env: nil,
|
||||||
|
destination: nil,
|
||||||
|
},
|
||||||
|
{
|
||||||
|
env: nil,
|
||||||
|
destination: nil,
|
||||||
|
},
|
||||||
|
{
|
||||||
|
env: nil,
|
||||||
|
destination: nil,
|
||||||
|
},
|
||||||
|
{
|
||||||
|
env: nil,
|
||||||
|
destination: nil,
|
||||||
|
},
|
||||||
|
{
|
||||||
|
env: nil,
|
||||||
|
destination: nil,
|
||||||
|
},
|
||||||
|
{
|
||||||
|
env: nil,
|
||||||
|
destination: nil,
|
||||||
|
},
|
||||||
|
{
|
||||||
|
env: nil,
|
||||||
|
destination: nil,
|
||||||
|
},
|
||||||
|
{
|
||||||
|
env: nil,
|
||||||
|
destination: nil,
|
||||||
|
},
|
||||||
|
{
|
||||||
|
env: nil,
|
||||||
|
destination: nil,
|
||||||
|
},
|
||||||
|
{
|
||||||
|
env: nil,
|
||||||
|
destination: nil,
|
||||||
|
},
|
||||||
|
{
|
||||||
|
env: nil,
|
||||||
|
destination: nil,
|
||||||
|
},
|
||||||
|
{
|
||||||
|
env: nil,
|
||||||
|
destination: nil,
|
||||||
|
},
|
||||||
|
{
|
||||||
|
env: nil,
|
||||||
|
destination: nil,
|
||||||
|
},
|
||||||
|
{
|
||||||
|
env: nil,
|
||||||
|
destination: nil,
|
||||||
|
},
|
||||||
|
{
|
||||||
|
env: nil,
|
||||||
|
destination: nil,
|
||||||
|
},
|
||||||
|
{
|
||||||
|
env: nil,
|
||||||
|
destination: nil,
|
||||||
|
},
|
||||||
|
{
|
||||||
|
env: nil,
|
||||||
|
destination: nil,
|
||||||
|
},
|
||||||
|
{
|
||||||
|
env: nil,
|
||||||
|
destination: nil,
|
||||||
|
},
|
||||||
|
{
|
||||||
|
env: nil,
|
||||||
|
destination: nil,
|
||||||
|
},
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user