...
1
16
17 package utils
18
19 import (
20 "bufio"
21 "log"
22 "os"
23 "strings"
24 )
25
26 func ParseEnvironment() {
27 if _, err := os.Stat(".env"); os.IsNotExist(err) {
28 log.Printf("Environment Variable file (.env) is not present. Relying on Global Environment Variables")
29 }
30
31 setEnvVariable("CLIENT_ID", os.Getenv("CLIENT_ID"))
32 setEnvVariable("ISSUER", os.Getenv("ISSUER"))
33 setEnvVariable("USERNAME", os.Getenv("USERNAME"))
34 setEnvVariable("PASSWORD", os.Getenv("PASSWORD"))
35 if os.Getenv("CLIENT_ID") == "" {
36 log.Printf("Could not resolve a CLIENT_ID environment variable.")
37 os.Exit(1)
38 }
39
40 if os.Getenv("ISSUER") == "" {
41 log.Printf("Could not resolve a ISSUER environment variable.")
42 os.Exit(1)
43 }
44 }
45
46 func setEnvVariable(env string, current string) {
47 if current != "" {
48 return
49 }
50
51 file, _ := os.Open(".env")
52 defer file.Close()
53
54 lookInFile := bufio.NewScanner(file)
55 lookInFile.Split(bufio.ScanLines)
56
57 for lookInFile.Scan() {
58 parts := strings.Split(lookInFile.Text(), "=")
59 key, value := parts[0], parts[1]
60 if key == env {
61 os.Setenv(key, value)
62 }
63 }
64 }
65
View as plain text