Created
March 8, 2018 20:36
-
-
Save tonyfabeen/024681893849574b134409304c327997 to your computer and use it in GitHub Desktop.
DotEnv in Golang
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
KEY_A=value-a | |
KEY_B=value-b | |
KEY_C=value-c | |
DB_HOST=postgres.database.com | |
DB_PASSWORD=dr.i,G<I=!B3f[U8Qg%z9oP}FqYn\r |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package config | |
import ( | |
"bufio" | |
"log" | |
"os" | |
"regexp" | |
) | |
func LoadFile(filepath string) (*os.File, error) { | |
file, err := os.Open(filepath) | |
if err != nil { | |
return nil, err | |
} | |
return file, nil | |
} | |
func ParseFile(file *os.File) map[string]string { | |
result := make(map[string]string) | |
scanner := bufio.NewScanner(file) | |
regex, _ := regexp.Compile(`(\w+)=(.+)`) | |
for scanner.Scan() { | |
line := scanner.Text() | |
matches := regex.FindAllStringSubmatch(line, -1) | |
for _, match := range matches { | |
log.Println("[config.ParseFile] ", match[1], match[2]) | |
result[match[1]] = match[2] | |
} | |
} | |
return result | |
} | |
func SetEnvVariables(envVariables map[string]string) { | |
for k, v := range envVariables { | |
log.Println("[config.SetEnvVariables]", k, v) | |
os.Setenv(k, v) | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package config | |
import ( | |
"os" | |
"testing" | |
) | |
var envFile = ".env.example" | |
func TestLoadFile(t *testing.T) { | |
result, _ := LoadFile(envFile) | |
if result == nil { | |
t.Error("file should not be nil") | |
} | |
} | |
func TestParseFile(t *testing.T) { | |
file, _ := LoadFile(envFile) | |
lines := ParseFile(file) | |
if lines == nil { | |
t.Error("should return file lines") | |
} | |
if value, ok := lines["KEY_A"]; !ok { | |
t.Error("should have the key", value) | |
} | |
if value := lines["KEY_A"]; value != "value-a" { | |
t.Error("should have right value", value) | |
} | |
if value, ok := lines["DB_HOST"]; !ok { | |
t.Error("should have the key DB_HOST", value) | |
} | |
if value := lines["DB_HOST"]; value != "postgres.database.com" { | |
t.Error("should have right value", value) | |
} | |
} | |
func TestEnvVariables(t *testing.T) { | |
file, _ := LoadFile(envFile) | |
envVars := ParseFile(file) | |
SetEnvVariables(envVars) | |
for k, v := range envVars { | |
if value := os.Getenv(k); value != v { | |
t.Error("should setup the right value", k, v) | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment