v / vlib / os
Raw file | 49 loc (38 sloc) | 1.06 KB | Latest commit hash 5f0160bf1
1module os
2
3$if js_node {
4 #global.$ENV = $process.env
5} $else {
6 #const global = $global;
7 #global.$ENV = {}
8}
9
10// setenv sets the value of an environment variable with `name` to `value`.
11pub fn setenv(key string, val string, overwrite bool) {
12 #if ($ENV[key] && !(overwrite.valueOf())) return;
13 #$ENV[key] = val + '';
14}
15
16// `getenv` returns the value of the environment variable named by the key.
17pub fn getenv(key string) string {
18 mut res := ''
19 #if ($ENV[key]) res = new string($ENV[key])
20
21 return res
22}
23
24// `getenv_opt` returns the value of a given environment variable.
25// Returns `none` if the environment variable does not exist.
26pub fn getenv_opt(key string) ?string {
27 #if (!$ENV[key]) return none__;
28
29 mut res := ''
30 #if ($ENV[key]) res = new string($ENV[key]);
31
32 return res
33}
34
35// unsetenv clears an environment variable with `name`.
36pub fn unsetenv(name string) int {
37 #$ENV[name] = ""
38
39 return 1
40}
41
42pub fn environ() map[string]string {
43 mut res := map[string]string{}
44 #for (const key in $ENV) {
45 #res.map.set(key,$ENV[key])
46 #}
47
48 return res
49}