-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathenv.n
More file actions
102 lines (94 loc) · 3.67 KB
/
env.n
File metadata and controls
102 lines (94 loc) · 3.67 KB
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
import libc
import "utils.n" as *
// --- Typed getters ---
// text retrieves an env var as a string.
// Throws if not set and no default is provided.
// env.text('HOST') → throws if missing
// env.text('HOST', 'localhost') → returns 'localhost' if missing
fn text(string key, ...[string] fallback):string! {
var val = libc.getenv(key.to_cstr())
if val as anyptr == 0 as anyptr {
if fallback.len() > 0 {
return fallback[0]
}
throw errorf('environment variable "%s" is not set', key)
}
return val.to_string()
}
// str is an alias for text.
fn str(string key, ...[string] fallback):string! {
return text(key, ...fallback)
}
// number retrieves an env var as an int.
// Throws if not set (and no default) or not a valid integer.
// env.number('PORT') → throws if missing
// env.number('PORT', 3000) → returns 3000 if missing
fn number(string key, ...[int] fallback):int! {
var val = libc.getenv(key.to_cstr())
if val as anyptr == 0 as anyptr {
if fallback.len() > 0 {
return fallback[0]
}
throw errorf('environment variable "%s" is not set', key)
}
return val.to_string().to_int()
}
// decimal retrieves an env var as a float.
// Throws if not set (and no default) or not a valid float.
// env.decimal('RATE') → throws if missing
// env.decimal('RATE', 0.5) → returns 0.5 if missing
fn decimal(string key, ...[float] fallback):float! {
var val = libc.getenv(key.to_cstr())
if val as anyptr == 0 as anyptr {
if fallback.len() > 0 {
return fallback[0]
}
throw errorf('environment variable "%s" is not set', key)
}
return val.to_string().to_float()
}
// boolean retrieves an env var as a bool.
// Accepts: true/false, 1/0, yes/no (case-insensitive).
// Throws if not set (and no default) or not a valid bool.
// env.boolean('DEBUG') → throws if missing
// env.boolean('DEBUG', false) → returns false if missing
fn boolean(string key, ...[bool] fallback):bool! {
var val = libc.getenv(key.to_cstr())
if val as anyptr == 0 as anyptr {
if fallback.len() > 0 {
return fallback[0]
}
throw errorf('environment variable "%s" is not set', key)
}
return parse_bool(val.to_string())
}
// array retrieves an env var as a string array, split by comma with whitespace trimmed.
// Throws if not set and no defaults are provided.
// The variadic args serve as the default array:
// env.array('TAGS') → throws if missing
// env.array('TAGS', 'a', 'b', 'c') → returns ['a','b','c'] if missing
fn array(string key, ...[string] fallback):[string]! {
var val = libc.getenv(key.to_cstr())
if val as anyptr == 0 as anyptr {
if fallback.len() > 0 {
return fallback
}
throw errorf('environment variable "%s" is not set', key)
}
return split_and_trim(val.to_string(), ','[0])
}
// dict retrieves an env var as a {string:string} map, parsing comma-separated key=value pairs.
// Example: "host=localhost,port=5432" → {"host": "localhost", "port": "5432"}
// Throws if not set and no default is provided.
// env.dict('DB_OPTS') → throws if missing
// env.dict('DB_OPTS', 'host=localhost,port=5432') → returns parsed fallback if missing
fn dict(string key, ...[string] fallback):{string:string}! {
var val = libc.getenv(key.to_cstr())
if val as anyptr == 0 as anyptr {
if fallback.len() > 0 {
return parse_dict(fallback[0])
}
throw errorf('environment variable "%s" is not set', key)
}
return parse_dict(val.to_string())
}