move confenv into conf

This commit is contained in:
aler9 2021-09-26 17:07:48 +02:00
parent 68ab5c05f6
commit 64808863ed
4 changed files with 11 additions and 14 deletions

View File

@ -81,7 +81,6 @@ endif
test-internal:
go test -v $(TEST_INTERNAL_OPTS) \
./internal/conf \
./internal/confenv \
./internal/confwatcher \
./internal/externalcmd \
./internal/hls \

View File

@ -11,7 +11,6 @@ import (
"golang.org/x/crypto/nacl/secretbox"
"gopkg.in/yaml.v2"
"github.com/aler9/rtsp-simple-server/internal/confenv"
"github.com/aler9/rtsp-simple-server/internal/logger"
)
@ -146,7 +145,7 @@ func Load(fpath string) (*Conf, bool, error) {
}
// read from environment
err = confenv.Load("RTSP", conf)
err = loadFromEnvironment("RTSP", conf)
if err != nil {
return nil, false, err
}

View File

@ -1,4 +1,4 @@
package confenv
package conf
import (
"fmt"
@ -9,7 +9,7 @@ import (
"time"
)
func load(env map[string]string, prefix string, rv reflect.Value) error {
func loadEnvInternal(env map[string]string, prefix string, rv reflect.Value) error {
rt := rv.Type()
if rt == reflect.TypeOf(time.Duration(0)) {
@ -106,7 +106,7 @@ func load(env map[string]string, prefix string, rv reflect.Value) error {
rv.SetMapIndex(reflect.ValueOf(mapKeyLower), nv)
}
err := load(env, prefix+"_"+mapKey, nv.Elem())
err := loadEnvInternal(env, prefix+"_"+mapKey, nv.Elem())
if err != nil {
return err
}
@ -119,11 +119,11 @@ func load(env map[string]string, prefix string, rv reflect.Value) error {
f := rt.Field(i)
// load only public fields
if f.Tag.Get("yaml") == "-" {
if f.Tag.Get("json") == "-" {
continue
}
err := load(env, prefix+"_"+strings.ToUpper(f.Name), rv.Field(i))
err := loadEnvInternal(env, prefix+"_"+strings.ToUpper(f.Name), rv.Field(i))
if err != nil {
return err
}
@ -134,13 +134,12 @@ func load(env map[string]string, prefix string, rv reflect.Value) error {
return fmt.Errorf("unsupported type: %v", rt)
}
// Load fills a structure with data from the environment.
func Load(prefix string, v interface{}) error {
func loadFromEnvironment(prefix string, v interface{}) error {
env := make(map[string]string)
for _, kv := range os.Environ() {
tmp := strings.SplitN(kv, "=", 2)
env[tmp[0]] = tmp[1]
}
return load(env, prefix, reflect.ValueOf(v).Elem())
return loadEnvInternal(env, prefix, reflect.ValueOf(v).Elem())
}

View File

@ -1,4 +1,4 @@
package confenv
package conf
import (
"os"
@ -32,7 +32,7 @@ type testStruct struct {
MyMap map[string]*mapEntry
}
func Test(t *testing.T) {
func TestEnvironment(t *testing.T) {
os.Setenv("MYPREFIX_MYSTRING", "testcontent")
defer os.Unsetenv("MYPREFIX_MYSTRING")
@ -55,7 +55,7 @@ func Test(t *testing.T) {
defer os.Unsetenv("MYPREFIX_MYMAP_MYKEY2_MYVALUE")
var s testStruct
err := Load("MYPREFIX", &s)
err := loadFromEnvironment("MYPREFIX", &s)
require.NoError(t, err)
require.Equal(t, "testcontent", s.MyString)