DEVELOPMENT ENVIRONMENT

~liljamo/tixe

ref: b76c91464a188c22423f19d39b6aa0bcc3108109 tixe/util/env.go -rw-r--r-- 758 bytes
b76c9146Jonni Liljamo feat: doc update, login page divider 1 year, 4 months ago
                                                                                
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
package util

import (
	"log"
	"os"
	"strconv"
)

func LoadVar(key string, def string) string {
	value, found := os.LookupEnv(key)
	if found {
		return value
	}

	if def == "" {
		log.Fatalf("[tixe/util] Environment variable %s is empty, and has no default!", key)
	} else {
		log.Printf("[tixe/util] Environment variable %s is empty, using default '%s'", key, def)
	}
	return def
}

func LoadVarBool(key string, def bool) bool {
	value, found := os.LookupEnv(key)
	if found {
		res, err := strconv.ParseBool(value)
		if err != nil {
			log.Fatalf("[tixe/util] Environment variable %s failed to parse to bool from '%s'", key, value)
		}
		return res
	}

	log.Printf("[tixe/util] Environment variable %s is empty, using default '%t'", key, def)
	return def
}