env
Environment variables: reading them with the right type, .env files, the run profile, and masking sensitive values.
import env
Settings such as a server port, a database address and API keys belong in environment variables rather than in the code. The env package reads them with the right type and gives you a default when they are not set. It can load them from a .env file too.
The "profile" is read from SALAM_ENV and says whether the program is running in development, test or production.
Functions
Get
func Get(name: str): strimport env
func main:
env.Set("PORT", "8080")
env.Set("DEBUG", "true")
env.Set("RATIO", "0.75")
println env.Get("PORT"), env.Has("PORT"), env.Has("NOPE")
println env.GetInt("PORT", 80), env.GetInt("NOPE", 80)
println env.GetBool("DEBUG", false), env.GetFloat("RATIO", 1.0)
println env.GetOr("HOST", "localhost")
env.Unset("PORT")
println env.Has("PORT")
env.Set("API_KEY", "example-not-a-real-key")
println env.Mask("example-not-a-real-key"), env.GetMasked("API_KEY")
env.Set("NAME", "Salam")
println env.Expand("${NAME} on port ${PORT:-3000}")
endGetOr
func GetOr(name: str, def: str): strThe example for this item is shown with the ones above: env-Get.salam
Has
func Has(name: str): boolThe example for this item is shown with the ones above: env-Get.salam
GetInt
func GetInt(name: str, def: i64): i64The example for this item is shown with the ones above: env-Get.salam
GetFloat
func GetFloat(name: str, def: f64): f64The example for this item is shown with the ones above: env-Get.salam
GetBool
func GetBool(name: str, def: bool): booltrue, 1 and yes count as true.The example for this item is shown with the ones above: env-Get.salam
Set
func Set(name: str, value: str): boolThe example for this item is shown with the ones above: env-Get.salam
Unset
func Unset(name: str): boolThe example for this item is shown with the ones above: env-Get.salam
Mask
func Mask(value: str): strThe example for this item is shown with the ones above: env-Get.salam
GetMasked
func GetMasked(name: str): strThe example for this item is shown with the ones above: env-Get.salam
Profile
func Profile(): strSALAM_ENV.import env
func main:
env.Set("SALAM_ENV", "production")
println env.Profile(), env.IsProduction(), env.IsDevelopment(), env.IsTest(), env.IsStaging()
env.Set("SALAM_ENV", "test")
println env.Profile(), env.IsTest()
endIsProduction
func IsProduction(): boolproduction profile.The example for this item is shown with the ones above: env-Profile.salam
IsDevelopment
func IsDevelopment(): booldevelopment profile.The example for this item is shown with the ones above: env-Profile.salam
IsTest
func IsTest(): booltest profile.The example for this item is shown with the ones above: env-Profile.salam
IsStaging
func IsStaging(): boolstaging profile.The example for this item is shown with the ones above: env-Profile.salam
Load
func Load(path: str): intKEY=VALUE file without changing variables that are already set. It returns how many variables were read.import env
import io
func main:
io.WriteFile(".env", "DB_HOST=localhost\nDB_PORT=5432\n# a comment\n")
println env.Load(".env"), env.Get("DB_HOST")
env.Set("DB_PORT", "1")
env.Load(".env")
println env.Get("DB_PORT")
env.Overload(".env")
println env.Get("DB_PORT")
println env.LoadText("A=1\nB=2"), env.Get("B")
println env.LoadDotEnv(".env")
io.WriteFile(".env.test", "MODE=testing\n")
env.Set("SALAM_ENV", "test")
println env.LoadProfile("."), env.Get("MODE")
endOverload
func Overload(path: str): intLoad, but it replaces existing variables with the file's values too.The example for this item is shown with the ones above: env-Load.salam
LoadText
func LoadText(text: str): intThe example for this item is shown with the ones above: env-Load.salam
LoadDotEnv
func LoadDotEnv(path: str): int.env file.The example for this item is shown with the ones above: env-Load.salam
LoadProfile
func LoadProfile(dir: str): int.env.<profile> file from the given directory, such as .env.test in the test profile.The example for this item is shown with the ones above: env-Load.salam
Expand
func Expand(text: str): str${name} and ${name:-default} in text with the values of the variables.The example for this item is shown with the ones above: env-Get.salam