Skip to content

Commit

Permalink
Read environment variables
Browse files Browse the repository at this point in the history
Addresses #15
  • Loading branch information
mooreryan committed Sep 23, 2024
1 parent 6e1617b commit e15961e
Show file tree
Hide file tree
Showing 4 changed files with 48 additions and 0 deletions.
5 changes: 5 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ Examples showing how to do many things in Gleam!
- [Data structures](#data-structures)
- [File system](#file-system)
- [Formats](#formats)
- [Operating system](#operating-system)

## Algorithms

Expand All @@ -29,3 +30,7 @@ Examples showing how to do many things in Gleam!
- [Rendering HTML](./universal/test/formats/rendering_html.gleam)
- [Rendering JSON](./universal/test/formats/rendering_json.gleam)
- [Rendering XML](./universal/test/formats/rendering_xml.gleam)

## Operating system

- [Read environment variable](./universal/test/operating_system/read_environment_variable.gleam)
1 change: 1 addition & 0 deletions universal/gleam.toml
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ xmb = ">= 1.0.0 and < 2.0.0"
gleam_json = ">= 1.0.1 and < 2.0.0"
lustre = ">= 4.4.4 and < 5.0.0"
gleam_crypto = ">= 1.3.0 and < 2.0.0"
envoy = ">= 1.0.1 and < 2.0.0"

[dev-dependencies]
gleeunit = ">= 1.0.0 and < 2.0.0"
2 changes: 2 additions & 0 deletions universal/manifest.toml
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
# You typically do not need to edit this file

packages = [
{ name = "envoy", version = "1.0.1", build_tools = ["gleam"], requirements = ["gleam_stdlib"], otp_app = "envoy", source = "hex", outer_checksum = "CFAACCCFC47654F7E8B75E614746ED924C65BD08B1DE21101548AC314A8B6A41" },
{ name = "filepath", version = "1.0.0", build_tools = ["gleam"], requirements = ["gleam_stdlib"], otp_app = "filepath", source = "hex", outer_checksum = "EFB6FF65C98B2A16378ABC3EE2B14124168C0CE5201553DE652E2644DCFDB594" },
{ name = "gleam_crypto", version = "1.3.0", build_tools = ["gleam"], requirements = ["gleam_stdlib"], otp_app = "gleam_crypto", source = "hex", outer_checksum = "ADD058DEDE8F0341F1ADE3AAC492A224F15700829D9A3A3F9ADF370F875C51B7" },
{ name = "gleam_erlang", version = "0.26.0", build_tools = ["gleam"], requirements = ["gleam_stdlib"], otp_app = "gleam_erlang", source = "hex", outer_checksum = "3DF72F95F4716883FA51396FB0C550ED3D55195B541568CAF09745984FD37AD1" },
Expand All @@ -16,6 +17,7 @@ packages = [
]

[requirements]
envoy = { version = ">= 1.0.1 and < 2.0.0" }
gleam_crypto = { version = ">= 1.3.0 and < 2.0.0" }
gleam_json = { version = ">= 1.0.1 and < 2.0.0" }
gleam_stdlib = { version = ">= 0.34.0 and < 2.0.0" }
Expand Down
40 changes: 40 additions & 0 deletions universal/test/operating_system/read_environment_variable.gleam
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
//// # Read environment variables
////
//// The envoy package is used to work with environment variables. This package
//// works on both Erlang and JavaScript targets.
////
//// Note that we are using `let assert` to crash if any of these functions
//// fail. In a real application or library, you would want to handle results
//// properly.
////
//// ## Dependencies
////
//// - https://hex.pm/packages/envoy
////

import envoy
import gleam/dict

pub fn main_test() {
// First, unset the PORT variable.
//
// We do this fist to get the environment into a known state. If the PORT
// variable is not already set, this will have no effect.
let Nil = envoy.unset("PORT")

// Try to get an environment variable by name.
//
// We expect that this will fail, as we unset this variable above.
let assert Error(Nil) = envoy.get("PORT")

// Set the PORT environment variable to 8080.
let Nil = envoy.set("PORT", "8080")

// Now that the PORT variable has been set, we expect to successfully get its
// value.
let assert Ok("8080") = envoy.get("PORT")

// We can also get a Dict of all set environment variables.
let environment_variables = envoy.all()
let assert Ok("8080") = dict.get(environment_variables, "PORT")
}

0 comments on commit e15961e

Please sign in to comment.