Skip to content

Commit 58581af

Browse files
authored
Merge pull request #141 from seal-runtime/env-vars-lib-92
2 parents 1e250ae + 77e6405 commit 58581af

File tree

16 files changed

+736
-104
lines changed

16 files changed

+736
-104
lines changed

.seal/guided_tour.luau

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -108,8 +108,8 @@ type EnvAndCliArgs = {}; local back: TableOfContents do
108108
end
109109

110110
-- get and set environment variables
111-
local PATH: string? = env.getvar("PATH")
112-
env.setvar("hi", "true")
111+
local PATH: string? = env.vars.get("PATH")
112+
env.vars.set("hi", "true")
113113
end
114114

115115
-- use @std/process to spawn child processes, shell commands, including in a nonblocking manner!

.seal/typedefs/std/env.luau

Lines changed: 0 additions & 35 deletions
This file was deleted.

.seal/typedefs/std/env/init.luau

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
--[=[
2+
A stdlib to interact with the script's running environment.
3+
]=]
4+
export type env = {
5+
--- a list of arguments passed to the program
6+
args: { string },
7+
--- your operating system
8+
os: "Windows" | "Linux" | "Android" | "MacOS" | "Other",
9+
--- the path of the executable
10+
executable_path: string,
11+
--[=[
12+
Get the current working directory of the running process.
13+
14+
Errors if the `cwd` doesn't exist or otherwise isn't accessible (permission denied).
15+
]=]
16+
cwd: () -> string,
17+
-- below are deprecated; set SEAL_ALLOW_DEPRECATED=TRUE to disable warnings at runtime
18+
-- --[=[
19+
-- Gets an environment variable in the current process.
20+
-- ]=]
21+
-- getvar: (key: string) -> string?,
22+
-- --[=[
23+
-- Sets an environment variable in the current process.
24+
25+
-- Note, this function is **unsafe** in multithreaded contexts on Linux.
26+
-- ]=]
27+
-- setvar: (key: string, value: string) -> string,
28+
-- --[=[
29+
-- Removes an environment variable in the current process.
30+
31+
-- Note, this function is **unsafe** in multithreaded contexts on Linux.
32+
-- ]=]
33+
-- removevar: (key: string) -> nil,
34+
vars: typeof(require("@self/vars"))
35+
}
36+
37+
return {} :: env

.seal/typedefs/std/env/vars.luau

Lines changed: 115 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,115 @@
1+
--[=[
2+
Library for interacting with the environment variables of the current process, including handling `.env` files.
3+
Refer to [dotenvy](https://github.com/allan2/dotenvy) for the specific syntax allowed.
4+
5+
*seal* automatically loads environment variables from the `.env` file in your cwd (or up).
6+
7+
Environment variables change this library's behavior:
8+
- `SEAL_LOAD_DOTENV=FALSE` disables loading environment variables from `.env`
9+
- `SEAL_LOAD_DOTENV=TRUE` causes *seal* to error out if it can't find a `.env` file near your cwd.
10+
- if `SEAL_LOAD_DOTENV` is unset or missing, *seal* will load a `.env` file if one is present.
11+
]=]
12+
export type vars = {
13+
--[=[
14+
Get an environment variable from the current process's running environment, your `.env` file, or additional
15+
environment files loaded via `env.vars.load`.
16+
17+
- If the environment variable is not present, returns `nil`.
18+
- If the environment variable is not valid utf-8, returns the invalid utf-8 string (without coercion)
19+
20+
## Usage
21+
22+
```luau
23+
local vars = require("@std/env/vars")
24+
local CONFIG_PATH = vars.get("APP_CONFIG_PATH") or DEFAULT_PATH
25+
local API_KEY = vars.get("API_KEY") or error("missing API_KEY")
26+
```
27+
]=]
28+
get: (key: string) -> string?,
29+
--[=[
30+
Handles boolean-style environment flags in a case-insensitive manner.
31+
32+
- any variations of `"TRUE" | "YES" | "ON" | "1" | "Y" | "T"` => `true`
33+
- any variations of `"FALSE" | "NO" | "OFF" | "0" | "N" | "F"` => `false`
34+
- if the environment variable is not set, returns `default`.
35+
36+
## Errors
37+
38+
- throws an error if the environment variable contains an unexpected string,
39+
is not valid Unicode, or cannot be interpreted as a boolean flag.
40+
41+
## Usage
42+
43+
```luau
44+
local PROD = vars.flag("PROD", false)
45+
```
46+
]=]
47+
flag: (name: string, default: boolean) -> boolean,
48+
--[=[
49+
Validate or transform an environment variable into another type `T`.
50+
51+
## Usage
52+
53+
```luau
54+
local PORT = env.vars.validate("PORT", function(s)
55+
if s == nil then
56+
error("missing PORT env variable")
57+
end
58+
59+
local port = tonumber(s)
60+
if port == nil then
61+
error(`PORT can't be converted to number, got: {s}`)
62+
elseif port < 0 then
63+
error(`PORT cannot be negative, got {port}`)
64+
end
65+
66+
return port
67+
end)
68+
```
69+
]=]
70+
validate: <T>(key: string, f: (value: string?) -> T) -> T,
71+
--[=[
72+
Set an environment variable for the currently-running program.
73+
74+
These variables are not persistent and will disappear after the program terminates; use a shell configuration file for persistent variables.
75+
76+
# Safety
77+
78+
- This function should only be called before initializing any threads (`thread.spawn` or `process.spawn`),
79+
otherwise it may cause undefined behavior when used in multithreaded programs on Unix-like operating systems.
80+
]=]
81+
set: (key: string, value: string) -> (),
82+
--[=[
83+
Remove an environment variable for the currently-running program.
84+
85+
# Safety
86+
87+
- This function should only be called before initializing any threads (`thread.spawn` or `process.spawn`),
88+
otherwise it may cause undefined behavior when used in multithreaded programs on Unix-like operating systems.
89+
]=]
90+
unset: (key: string) -> (),
91+
--[=[
92+
Returns a map-like table that represents the snapshot of the process's environment variables
93+
at the time this function was called.
94+
95+
If you want to modify these variables, use `vars.set`; modifying this table will cause an error.
96+
]=]
97+
all: () -> { [string]: string },
98+
--[=[
99+
Load additional environment variables from the environment file at `path` into the running process's environment.
100+
See [dotenvy](https://github.com/allan2/dotenvy) for the specific syntax allowed.
101+
102+
Note that any `.env` file in your cwd (or up) will already be loaded by *seal*, so you don't need to load it explicitly.
103+
To disable this default behavior, set `SEAL_LOAD_DOTENV=FALSE`.
104+
105+
Set `override` to replace any existing environment variables of the same name; defaults to `false`.
106+
107+
# Safety
108+
109+
- You should only call this function before you spawn any threads via `@std/thread` or `process.spawn`.
110+
Calling this function in multithreaded contexts on Unix-like operating systems may cause undefined behavior.
111+
]=]
112+
load: (path: string, override: boolean?) -> (),
113+
}
114+
115+
return {} :: vars

Cargo.lock

Lines changed: 7 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Cargo.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,7 @@ pretty-hex = "0.4.1"
6464
tungstenite = { version = "0.28.0", features = ["native-tls"] }
6565
url = "2.5.7"
6666
urlencoding = "2.1.3"
67+
dotenvy = "0.15.7"
6768

6869
[profile.dev.package.num-bigint-dig]
6970
opt-level = 3 # otherwise rsa keygen takes forever
Lines changed: 4 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ A stdlib to interact with the script's running environment.
1414
<h4>
1515

1616
```luau
17-
args: {string},
17+
args: { string },
1818
```
1919

2020
</h4>
@@ -67,52 +67,18 @@ Errors if the `cwd` doesn't exist or otherwise isn't accessible (permission deni
6767

6868
---
6969

70-
### env.getvar
70+
### env.vars
7171

7272
<h4>
7373

7474
```luau
75-
function env.getvar(key: string) -> string?,
75+
vars: typeof(require("@self/vars"))
7676
```
7777

7878
</h4>
7979

80-
Gets an environment variable in the current process.
81-
82-
---
83-
84-
### env.setvar
85-
86-
<h4>
87-
88-
```luau
89-
function env.setvar(key: string, value: string) -> string,
90-
```
91-
92-
</h4>
93-
94-
Sets an environment variable in the current process.
95-
96-
Note, this function is **unsafe** in multithreaded contexts on Linux.
97-
98-
---
99-
100-
### env.removevar
101-
102-
<h4>
103-
104-
```luau
105-
function env.removevar(key: string) -> nil,
106-
```
107-
108-
</h4>
109-
110-
Removes an environment variable in the current process.
111-
112-
Note, this function is **unsafe** in multithreaded contexts on Linux.
113-
11480
---
11581

116-
Autogenerated from [std/env.luau](/.seal/typedefs/std/env.luau).
82+
Autogenerated from [std/env/init.luau](/.seal/typedefs/std/env/init.luau).
11783

11884
*seal* is best experienced with inline, in-editor documentation. Please see the linked typedefs file if this documentation is confusing, too verbose, or inaccurate.

0 commit comments

Comments
 (0)