diff --git a/README.md b/README.md index 0d16439..e8990a9 100644 --- a/README.md +++ b/README.md @@ -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 @@ -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) diff --git a/universal/gleam.toml b/universal/gleam.toml index 08e21a3..d111c59 100644 --- a/universal/gleam.toml +++ b/universal/gleam.toml @@ -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" diff --git a/universal/manifest.toml b/universal/manifest.toml index 94cade7..1a69272 100644 --- a/universal/manifest.toml +++ b/universal/manifest.toml @@ -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" }, @@ -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" } diff --git a/universal/test/operating_system/read_environment_variables.gleam b/universal/test/operating_system/read_environment_variables.gleam new file mode 100644 index 0000000..1dab836 --- /dev/null +++ b/universal/test/operating_system/read_environment_variables.gleam @@ -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") +}