Skip to content

Commit 3e6dd78

Browse files
committed
Read environment variables
Addresses #15
1 parent ce3e2f7 commit 3e6dd78

File tree

4 files changed

+48
-0
lines changed

4 files changed

+48
-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 variable](./universal/test/operating_system/read_environment_variable.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: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
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+
//// Note that we are using `let assert` to crash if any of these functions
7+
//// fail. In a real application or library, you would want to handle results
8+
//// properly.
9+
////
10+
//// ## Dependencies
11+
////
12+
//// - https://hex.pm/packages/envoy
13+
////
14+
15+
import envoy
16+
import gleam/dict
17+
18+
pub fn main_test() {
19+
// First, unset the PORT variable.
20+
//
21+
// We do this fist to get the environment into a known state. If the PORT
22+
// variable is not already set, this will have no effect.
23+
let Nil = envoy.unset("PORT")
24+
25+
// Try to get an environment variable by name.
26+
//
27+
// We expect that this will fail, as we unset this variable above.
28+
let assert Error(Nil) = envoy.get("PORT")
29+
30+
// Set the PORT environment variable to 8080.
31+
let Nil = envoy.set("PORT", "8080")
32+
33+
// Now that the PORT variable has been set, we expect to successfully get its
34+
// value.
35+
let assert Ok("8080") = envoy.get("PORT")
36+
37+
// We can also get a Dict of all set environment variables.
38+
let environment_variables = envoy.all()
39+
let assert Ok("8080") = dict.get(environment_variables, "PORT")
40+
}

0 commit comments

Comments
 (0)