-
Notifications
You must be signed in to change notification settings - Fork 0
/
vars.go
54 lines (49 loc) · 1.29 KB
/
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
42
43
44
45
46
47
48
49
50
51
52
53
54
package env
import (
"fmt"
"slices"
"strings"
)
// Vars is a map of environment variables.
type Vars map[string]string
// Names returns the names of all variables in the map as a sorted slice.
func (v Vars) Names() []string {
names := make([]string, 0, len(v))
for k := range v {
names = append(names, k)
}
slices.Sort(names)
return names
}
// Set applies the variables to the environment. If an error occurs while setting
// a variable, the error is returned without any further variables being set.
//
// # returns
//
// error // any error that occurs while setting the variables
func (v Vars) Set() error {
for k, v := range v {
if err := osSetenv(k, v); err != nil {
return fmt.Errorf("failed to set environment variable %s: %w", k, err)
}
}
return nil
}
// String returns a string representation of the variables. The result is a string of
// comma delimited NAME="VALUE" entries, sorted by NAME and enclosed in [-]'s.
//
// # result
//
// string // a string representation of the variables in the form:
//
// [NAME1="VALUE1",NAME2="VALUE2",NAME3="VALUE3"]
//
// If the map is empty the result is "[]".
func (v Vars) String() string {
n := v.Names()
ls := make([]string, 0, len(n))
for _, k := range n {
ls = append(ls, k+`="`+v[k]+`"`)
}
return "[" + strings.Join(ls, ",") + "]"
}