-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.v
161 lines (143 loc) · 4.2 KB
/
main.v
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
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
module main
import os
import regex
enum Parameter {
pending
help
vars
file
output
}
fn main() {
env_vars := os.environ()
mut parse_all := false
mut output := 'appsettings.local.json'
mut vars := map[string]string{}
mut rc_path := '.fromenvrc'
mut parameter := Parameter.pending
for arg in os.args {
if arg == '--help' {
println('Appsettings.FromEnv
Parameter Description Example Default
--vars Specify environment variables to search for. fromenv --vars ConnectionString ClientSecret
--file Specify file containing new-line-separated environment variables to search for. fromenv --file ../.fromenvrc ${rc_path}
--output Specify output file. fromenv --output out/appsettings.local.json ${output}
--all If we should parse ALL environment variables that exists. fromenv --all ${parse_all}
')
return
}
if arg == '--vars' {
parameter = Parameter.vars
continue
} else if arg == '--output' {
parameter = Parameter.output
continue
} else if arg == '--file' {
parameter = Parameter.file
continue
} else if arg == '--all' {
parse_all = true
}
if parameter == Parameter.vars {
vars[arg] = env_vars[arg] or { 'null' }
} else if parameter == Parameter.output {
output = arg
} else if parameter == Parameter.file {
rc_path = arg
}
}
if parse_all {
for key, value in env_vars {
vars[key] = value
println(key)
}
}
// Parse .fromenvrc
if os.exists(rc_path) {
mut rc_vars := os.read_file(rc_path)!.replace('\r\n', '\n').split('\n')
rc_vars.sort()
for var in rc_vars {
if var.trim_space() == "" {
continue
}
vars[var.trim_space()] = env_vars[var] or { 'null' }
}
}
parts := output.split(os.path_separator)
dir := parts[0..parts.len - 1].join(os.path_separator)
if dir != '' && !os.exists(dir) {
os.mkdir(dir)!
}
vars["Logging__LogLevel__Default"] = "Information"
vars["Logging__LogLevel__Test"] = "Warning"
vars["SmtpUrl"] = "2"
vars["Identity__ClientId"] = "true"
mut re := regex.regex_opt(r'(__)|(:)')!
mut num_re := regex.regex_opt(r'^\d+$')!
mut res := '{'
mut prev_parts := []string{}
mut i := 0
for key, value in vars {
if i != 0 { res += ',' } // All values are separated by ',' (except first)
curr_parts := re.split(key).filter(it.trim_space() != '')
// Descend if multiple parts
mut diff_parent := false
mut last_was_array := false
for j, part in curr_parts {
if !diff_parent && j < prev_parts.len - 1 && part == prev_parts[j] {
if j + 1 < curr_parts.len && num_re.matches_string(curr_parts[j + 1]) {
last_was_array = true
} else if last_was_array {
last_was_array = false
}
continue
}
diff_parent = true
res += '\n' + '\t'.repeat(j + 1)
if last_was_array {
last_was_array = false
if j < curr_parts.len - 1 {
res += '{'
}
continue
} else if j + 1 < curr_parts.len && num_re.matches_string(curr_parts[j + 1]) {
last_was_array = true
res += '"${part}": ['
} else {
res += '"${part}": '
if j < curr_parts.len - 1 {
res += '{'
}
}
}
// All values are written once
if value in ['null', 'false', 'true'] || num_re.matches_string(value) {
res += value
} else {
res += '"${value}"'
}
// Ascend if next is not the same
diff_parent = false
next_parts := if i < vars.len - 1 { re.split(vars.keys()[i + 1]).filter(it.trim_space() != '') } else { []string{} }
mut level := curr_parts.len - 1
for j in 0..(curr_parts.len - 1) {
if !diff_parent && j < next_parts.len - 1 && curr_parts[j] == next_parts[j] {
continue
}
diff_parent = true
res += '\n' + '\t'.repeat(level)
if num_re.matches_string(curr_parts[level]) {
res += ']'
} else {
res += '}'
}
level -= 1
}
prev_parts = curr_parts.clone()
i += 1
}
res += "\n}"
println('Writing to ${output}:\n${res}')
os.write_file(output, res.replace('\\', '\\\\'))!
println('Application executed successfully ^^')
}