Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Read environment variables #20

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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 @@ -31,3 +32,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 variables](./universal/test/operating_system/read_environment_variables.gleam)
1 change: 1 addition & 0 deletions universal/gleam.toml
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ 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"
tom = ">= 1.1.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 @@ -17,6 +18,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
36 changes: 36 additions & 0 deletions universal/test/operating_system/read_environment_variables.gleam
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
//// # Read environment variables
////
//// The envoy package is used to work with environment variables. This package
//// works on both Erlang and JavaScript targets.
////
//// ## Dependencies
////
//// - https://hex.pm/packages/envoy

import envoy
import gleam/dict

pub fn main_test() {
// The `unset` function can be used to unset environment variables. We are
// using it first here to reset the PORT environment variable to a known
// state.
envoy.unset("PORT")

// We've used `let assert` to crash if any of these functions fail. In a real
// application or library you'd want to handle the results properly.

// The `get` function gets environment variables by name. It returns an error
// if the environment variable is not set.
let assert Error(Nil) = envoy.get("PORT")

// The `set` function sets environment variables.
envoy.set("PORT", "8080")

// The `get` function returns `Ok` if the environment variable is set.
let assert Ok("8080") = envoy.get("PORT")

// The `all` function can be used to get a Dict of all set environment
// variables.
let environment_variables = envoy.all()
let assert Ok("8080") = dict.get(environment_variables, "PORT")
}