-
Notifications
You must be signed in to change notification settings - Fork 26
/
environment.go
70 lines (58 loc) · 1.95 KB
/
environment.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
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
package packit
import (
"fmt"
"os"
"path/filepath"
)
// Environment provides a key-value store for declaring environment variables.
type Environment map[string]string
// Append adds a key-value pair to the environment as an appended value
// according to the specification:
// https://github.com/buildpacks/spec/blob/main/buildpack.md#append.
func (e Environment) Append(name, value, delim string) {
e[name+".append"] = value
delete(e, name+".delim")
if delim != "" {
e[name+".delim"] = delim
}
}
// Default adds a key-value pair to the environment as a default value
// according to the specification:
// https://github.com/buildpacks/spec/blob/main/buildpack.md#default.
func (e Environment) Default(name, value string) {
e[name+".default"] = value
}
// Override adds a key-value pair to the environment as an overridden value
// according to the specification:
// https://github.com/buildpacks/spec/blob/main/buildpack.md#override.
func (e Environment) Override(name, value string) {
e[name+".override"] = value
}
// Prepend adds a key-value pair to the environment as a prepended value
// according to the specification:
// https://github.com/buildpacks/spec/blob/main/buildpack.md#prepend.
func (e Environment) Prepend(name, value, delim string) {
e[name+".prepend"] = value
delete(e, name+".delim")
if delim != "" {
e[name+".delim"] = delim
}
}
func newEnvironmentFromPath(path string) (Environment, error) {
envFiles, err := filepath.Glob(filepath.Join(path, "*"))
if err != nil {
return Environment{}, fmt.Errorf("failed to match env directory files: %s", err)
}
environment := Environment{}
for _, file := range envFiles {
switch filepath.Ext(file) {
case ".delim", ".prepend", ".append", ".default", ".override":
contents, err := os.ReadFile(file)
if err != nil {
return Environment{}, fmt.Errorf("failed to load environment variable: %s", err)
}
environment[filepath.Base(file)] = string(contents)
}
}
return environment, nil
}