diff options
author | Wolfy-J <[email protected]> | 2019-01-05 12:21:37 +0300 |
---|---|---|
committer | Wolfy-J <[email protected]> | 2019-01-05 12:21:37 +0300 |
commit | 46009112a783a1fdae95e0a061d4c8c41a1c8ff1 (patch) | |
tree | a34cf8ad465e9f354723f855c531ea1db3235cbd /service/env | |
parent | d5e5be00aca87ea39ece9c04efe7dc354b1dc3f6 (diff) |
first set of patches
Diffstat (limited to 'service/env')
-rw-r--r-- | service/env/environment.go | 16 | ||||
-rw-r--r-- | service/env/service.go | 14 | ||||
-rw-r--r-- | service/env/service_test.go | 17 |
3 files changed, 45 insertions, 2 deletions
diff --git a/service/env/environment.go b/service/env/environment.go index 52a5bcf4..fe8f1dd8 100644 --- a/service/env/environment.go +++ b/service/env/environment.go @@ -3,9 +3,21 @@ package env // Environment aggregates list of environment variables. This interface can be used in custom implementation to drive // values from external sources. type Environment interface { - // GetEnv must return list of env variables. - GetEnv() (map[string]string, error) + Setter + Getter + + // Copy all environment values. + Copy(setter Setter) error +} +// Setter provides ability to set environment value. +type Setter interface { // SetEnv sets or creates environment value. SetEnv(key, value string) } + +// Setter provides ability to set environment value. +type Getter interface { + // GetEnv must return list of env variables. + GetEnv() (map[string]string, error) +} diff --git a/service/env/service.go b/service/env/service.go index 4d1327d4..83175b36 100644 --- a/service/env/service.go +++ b/service/env/service.go @@ -39,3 +39,17 @@ func (s *Service) GetEnv() (map[string]string, error) { func (s *Service) SetEnv(key, value string) { s.values[key] = value } + +// Copy all environment values. +func (s *Service) Copy(setter Setter) error { + values, err := s.GetEnv() + if err != nil { + return err + } + + for k, v := range values { + setter.SetEnv(k, v) + } + + return nil +} diff --git a/service/env/service_test.go b/service/env/service_test.go index 61fecd28..c20bb76c 100644 --- a/service/env/service_test.go +++ b/service/env/service_test.go @@ -49,3 +49,20 @@ func Test_Set(t *testing.T) { assert.Equal(t, "value-new", values["key"]) assert.Equal(t, "new", values["other"]) } + +func Test_Copy(t *testing.T) { + s1 := NewService(map[string]string{"RR": "version"}) + s2 := NewService(map[string]string{}) + + s1.SetEnv("key", "value-new") + s1.SetEnv("other", "new") + + assert.NoError(t, s1.Copy(s2)) + + values, err := s2.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"]) +} |