-
Notifications
You must be signed in to change notification settings - Fork 3
/
envfile.go
40 lines (35 loc) · 1 KB
/
envfile.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
package main
import (
"bufio"
"os"
"strings"
)
func EnvFileProvider(filter string) (map[string]string, error) {
// TODO(zomglings): For now, we expect the filter to contain the name of the env file. However,
// we will later want to add other things, like prefixes that we should strip off of lines (e.g. "export" or "set")
// and prefixes that we should ignore (e.g. "#" or "//")
vars := map[string]string{}
filename := filter
ifp, fileErr := os.Open(filename)
if fileErr != nil {
return vars, fileErr
}
defer ifp.Close()
scanner := bufio.NewScanner(ifp)
for scanner.Scan() {
line := scanner.Text()
components := strings.Split(line, "=")
name := components[0]
var value string
if len(components) > 1 {
value = strings.Join(components[1:], "=")
}
vars[name] = value
}
scanErr := scanner.Err()
return vars, scanErr
}
func init() {
helpString := "Provides the environment variables defined in the env file with the given path."
RegisterPlugin("file", helpString, noop, EnvFileProvider)
}