forked from paketo-buildpacks/packit
-
Notifications
You must be signed in to change notification settings - Fork 0
/
formatted_map.go
60 lines (48 loc) · 1.18 KB
/
formatted_map.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
package scribe
import (
"fmt"
"sort"
"strings"
"github.com/paketo-buildpacks/packit"
)
type FormattedMap map[string]interface{}
func (m FormattedMap) String() string {
var (
keys []string
padding int
)
for key := range m {
if len(key) > padding {
padding = len(key)
}
keys = append(keys, key)
}
sort.Strings(keys)
var builder strings.Builder
for _, key := range keys {
value := m[key]
if value == nil {
value = "<empty>"
}
for i := len(key); i < padding; i++ {
key = key + " "
}
builder.WriteString(fmt.Sprintf("%s -> \"%v\"\n", key, value))
}
return strings.TrimSpace(builder.String())
}
func NewFormattedMapFromEnvironment(environment packit.Environment) FormattedMap {
envMap := FormattedMap{}
for key, value := range environment {
parts := strings.SplitN(key, ".", 2)
switch {
case parts[1] == "override" || parts[1] == "default":
envMap[parts[0]] = value
case parts[1] == "prepend":
envMap[parts[0]] = strings.Join([]string{value, "$" + parts[0]}, environment[parts[0]+".delim"])
case parts[1] == "append":
envMap[parts[0]] = strings.Join([]string{"$" + parts[0], value}, environment[parts[0]+".delim"])
}
}
return envMap
}