diff options
Diffstat (limited to 'service/env')
-rw-r--r-- | service/env/environment.go (renamed from service/env/provider.go) | 5 | ||||
-rw-r--r-- | service/env/service.go | 23 | ||||
-rw-r--r-- | service/env/service_test.go | 27 |
3 files changed, 48 insertions, 7 deletions
diff --git a/service/env/provider.go b/service/env/environment.go index 2918f18c..8e89d2c8 100644 --- a/service/env/provider.go +++ b/service/env/environment.go @@ -2,7 +2,10 @@ package env // Provider aggregates list of environment variables. This interface can be used in custom implementation to drive // values from external sources. -type Provider interface { +type Environment interface { // GetEnv must return list of env variables. GetEnv() (map[string]string, error) + + // SetEnv sets or creates environment value. + SetEnv(key, value string) } diff --git a/service/env/service.go b/service/env/service.go index 0822d55a..41e70bee 100644 --- a/service/env/service.go +++ b/service/env/service.go @@ -1,7 +1,12 @@ package env -// ID contains default svc name. -const ID = "env" +const ( + // ID contains default service name. + ID = "env" + + // rrKey contains default env key to indicate than php running in RR mode. + rrKey = "rr" +) // Service provides ability to map _ENV values from config file. type Service struct { @@ -12,16 +17,17 @@ type Service struct { // NewService creates new env service instance for given rr version. func NewService(defaults map[string]string) *Service { s := &Service{values: defaults} - if s.values == nil { - s.values = make(map[string]string) - } - return s } // Init must return configure svc and return true if svc hasStatus enabled. Must return error in case of // misconfiguration. Services must not be used without proper configuration pushed first. func (s *Service) Init(cfg *Config) (bool, error) { + if s.values == nil { + s.values = make(map[string]string) + s.values[rrKey] = "yes" + } + for k, v := range cfg.Values { s.values[k] = v } @@ -33,3 +39,8 @@ func (s *Service) Init(cfg *Config) (bool, error) { func (s *Service) GetEnv() (map[string]string, error) { return s.values, nil } + +// SetEnv sets or creates environment value. +func (s *Service) SetEnv(key, value string) { + s.values[key] = value +} diff --git a/service/env/service_test.go b/service/env/service_test.go index 69a2a375..28e0d15b 100644 --- a/service/env/service_test.go +++ b/service/env/service_test.go @@ -10,6 +10,16 @@ func Test_NewService(t *testing.T) { assert.Len(t, s.values, 1) } +func Test_Init(t *testing.T) { + s := &Service{} + s.Init(&Config{}) + assert.Len(t, s.values, 1) + + values, err := s.GetEnv() + assert.NoError(t, err) + assert.Equal(t, "yes", values["rr"]) +} + func Test_Extend(t *testing.T) { s := NewService(map[string]string{"rr": "version"}) @@ -22,3 +32,20 @@ func Test_Extend(t *testing.T) { assert.Equal(t, "version", values["rr"]) assert.Equal(t, "value", values["key"]) } + +func Test_Set(t *testing.T) { + s := NewService(map[string]string{"rr": "version"}) + + s.Init(&Config{Values: map[string]string{"key": "value"}}) + assert.Len(t, s.values, 2) + + s.SetEnv("key", "value-new") + s.SetEnv("other", "new") + + values, err := s.GetEnv() + assert.NoError(t, err) + assert.Len(t, values, 3) + assert.Equal(t, "version", values["rr"]) + assert.Equal(t, "value-new", values["key"]) + assert.Equal(t, "new", values["other"]) +} |