Skip to content

Commit 046bed9

Browse files
committed
Read environment variables
Addresses #15
1 parent ce3e2f7 commit 046bed9

File tree

4 files changed

+44
-0
lines changed

4 files changed

+44
-0
lines changed

README.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ Examples showing how to do many things in Gleam!
77
- [Data structures](#data-structures)
88
- [File system](#file-system)
99
- [Formats](#formats)
10+
- [Operating system](#operating-system)
1011

1112
## Algorithms
1213

@@ -31,3 +32,7 @@ Examples showing how to do many things in Gleam!
3132
- [Rendering HTML](./universal/test/formats/rendering_html.gleam)
3233
- [Rendering JSON](./universal/test/formats/rendering_json.gleam)
3334
- [Rendering XML](./universal/test/formats/rendering_xml.gleam)
35+
36+
## Operating system
37+
38+
- [Read environment variables](./universal/test/operating_system/read_environment_variables.gleam)

universal/gleam.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ gleam_json = ">= 1.0.1 and < 2.0.0"
2121
lustre = ">= 4.4.4 and < 5.0.0"
2222
gleam_crypto = ">= 1.3.0 and < 2.0.0"
2323
tom = ">= 1.1.0 and < 2.0.0"
24+
envoy = ">= 1.0.1 and < 2.0.0"
2425

2526
[dev-dependencies]
2627
gleeunit = ">= 1.0.0 and < 2.0.0"

universal/manifest.toml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
# You typically do not need to edit this file
33

44
packages = [
5+
{ name = "envoy", version = "1.0.1", build_tools = ["gleam"], requirements = ["gleam_stdlib"], otp_app = "envoy", source = "hex", outer_checksum = "CFAACCCFC47654F7E8B75E614746ED924C65BD08B1DE21101548AC314A8B6A41" },
56
{ name = "filepath", version = "1.0.0", build_tools = ["gleam"], requirements = ["gleam_stdlib"], otp_app = "filepath", source = "hex", outer_checksum = "EFB6FF65C98B2A16378ABC3EE2B14124168C0CE5201553DE652E2644DCFDB594" },
67
{ name = "gleam_crypto", version = "1.3.0", build_tools = ["gleam"], requirements = ["gleam_stdlib"], otp_app = "gleam_crypto", source = "hex", outer_checksum = "ADD058DEDE8F0341F1ADE3AAC492A224F15700829D9A3A3F9ADF370F875C51B7" },
78
{ name = "gleam_erlang", version = "0.26.0", build_tools = ["gleam"], requirements = ["gleam_stdlib"], otp_app = "gleam_erlang", source = "hex", outer_checksum = "3DF72F95F4716883FA51396FB0C550ED3D55195B541568CAF09745984FD37AD1" },
@@ -17,6 +18,7 @@ packages = [
1718
]
1819

1920
[requirements]
21+
envoy = { version = ">= 1.0.1 and < 2.0.0" }
2022
gleam_crypto = { version = ">= 1.3.0 and < 2.0.0" }
2123
gleam_json = { version = ">= 1.0.1 and < 2.0.0" }
2224
gleam_stdlib = { version = ">= 0.34.0 and < 2.0.0" }
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
//// # Read environment variables
2+
////
3+
//// The envoy package is used to work with environment variables. This package
4+
//// works on both Erlang and JavaScript targets.
5+
////
6+
//// ## Dependencies
7+
////
8+
//// - https://hex.pm/packages/envoy
9+
10+
import envoy
11+
import gleam/dict
12+
13+
pub fn main_test() {
14+
// The `unset` function can be used to unset environment variables. We are
15+
// using it first here to reset the PORT environment variable to a known
16+
// state.
17+
envoy.unset("PORT")
18+
19+
// We've used `let assert` to crash if any of these functions fail. In a real
20+
// application or library you'd want to handle the results properly.
21+
22+
// The `get` function gets environment variables by name. It returns an error
23+
// if the environment variable is not set.
24+
let assert Error(Nil) = envoy.get("PORT")
25+
26+
// The `set` function sets environment variables.
27+
envoy.set("PORT", "8080")
28+
29+
// The `get` function returns `Ok` if the environment variable is set.
30+
let assert Ok("8080") = envoy.get("PORT")
31+
32+
// The `all` function can be used to get a Dict of all set environment
33+
// variables.
34+
let environment_variables = envoy.all()
35+
let assert Ok("8080") = dict.get(environment_variables, "PORT")
36+
}

0 commit comments

Comments
 (0)