From 64808863edd54c87e3af8206cf164f408db4b0cc Mon Sep 17 00:00:00 2001 From: aler9 <46489434+aler9@users.noreply.github.com> Date: Sun, 26 Sep 2021 17:07:48 +0200 Subject: [PATCH] move confenv into conf --- Makefile | 1 - internal/conf/conf.go | 3 +-- internal/{confenv/confenv.go => conf/env.go} | 15 +++++++-------- .../{confenv/confenv_test.go => conf/env_test.go} | 6 +++--- 4 files changed, 11 insertions(+), 14 deletions(-) rename internal/{confenv/confenv.go => conf/env.go} (85%) rename internal/{confenv/confenv_test.go => conf/env_test.go} (93%) diff --git a/Makefile b/Makefile index ad4827f5..fa06f903 100644 --- a/Makefile +++ b/Makefile @@ -81,7 +81,6 @@ endif test-internal: go test -v $(TEST_INTERNAL_OPTS) \ ./internal/conf \ - ./internal/confenv \ ./internal/confwatcher \ ./internal/externalcmd \ ./internal/hls \ diff --git a/internal/conf/conf.go b/internal/conf/conf.go index fb9e5fee..657c9b23 100644 --- a/internal/conf/conf.go +++ b/internal/conf/conf.go @@ -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 } diff --git a/internal/confenv/confenv.go b/internal/conf/env.go similarity index 85% rename from internal/confenv/confenv.go rename to internal/conf/env.go index dc1e2ae8..097a5a60 100644 --- a/internal/confenv/confenv.go +++ b/internal/conf/env.go @@ -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()) } diff --git a/internal/confenv/confenv_test.go b/internal/conf/env_test.go similarity index 93% rename from internal/confenv/confenv_test.go rename to internal/conf/env_test.go index 1dcc7d65..2f23921b 100644 --- a/internal/confenv/confenv_test.go +++ b/internal/conf/env_test.go @@ -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)