-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathenv.go
More file actions
64 lines (49 loc) · 963 Bytes
/
env.go
File metadata and controls
64 lines (49 loc) · 963 Bytes
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
56
57
58
59
60
61
62
63
64
package envparse
import (
"fmt"
"strings"
)
// Env maps a string key to a list of values.
// see net/url/url.go
type Env map[string]string
// Set sets the key to value.
func (e Env) Set(key, value string) {
e[key] = value
}
// Del deletes the values associated with key.
func (e Env) Del(key string) {
delete(e, key)
}
// String returns string representation
// of Env
func (e Env) String() string {
var b strings.Builder
left := len(e)
for k, v := range e {
b.WriteString(k)
b.WriteByte('=')
b.WriteString(fmt.Sprintf("%q", v))
left--
// got more elements to emit
if left != 0 {
b.WriteByte(' ')
}
}
return b.String()
}
// Parse parses raw key=value
// pairs into Env(some sort of url.Values)
func Parse(raw string) (Env, error) {
if raw == "" {
return nil, nil
}
e := make(Env)
cb := func(key, value string) {
e.Set(key, value)
}
err := ParseRaw(raw, cb)
if err != nil {
return nil, err
}
return e, nil
}