From 240b76346afdea887a68ddad080db87c92b360b6 Mon Sep 17 00:00:00 2001 From: Jonni Liljamo Date: Wed, 11 Oct 2023 15:24:15 +0300 Subject: [PATCH] feat: util package with two env var loaders --- util/env.go | 41 +++++++++++++++++++++++++++++++++++++++++ 1 file changed, 41 insertions(+) create mode 100644 util/env.go diff --git a/util/env.go b/util/env.go new file mode 100644 index 0000000..c97907d --- /dev/null +++ b/util/env.go @@ -0,0 +1,41 @@ +/* + * Copyright (C) 2023 Jonni Liljamo + * + * This file is licensed under GPL-3.0-or-later, see NOTICE and LICENSE for + * more information. + */ +package util + +import ( + "log" + "os" + "strconv" +) + +func LoadEnvStr(key string, def string) string { + value, found := os.LookupEnv(key) + if found { + return value + } + + if def == "" { + log.Fatalf("[erya] Environment variable %s is empty, and has no default", key) + } else { + log.Printf("[erya] Environment variable %s is empty, using default '%s'", key, def) + } + return def +} + +func LoadEnvBool(key string, def bool) bool { + value, found := os.LookupEnv(key) + if found { + res, err := strconv.ParseBool(value) + if err != nil { + log.Fatalf("[erya] Environment variable %s failed to parse to bool from '%s'", key, value) + } + return res + } + + log.Printf("[erya] Environment variable %s is empty, using default '%t'", key, def) + return def +} -- 2.44.1