move confenv into conf
This commit is contained in:
parent
68ab5c05f6
commit
64808863ed
1
Makefile
1
Makefile
|
@ -81,7 +81,6 @@ endif
|
||||||
test-internal:
|
test-internal:
|
||||||
go test -v $(TEST_INTERNAL_OPTS) \
|
go test -v $(TEST_INTERNAL_OPTS) \
|
||||||
./internal/conf \
|
./internal/conf \
|
||||||
./internal/confenv \
|
|
||||||
./internal/confwatcher \
|
./internal/confwatcher \
|
||||||
./internal/externalcmd \
|
./internal/externalcmd \
|
||||||
./internal/hls \
|
./internal/hls \
|
||||||
|
|
|
@ -11,7 +11,6 @@ import (
|
||||||
"golang.org/x/crypto/nacl/secretbox"
|
"golang.org/x/crypto/nacl/secretbox"
|
||||||
"gopkg.in/yaml.v2"
|
"gopkg.in/yaml.v2"
|
||||||
|
|
||||||
"github.com/aler9/rtsp-simple-server/internal/confenv"
|
|
||||||
"github.com/aler9/rtsp-simple-server/internal/logger"
|
"github.com/aler9/rtsp-simple-server/internal/logger"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
@ -146,7 +145,7 @@ func Load(fpath string) (*Conf, bool, error) {
|
||||||
}
|
}
|
||||||
|
|
||||||
// read from environment
|
// read from environment
|
||||||
err = confenv.Load("RTSP", conf)
|
err = loadFromEnvironment("RTSP", conf)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, false, err
|
return nil, false, err
|
||||||
}
|
}
|
||||||
|
|
|
@ -1,4 +1,4 @@
|
||||||
package confenv
|
package conf
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"fmt"
|
"fmt"
|
||||||
|
@ -9,7 +9,7 @@ import (
|
||||||
"time"
|
"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()
|
rt := rv.Type()
|
||||||
|
|
||||||
if rt == reflect.TypeOf(time.Duration(0)) {
|
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)
|
rv.SetMapIndex(reflect.ValueOf(mapKeyLower), nv)
|
||||||
}
|
}
|
||||||
|
|
||||||
err := load(env, prefix+"_"+mapKey, nv.Elem())
|
err := loadEnvInternal(env, prefix+"_"+mapKey, nv.Elem())
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
@ -119,11 +119,11 @@ func load(env map[string]string, prefix string, rv reflect.Value) error {
|
||||||
f := rt.Field(i)
|
f := rt.Field(i)
|
||||||
|
|
||||||
// load only public fields
|
// load only public fields
|
||||||
if f.Tag.Get("yaml") == "-" {
|
if f.Tag.Get("json") == "-" {
|
||||||
continue
|
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 {
|
if err != nil {
|
||||||
return err
|
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)
|
return fmt.Errorf("unsupported type: %v", rt)
|
||||||
}
|
}
|
||||||
|
|
||||||
// Load fills a structure with data from the environment.
|
func loadFromEnvironment(prefix string, v interface{}) error {
|
||||||
func Load(prefix string, v interface{}) error {
|
|
||||||
env := make(map[string]string)
|
env := make(map[string]string)
|
||||||
for _, kv := range os.Environ() {
|
for _, kv := range os.Environ() {
|
||||||
tmp := strings.SplitN(kv, "=", 2)
|
tmp := strings.SplitN(kv, "=", 2)
|
||||||
env[tmp[0]] = tmp[1]
|
env[tmp[0]] = tmp[1]
|
||||||
}
|
}
|
||||||
|
|
||||||
return load(env, prefix, reflect.ValueOf(v).Elem())
|
return loadEnvInternal(env, prefix, reflect.ValueOf(v).Elem())
|
||||||
}
|
}
|
|
@ -1,4 +1,4 @@
|
||||||
package confenv
|
package conf
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"os"
|
"os"
|
||||||
|
@ -32,7 +32,7 @@ type testStruct struct {
|
||||||
MyMap map[string]*mapEntry
|
MyMap map[string]*mapEntry
|
||||||
}
|
}
|
||||||
|
|
||||||
func Test(t *testing.T) {
|
func TestEnvironment(t *testing.T) {
|
||||||
os.Setenv("MYPREFIX_MYSTRING", "testcontent")
|
os.Setenv("MYPREFIX_MYSTRING", "testcontent")
|
||||||
defer os.Unsetenv("MYPREFIX_MYSTRING")
|
defer os.Unsetenv("MYPREFIX_MYSTRING")
|
||||||
|
|
||||||
|
@ -55,7 +55,7 @@ func Test(t *testing.T) {
|
||||||
defer os.Unsetenv("MYPREFIX_MYMAP_MYKEY2_MYVALUE")
|
defer os.Unsetenv("MYPREFIX_MYMAP_MYKEY2_MYVALUE")
|
||||||
|
|
||||||
var s testStruct
|
var s testStruct
|
||||||
err := Load("MYPREFIX", &s)
|
err := loadFromEnvironment("MYPREFIX", &s)
|
||||||
require.NoError(t, err)
|
require.NoError(t, err)
|
||||||
|
|
||||||
require.Equal(t, "testcontent", s.MyString)
|
require.Equal(t, "testcontent", s.MyString)
|
Loading…
Reference in New Issue