forked from sensu/sensu-go
-
Notifications
You must be signed in to change notification settings - Fork 0
/
env_vars.go
41 lines (37 loc) · 931 Bytes
/
env_vars.go
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
package v2
import (
"errors"
"strings"
)
func validateVar(v string) error {
parts := strings.Split(v, "=")
if len(parts) != 2 {
return errors.New("environment variables must be of the form FOO=BAR")
}
if len(parts[0]) == 0 || len(parts[1]) == 0 {
return errors.New("environment variables must be of the form FOO=BAR")
}
return nil
}
// ValidateEnvVars ensures that all the environment variables are well-formed.
// Vars should be of the form FOO=BAR.
func ValidateEnvVars(vars []string) error {
for _, v := range vars {
if err := validateVar(v); err != nil {
return err
}
}
return nil
}
// EnvVarsToMap converts a list of FOO=BAR key-value pairs into a map.
func EnvVarsToMap(vars []string) map[string]string {
result := make(map[string]string, len(vars))
for _, v := range vars {
parts := strings.Split(v, "=")
if len(parts) == 1 {
continue
}
result[parts[0]] = parts[1]
}
return result
}