summaryrefslogtreecommitdiff
path: root/service/env/service.go
blob: 00bc41ef7839da9fa92a1cdfe3800c6680dca6db (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
package env

// ID contains default service name.
const ID = "env"

// Services provides ability to map _ENV values from config file.
type Service struct {
	// values is default set of values.
	values map[string]string
}

// NewService creates new env service instance for given rr version.
func NewService(defaults map[string]string) *Service {
	s := &Service{values: defaults}
	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["RR"] = "true"
	}

	for k, v := range cfg.Values {
		s.values[k] = v
	}

	return true, nil
}

// GetEnv must return list of env variables.
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
}

// 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
}