-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathenv2toml.go
137 lines (116 loc) · 2.72 KB
/
env2toml.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
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
package env2toml
import (
"fmt"
"os"
"sort"
"strconv"
"strings"
"github.com/BurntSushi/toml"
)
type VarItem struct {
Section string
Array bool
ArrayIndex int
Key string
Value string
}
func Parse(prefix string) (string, error) {
var varList []VarItem
var sections []string
// parse Environ
for _, e := range os.Environ() {
pair := strings.SplitN(e, "=", 2)
k, v := pair[0], pair[1]
if strings.HasPrefix(k, prefix) {
s := strings.ToLower(strings.TrimPrefix(k, prefix))
keys := strings.Split(s, "__")
var section string
isArray := false
arrayIndex := -1
for i := 0; i < len(keys)-1; i++ {
if index, err := strconv.Atoi(keys[i]); err == nil {
isArray = true
arrayIndex = index
continue
}
if section != "" {
section = section + "." + keys[i]
} else {
section = keys[i]
}
if !contains(sections, section) {
sections = append(sections, section)
}
}
newKey := keys[len(keys)-1]
value := v
// is a toml value?
if !isValidTomlValue(`a=` + v) {
value = fmt.Sprintf(`"%s"`, strings.ReplaceAll(v, `\`, `\\`))
}
varList = append(varList, VarItem{
Section: section,
Array: isArray,
ArrayIndex: arrayIndex,
Key: newKey,
Value: value,
})
}
}
var result strings.Builder
// is a section
for _, item := range varList {
if item.Section == "" {
result.WriteString(fmt.Sprintf("%s=%s\n", item.Key, item.Value))
}
}
// Generate TOML text for sections
for _, sec := range sections {
arrayItems := make(map[int][]VarItem)
isArray := false
for _, item := range varList {
if item.Section == sec {
if item.Array {
isArray = true
arrayItems[item.ArrayIndex] = append(arrayItems[item.ArrayIndex], item)
} else {
arrayItems[0] = append(arrayItems[0], item)
}
}
}
if !isArray {
result.WriteString(fmt.Sprintf("\n[%s]\n", sec))
for _, item := range arrayItems[0] {
result.WriteString(fmt.Sprintf("%s=%s\n", item.Key, item.Value))
}
continue
}
keys := make([]int, 0, len(arrayItems))
for key := range arrayItems {
keys = append(keys, key)
}
sort.Ints(keys)
for _, k := range keys {
items := arrayItems[k]
result.WriteString(fmt.Sprintf("\n[[%s]]\n", sec))
for _, item := range items {
result.WriteString(fmt.Sprintf("%s=%s\n", item.Key, item.Value))
}
}
}
return result.String(), nil
}
func contains(slice []string, item string) bool {
for _, s := range slice {
if s == item {
return true
}
}
return false
}
func isValidTomlValue(value string) bool {
// Github.com/BurntSushi/toml test toml value.
var result map[string]interface{}
_, err := toml.Decode(value, &result)
return err == nil
}