-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathenvmapper.go
More file actions
28 lines (24 loc) · 750 Bytes
/
envmapper.go
File metadata and controls
28 lines (24 loc) · 750 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
package structparse
import (
"fmt"
"os"
"strings"
)
type EnvironmentMapper struct {
delimiter string
prefix string
}
// NewEnvironmentMapper creates an environment mapping parser
// This parses a string looking for a delimiter indicating that the value should be loaded from the environment
func NewEnvironmentMapper(delimiter, prefix string) *EnvironmentMapper {
return &EnvironmentMapper{delimiter, prefix}
}
func (em *EnvironmentMapper) ParseString(line string) interface{} {
if !strings.HasPrefix(line, em.delimiter) {
return line
}
key := fmt.Sprintf("%s%s", em.prefix, strings.Replace(line, em.delimiter, "", -1))
value := os.Getenv(key)
//log.Printf("Parsing: '%s' Key: '%s' Value: '%s'", line, key, value)
return value
}