This repository has been archived by the owner on Jan 21, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathvenv.v
67 lines (54 loc) · 1.34 KB
/
venv.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
module venv
import os
const (
w_dir = os.getwd()
env_main = '.env'
)
// Main Config the parser function
pub struct LoaderConfig {
overwrite bool = true // custom default to true
uppercase bool = true // convert env keys to uppercase
}
// load_env is a simple loader
pub fn load_env() {
loader({})
}
// load_env_conf loads .env with defined configurations
pub fn load_env_conf(config LoaderConfig) {
loader(config)
}
// loader is the main .env loader / parser
fn loader(config LoaderConfig) {
env_path := os.join_path(w_dir, env_main)
// check if .env exists in the working_dir
if os.is_file(env_path) {
// read each line of its content
env_lines := os.read_lines(env_path) or {
return
}
// parse each variables
for i in env_lines{
// defined variables should
// be in the form VAR=value
if i.contains("="){
var, value := split_slash(i)
// set each env variable
// .. -> get the values of `LoaderConfig`
// .. -> automatically trim all spaces
os.setenv(set_upper(var, config.uppercase).trim_space(), value.trim_space(), config.overwrite)
}
}
}
}
// splites the defined variable
fn split_slash(variable string) (string, string) {
x := variable.split('=')
return x[0], x[1]
}
// return uppercase if upper is true
fn set_upper(x string, upper bool) string {
if upper {
return x.to_upper()
}
return x
}