module ENV

Overview

ENV is a hash-like accessor for environment variables.

Example

# Set env var PORT to a default if not already set
ENV["PORT"] ||= "5000"
# Later use that env var.
puts ENV["PORT"].to_i

NOTE All keys and values are strings. You must take care to cast other types at runtime, e.g. integer port numbers.

Extended Modules

Defined in:

secrets-env.cr

Constant Summary

DEFAULT_SECRETS_PATH = "/run/secrets"

Class Method Summary

Class Method Detail

def self.accessed(static_only) : Array(String) #

DEPRECATED Static resolution no longer supported


[View source]
def self.accessed : Array(String) #

Returns the set of all environment variables or secrets that have been accessed by the program.


[View source]
def self.each(&) #

Iterates over all KEY=VALUE pairs of environment variables, yielding both the key and value.

ENV.each do |key, value|
  puts "#{key} => #{value}"
end

[View source]
def self.fetch(key : String, &block : String -> String | Nil) #

Retrieves a value corresponding to a given key. The value will be retrieved from (in order of priorities): system env vars, available secrets files or the return value of the block.

This override the default ENV module behaviour to support reading of secrets injected to an environment from docker-compose, kubernetes and other orchestration tools.


[View source]
def self.fetch_secret(key : String, &block : String -> String | Nil) #

Retrieves a value corresponding to the given key. Return the value of the block if the key does not exist.


[View source]
def self.has_key?(key : String) : Bool #

Returns true if the environment variable named key exists or an secrets file of the same name is available.


[View source]
def self.has_secret?(key : String) : Bool #

Returns true if the secret named key exists.


[View source]