forked from paketo-buildpacks/packit
-
Notifications
You must be signed in to change notification settings - Fork 0
/
formatted_map.go
76 lines (64 loc) · 1.74 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
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
package scribe
import (
"fmt"
"sort"
"strings"
)
// A FormattedMap is a wrapper for map[string]interface{} to extend functionality.
type FormattedMap map[string]interface{}
// Sorts all of the keys in the FormattedMap alphabetically and then constructs
// a padded table.
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())
}
// NewFormattedMapFromEnvironment take an environment and returns a
// FormattedMap with the appropriate environment variable information added.
func NewFormattedMapFromEnvironment(environment map[string]string) FormattedMap {
envMap := FormattedMap{}
for key, value := range environment {
parts := strings.SplitN(key, ".", 2)
if len(parts) < 2 {
envMap[key] = value
continue
}
switch {
case parts[1] == "override" || parts[1] == "default":
envMap[parts[0]] = value
case parts[1] == "prepend":
existingValue, ok := envMap[parts[0]]
if !ok {
existingValue = "$" + parts[0]
}
envMap[parts[0]] = strings.Join([]string{value, fmt.Sprintf("%v", existingValue)}, environment[parts[0]+".delim"])
case parts[1] == "append":
existingValue, ok := envMap[parts[0]]
if !ok {
existingValue = "$" + parts[0]
}
envMap[parts[0]] = strings.Join([]string{fmt.Sprintf("%v", existingValue), value}, environment[parts[0]+".delim"])
}
}
return envMap
}