2021-09-26 15:07:48 +00:00
|
|
|
package conf
|
2020-10-13 21:36:27 +00:00
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"os"
|
|
|
|
"reflect"
|
|
|
|
"strconv"
|
|
|
|
"strings"
|
|
|
|
)
|
|
|
|
|
2021-10-11 09:46:40 +00:00
|
|
|
type envUnmarshaler interface {
|
|
|
|
unmarshalEnv(string) error
|
|
|
|
}
|
|
|
|
|
2021-09-26 15:07:48 +00:00
|
|
|
func loadEnvInternal(env map[string]string, prefix string, rv reflect.Value) error {
|
2020-10-13 21:36:27 +00:00
|
|
|
rt := rv.Type()
|
|
|
|
|
2021-10-11 09:46:40 +00:00
|
|
|
if i, ok := rv.Addr().Interface().(envUnmarshaler); ok {
|
2021-05-09 15:51:38 +00:00
|
|
|
if ev, ok := env[prefix]; ok {
|
2021-10-11 09:46:40 +00:00
|
|
|
err := i.unmarshalEnv(ev)
|
2020-10-13 21:36:27 +00:00
|
|
|
if err != nil {
|
2021-05-09 15:51:38 +00:00
|
|
|
return fmt.Errorf("%s: %s", prefix, err)
|
2020-10-13 21:36:27 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2021-09-27 08:36:28 +00:00
|
|
|
switch rt {
|
|
|
|
case reflect.TypeOf(""):
|
2021-05-09 15:51:38 +00:00
|
|
|
if ev, ok := env[prefix]; ok {
|
2020-10-13 21:36:27 +00:00
|
|
|
rv.SetString(ev)
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
|
2021-09-27 08:36:28 +00:00
|
|
|
case reflect.TypeOf(int(0)):
|
2021-05-09 15:51:38 +00:00
|
|
|
if ev, ok := env[prefix]; ok {
|
2020-10-13 21:36:27 +00:00
|
|
|
iv, err := strconv.ParseInt(ev, 10, 64)
|
|
|
|
if err != nil {
|
2021-05-09 15:51:38 +00:00
|
|
|
return fmt.Errorf("%s: %s", prefix, err)
|
2020-10-13 21:36:27 +00:00
|
|
|
}
|
|
|
|
rv.SetInt(iv)
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
|
2021-09-27 08:36:28 +00:00
|
|
|
case reflect.TypeOf(uint64(0)):
|
2021-05-09 15:51:38 +00:00
|
|
|
if ev, ok := env[prefix]; ok {
|
2021-01-10 11:55:53 +00:00
|
|
|
iv, err := strconv.ParseUint(ev, 10, 64)
|
|
|
|
if err != nil {
|
2021-05-09 15:51:38 +00:00
|
|
|
return fmt.Errorf("%s: %s", prefix, err)
|
2021-01-10 11:55:53 +00:00
|
|
|
}
|
|
|
|
rv.SetUint(iv)
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
|
Add additional Raspberry Pi Camera parameters (#1198)
* rpicamera: add rpiCameraHFlip and rpiCameraVFlip parameters
* rpicamera: add rpiCameraBrightness, rpiCameraContrast,
rpiCameraSaturation, rpiCameraSharpness, rpiCameraExposure,
rpiCameraAWB, rpiCameraDenoise, rpiCameraShutter, rpiCameraMetering,
rpiCameraGain, rpiCameraEV, rpiCameraROI, rpiCameraTuningFile
* support float values in config file
2022-10-24 13:08:30 +00:00
|
|
|
case reflect.TypeOf(float64(0)):
|
|
|
|
if ev, ok := env[prefix]; ok {
|
|
|
|
iv, err := strconv.ParseFloat(ev, 64)
|
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("%s: %s", prefix, err)
|
|
|
|
}
|
|
|
|
rv.SetFloat(iv)
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
|
2021-09-27 08:36:28 +00:00
|
|
|
case reflect.TypeOf(bool(false)):
|
2021-05-09 15:51:38 +00:00
|
|
|
if ev, ok := env[prefix]; ok {
|
2020-10-13 21:36:27 +00:00
|
|
|
switch strings.ToLower(ev) {
|
|
|
|
case "yes", "true":
|
|
|
|
rv.SetBool(true)
|
|
|
|
|
|
|
|
case "no", "false":
|
|
|
|
rv.SetBool(false)
|
|
|
|
|
|
|
|
default:
|
2021-05-09 15:51:38 +00:00
|
|
|
return fmt.Errorf("%s: invalid value '%s'", prefix, ev)
|
2020-10-13 21:36:27 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
return nil
|
2021-09-27 08:36:28 +00:00
|
|
|
}
|
2020-10-13 21:36:27 +00:00
|
|
|
|
2021-09-27 08:36:28 +00:00
|
|
|
switch rt.Kind() {
|
2020-10-13 21:36:27 +00:00
|
|
|
case reflect.Map:
|
|
|
|
for k := range env {
|
2021-05-09 15:51:38 +00:00
|
|
|
if !strings.HasPrefix(k, prefix+"_") {
|
2020-10-13 21:36:27 +00:00
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
2021-05-09 15:51:38 +00:00
|
|
|
mapKey := strings.Split(k[len(prefix+"_"):], "_")[0]
|
2020-10-14 07:48:54 +00:00
|
|
|
if len(mapKey) == 0 {
|
|
|
|
continue
|
|
|
|
}
|
2020-10-30 22:08:09 +00:00
|
|
|
|
|
|
|
// allow only keys in uppercase
|
|
|
|
if mapKey != strings.ToUpper(mapKey) {
|
|
|
|
continue
|
|
|
|
}
|
2020-10-13 21:36:27 +00:00
|
|
|
|
2020-10-18 22:22:16 +00:00
|
|
|
// initialize only if there's at least one key
|
|
|
|
if rv.IsNil() {
|
|
|
|
rv.Set(reflect.MakeMap(rt))
|
|
|
|
}
|
|
|
|
|
2020-10-30 22:08:09 +00:00
|
|
|
mapKeyLower := strings.ToLower(mapKey)
|
|
|
|
nv := rv.MapIndex(reflect.ValueOf(mapKeyLower))
|
2020-10-13 21:36:27 +00:00
|
|
|
zero := reflect.Value{}
|
|
|
|
if nv == zero {
|
|
|
|
nv = reflect.New(rt.Elem().Elem())
|
2020-10-30 22:08:09 +00:00
|
|
|
rv.SetMapIndex(reflect.ValueOf(mapKeyLower), nv)
|
2020-10-13 21:36:27 +00:00
|
|
|
}
|
|
|
|
|
2021-09-26 15:07:48 +00:00
|
|
|
err := loadEnvInternal(env, prefix+"_"+mapKey, nv.Elem())
|
2020-10-13 21:36:27 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
|
|
|
|
case reflect.Struct:
|
|
|
|
flen := rt.NumField()
|
|
|
|
for i := 0; i < flen; i++ {
|
2020-10-13 22:50:08 +00:00
|
|
|
f := rt.Field(i)
|
2020-10-13 21:36:27 +00:00
|
|
|
|
2020-10-19 20:17:48 +00:00
|
|
|
// load only public fields
|
2021-09-26 15:07:48 +00:00
|
|
|
if f.Tag.Get("json") == "-" {
|
2020-10-13 21:36:27 +00:00
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
2021-09-26 15:07:48 +00:00
|
|
|
err := loadEnvInternal(env, prefix+"_"+strings.ToUpper(f.Name), rv.Field(i))
|
2020-10-13 21:36:27 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return nil
|
2022-12-15 23:50:47 +00:00
|
|
|
|
|
|
|
case reflect.Slice:
|
|
|
|
if rt.Elem() == reflect.TypeOf("") {
|
|
|
|
if ev, ok := env[prefix]; ok {
|
|
|
|
rv.Set(reflect.ValueOf(strings.Split(ev, ",")))
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
2020-10-13 21:36:27 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return fmt.Errorf("unsupported type: %v", rt)
|
|
|
|
}
|
|
|
|
|
2021-09-26 15:07:48 +00:00
|
|
|
func loadFromEnvironment(prefix string, v interface{}) error {
|
2020-10-13 21:36:27 +00:00
|
|
|
env := make(map[string]string)
|
|
|
|
for _, kv := range os.Environ() {
|
2020-10-30 23:08:50 +00:00
|
|
|
tmp := strings.SplitN(kv, "=", 2)
|
2020-10-13 21:36:27 +00:00
|
|
|
env[tmp[0]] = tmp[1]
|
|
|
|
}
|
|
|
|
|
2021-09-26 15:07:48 +00:00
|
|
|
return loadEnvInternal(env, prefix, reflect.ValueOf(v).Elem())
|
2020-10-13 21:36:27 +00:00
|
|
|
}
|