forked from coop-cloud-mirrors/godotenv
		
	Compare commits
	
		
			17 Commits
		
	
	
		
			v1
			...
			setup_trav
		
	
	| Author | SHA1 | Date | |
|---|---|---|---|
| 0a959c8d8f | |||
| bcaccd4f68 | |||
| 22e45bfff4 | |||
| 2fc79dff51 | |||
| 726cc8b906 | |||
| 861984c215 | |||
| 0ff0c0fc7a | |||
| 4ed13390c0 | |||
| 008304c688 | |||
| 443e926da0 | |||
| 2ed25fcb28 | |||
| f6581828bb | |||
| d29c003c20 | |||
| 19b5c2bf30 | |||
| e1c92610d7 | |||
| ead2e75027 | |||
| dc9cc93c4e | 
							
								
								
									
										8
									
								
								.travis.yml
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										8
									
								
								.travis.yml
									
									
									
									
									
										Normal file
									
								
							| @ -0,0 +1,8 @@ | |||||||
|  | language: go | ||||||
|  |  | ||||||
|  | go: | ||||||
|  |   - 1.8 | ||||||
|  |  | ||||||
|  | os: | ||||||
|  |   - linux | ||||||
|  |   - osx | ||||||
| @ -1,4 +1,4 @@ | |||||||
| # GoDotEnv [](https://app.wercker.com/project/bykey/507594c2ec7e60f19403a568dfea0f78) | # GoDotEnv [](https://travis-ci.org/joho/godotenv) [](https://ci.appveyor.com/project/joho/godotenv) | ||||||
|  |  | ||||||
| A Go (golang) port of the Ruby dotenv project (which loads env vars from a .env file) | A Go (golang) port of the Ruby dotenv project (which loads env vars from a .env file) | ||||||
|  |  | ||||||
|  | |||||||
| @ -11,5 +11,5 @@ package autoload | |||||||
| import "github.com/joho/godotenv" | import "github.com/joho/godotenv" | ||||||
|  |  | ||||||
| func init() { | func init() { | ||||||
| 	_ = godotenv.Load() | 	godotenv.Load() | ||||||
| } | } | ||||||
|  | |||||||
| @ -45,7 +45,7 @@ example | |||||||
|  |  | ||||||
| 	// take rest of args and "exec" them | 	// take rest of args and "exec" them | ||||||
| 	cmd := args[0] | 	cmd := args[0] | ||||||
| 	cmdArgs := args[1:len(args)] | 	cmdArgs := args[1:] | ||||||
|  |  | ||||||
| 	err := godotenv.Exec(envFilenames, cmd, cmdArgs) | 	err := godotenv.Exec(envFilenames, cmd, cmdArgs) | ||||||
| 	if err != nil { | 	if err != nil { | ||||||
|  | |||||||
							
								
								
									
										2
									
								
								fixtures/invalid1.env
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										2
									
								
								fixtures/invalid1.env
									
									
									
									
									
										Normal file
									
								
							| @ -0,0 +1,2 @@ | |||||||
|  | INVALID LINE | ||||||
|  | foo=bar | ||||||
							
								
								
									
										124
									
								
								godotenv.go
									
									
									
									
									
								
							
							
						
						
									
										124
									
								
								godotenv.go
									
									
									
									
									
								
							| @ -1,18 +1,16 @@ | |||||||
| /* | // Package godotenv is a go port of the ruby dotenv library (https://github.com/bkeepers/dotenv) | ||||||
| A go port of the ruby dotenv library (https://github.com/bkeepers/dotenv) | // | ||||||
|  | // Examples/readme can be found on the github page at https://github.com/joho/godotenv | ||||||
| Examples/readme can be found on the github page at https://github.com/joho/godotenv | // | ||||||
|  | // The TL;DR is that you make a .env file that looks something like | ||||||
| The TL;DR is that you make a .env file that looks something like | // | ||||||
|  | // 		SOME_ENV_VAR=somevalue | ||||||
| 		SOME_ENV_VAR=somevalue | // | ||||||
|  | // and then in your go code you can call | ||||||
| and then in your go code you can call | // | ||||||
|  | // 		godotenv.Load() | ||||||
| 		godotenv.Load() | // | ||||||
|  | // and all the env vars declared in .env will be avaiable through os.Getenv("SOME_ENV_VAR") | ||||||
| and all the env vars declared in .env will be avaiable through os.Getenv("SOME_ENV_VAR") |  | ||||||
| */ |  | ||||||
| package godotenv | package godotenv | ||||||
|  |  | ||||||
| import ( | import ( | ||||||
| @ -23,22 +21,22 @@ import ( | |||||||
| 	"strings" | 	"strings" | ||||||
| ) | ) | ||||||
|  |  | ||||||
| /* | // Load will read your env file(s) and load them into ENV for this process. | ||||||
| 	Call this function as close as possible to the start of your program (ideally in main) | // | ||||||
|  | // Call this function as close as possible to the start of your program (ideally in main) | ||||||
| 	If you call Load without any args it will default to loading .env in the current path | // | ||||||
|  | // If you call Load without any args it will default to loading .env in the current path | ||||||
| 	You can otherwise tell it which files to load (there can be more than one) like | // | ||||||
|  | // You can otherwise tell it which files to load (there can be more than one) like | ||||||
| 		godotenv.Load("fileone", "filetwo") | // | ||||||
|  | //		godotenv.Load("fileone", "filetwo") | ||||||
| 	It's important to note that it WILL NOT OVERRIDE an env variable that already exists - consider the .env file to set dev vars or sensible defaults | // | ||||||
| */ | // It's important to note that it WILL NOT OVERRIDE an env variable that already exists - consider the .env file to set dev vars or sensible defaults | ||||||
| func Load(filenames ...string) (err error) { | func Load(filenames ...string) (err error) { | ||||||
| 	filenames = filenamesOrDefault(filenames) | 	filenames = filenamesOrDefault(filenames) | ||||||
|  |  | ||||||
| 	for _, filename := range filenames { | 	for _, filename := range filenames { | ||||||
| 		err = loadFile(filename) | 		err = loadFile(filename, false) | ||||||
| 		if err != nil { | 		if err != nil { | ||||||
| 			return // return early on a spazout | 			return // return early on a spazout | ||||||
| 		} | 		} | ||||||
| @ -46,10 +44,31 @@ func Load(filenames ...string) (err error) { | |||||||
| 	return | 	return | ||||||
| } | } | ||||||
|  |  | ||||||
| /* | // Overload will read your env file(s) and load them into ENV for this process. | ||||||
|   Read all env (with same file loading semantics as Load) but return values as | // | ||||||
|   a map rather than automatically writing values into env | // Call this function as close as possible to the start of your program (ideally in main) | ||||||
| */ | // | ||||||
|  | // If you call Overload without any args it will default to loading .env in the current path | ||||||
|  | // | ||||||
|  | // You can otherwise tell it which files to load (there can be more than one) like | ||||||
|  | // | ||||||
|  | //		godotenv.Overload("fileone", "filetwo") | ||||||
|  | // | ||||||
|  | // It's important to note this WILL OVERRIDE an env variable that already exists - consider the .env file to forcefilly set all vars. | ||||||
|  | func Overload(filenames ...string) (err error) { | ||||||
|  | 	filenames = filenamesOrDefault(filenames) | ||||||
|  |  | ||||||
|  | 	for _, filename := range filenames { | ||||||
|  | 		err = loadFile(filename, true) | ||||||
|  | 		if err != nil { | ||||||
|  | 			return // return early on a spazout | ||||||
|  | 		} | ||||||
|  | 	} | ||||||
|  | 	return | ||||||
|  | } | ||||||
|  |  | ||||||
|  | // Read all env (with same file loading semantics as Load) but return values as | ||||||
|  | // a map rather than automatically writing values into env | ||||||
| func Read(filenames ...string) (envMap map[string]string, err error) { | func Read(filenames ...string) (envMap map[string]string, err error) { | ||||||
| 	filenames = filenamesOrDefault(filenames) | 	filenames = filenamesOrDefault(filenames) | ||||||
| 	envMap = make(map[string]string) | 	envMap = make(map[string]string) | ||||||
| @ -70,15 +89,13 @@ func Read(filenames ...string) (envMap map[string]string, err error) { | |||||||
| 	return | 	return | ||||||
| } | } | ||||||
|  |  | ||||||
| /* | // Exec loads env vars from the specified filenames (empty map falls back to default) | ||||||
|   Loads env vars from the specified filenames (empty map falls back to default) | // then executes the cmd specified. | ||||||
|   then executes the cmd specified. | // | ||||||
|  | // Simply hooks up os.Stdin/err/out to the command and calls Run() | ||||||
|   Simply hooks up os.Stdin/err/out to the command and calls Run() | // | ||||||
|  | // If you want more fine grained control over your command it's recommended | ||||||
|   If you want more fine grained control over your command it's recommended | // that you use `Load()` or `Read()` and the `os/exec` package yourself. | ||||||
|   that you use `Load()` or `Read()` and the `os/exec` package yourself. |  | ||||||
| */ |  | ||||||
| func Exec(filenames []string, cmd string, cmdArgs []string) error { | func Exec(filenames []string, cmd string, cmdArgs []string) error { | ||||||
| 	Load(filenames...) | 	Load(filenames...) | ||||||
|  |  | ||||||
| @ -92,22 +109,23 @@ func Exec(filenames []string, cmd string, cmdArgs []string) error { | |||||||
| func filenamesOrDefault(filenames []string) []string { | func filenamesOrDefault(filenames []string) []string { | ||||||
| 	if len(filenames) == 0 { | 	if len(filenames) == 0 { | ||||||
| 		return []string{".env"} | 		return []string{".env"} | ||||||
| 	} else { |  | ||||||
| 		return filenames |  | ||||||
| 	} | 	} | ||||||
|  | 	return filenames | ||||||
| } | } | ||||||
|  |  | ||||||
| func loadFile(filename string) (err error) { | func loadFile(filename string, overload bool) error { | ||||||
| 	envMap, err := readFile(filename) | 	envMap, err := readFile(filename) | ||||||
| 	if err != nil { | 	if err != nil { | ||||||
| 		return | 		return err | ||||||
| 	} | 	} | ||||||
|  |  | ||||||
| 	for key, value := range envMap { | 	for key, value := range envMap { | ||||||
| 		os.Setenv(key, value) | 		if os.Getenv(key) == "" || overload { | ||||||
|  | 			os.Setenv(key, value) | ||||||
|  | 		} | ||||||
| 	} | 	} | ||||||
|  |  | ||||||
| 	return | 	return nil | ||||||
| } | } | ||||||
|  |  | ||||||
| func readFile(filename string) (envMap map[string]string, err error) { | func readFile(filename string) (envMap map[string]string, err error) { | ||||||
| @ -125,13 +143,19 @@ func readFile(filename string) (envMap map[string]string, err error) { | |||||||
| 		lines = append(lines, scanner.Text()) | 		lines = append(lines, scanner.Text()) | ||||||
| 	} | 	} | ||||||
|  |  | ||||||
|  | 	if err = scanner.Err(); err != nil { | ||||||
|  | 		return | ||||||
|  | 	} | ||||||
|  |  | ||||||
| 	for _, fullLine := range lines { | 	for _, fullLine := range lines { | ||||||
| 		if !isIgnoredLine(fullLine) { | 		if !isIgnoredLine(fullLine) { | ||||||
| 			key, value, err := parseLine(fullLine) | 			var key, value string | ||||||
|  | 			key, value, err = parseLine(fullLine) | ||||||
|  |  | ||||||
| 			if err == nil && os.Getenv(key) == "" { | 			if err != nil { | ||||||
| 				envMap[key] = value | 				return | ||||||
| 			} | 			} | ||||||
|  | 			envMap[key] = value | ||||||
| 		} | 		} | ||||||
| 	} | 	} | ||||||
| 	return | 	return | ||||||
| @ -147,7 +171,7 @@ func parseLine(line string) (key string, value string, err error) { | |||||||
| 	if strings.Contains(line, "#") { | 	if strings.Contains(line, "#") { | ||||||
| 		segmentsBetweenHashes := strings.Split(line, "#") | 		segmentsBetweenHashes := strings.Split(line, "#") | ||||||
| 		quotesAreOpen := false | 		quotesAreOpen := false | ||||||
| 		segmentsToKeep := make([]string, 0) | 		var segmentsToKeep []string | ||||||
| 		for _, segment := range segmentsBetweenHashes { | 		for _, segment := range segmentsBetweenHashes { | ||||||
| 			if strings.Count(segment, "\"") == 1 || strings.Count(segment, "'") == 1 { | 			if strings.Count(segment, "\"") == 1 || strings.Count(segment, "'") == 1 { | ||||||
| 				if quotesAreOpen { | 				if quotesAreOpen { | ||||||
|  | |||||||
| @ -5,6 +5,8 @@ import ( | |||||||
| 	"testing" | 	"testing" | ||||||
| ) | ) | ||||||
|  |  | ||||||
|  | var noopPresets = make(map[string]string) | ||||||
|  |  | ||||||
| func parseAndCompare(t *testing.T, rawEnvLine string, expectedKey string, expectedValue string) { | func parseAndCompare(t *testing.T, rawEnvLine string, expectedKey string, expectedValue string) { | ||||||
| 	key, value, _ := parseLine(rawEnvLine) | 	key, value, _ := parseLine(rawEnvLine) | ||||||
| 	if key != expectedKey || value != expectedValue { | 	if key != expectedKey || value != expectedValue { | ||||||
| @ -12,11 +14,15 @@ func parseAndCompare(t *testing.T, rawEnvLine string, expectedKey string, expect | |||||||
| 	} | 	} | ||||||
| } | } | ||||||
|  |  | ||||||
| func loadEnvAndCompareValues(t *testing.T, envFileName string, expectedValues map[string]string) { | func loadEnvAndCompareValues(t *testing.T, loader func(files ...string) error, envFileName string, expectedValues map[string]string, presets map[string]string) { | ||||||
| 	// first up, clear the env | 	// first up, clear the env | ||||||
| 	os.Clearenv() | 	os.Clearenv() | ||||||
|  |  | ||||||
| 	err := Load(envFileName) | 	for k, v := range presets { | ||||||
|  | 		os.Setenv(k, v) | ||||||
|  | 	} | ||||||
|  |  | ||||||
|  | 	err := loader(envFileName) | ||||||
| 	if err != nil { | 	if err != nil { | ||||||
| 		t.Fatalf("Error loading %v", envFileName) | 		t.Fatalf("Error loading %v", envFileName) | ||||||
| 	} | 	} | ||||||
| @ -38,6 +44,14 @@ func TestLoadWithNoArgsLoadsDotEnv(t *testing.T) { | |||||||
| 	} | 	} | ||||||
| } | } | ||||||
|  |  | ||||||
|  | func TestOverloadWithNoArgsOverloadsDotEnv(t *testing.T) { | ||||||
|  | 	err := Overload() | ||||||
|  | 	pathError := err.(*os.PathError) | ||||||
|  | 	if pathError == nil || pathError.Op != "open" || pathError.Path != ".env" { | ||||||
|  | 		t.Errorf("Didn't try and open .env by default") | ||||||
|  | 	} | ||||||
|  | } | ||||||
|  |  | ||||||
| func TestLoadFileNotFound(t *testing.T) { | func TestLoadFileNotFound(t *testing.T) { | ||||||
| 	err := Load("somefilethatwillneverexistever.env") | 	err := Load("somefilethatwillneverexistever.env") | ||||||
| 	if err == nil { | 	if err == nil { | ||||||
| @ -45,6 +59,13 @@ func TestLoadFileNotFound(t *testing.T) { | |||||||
| 	} | 	} | ||||||
| } | } | ||||||
|  |  | ||||||
|  | func TestOverloadFileNotFound(t *testing.T) { | ||||||
|  | 	err := Overload("somefilethatwillneverexistever.env") | ||||||
|  | 	if err == nil { | ||||||
|  | 		t.Error("File wasn't found but Overload didn't return an error") | ||||||
|  | 	} | ||||||
|  | } | ||||||
|  |  | ||||||
| func TestReadPlainEnv(t *testing.T) { | func TestReadPlainEnv(t *testing.T) { | ||||||
| 	envFileName := "fixtures/plain.env" | 	envFileName := "fixtures/plain.env" | ||||||
| 	expectedValues := map[string]string{ | 	expectedValues := map[string]string{ | ||||||
| @ -71,6 +92,34 @@ func TestReadPlainEnv(t *testing.T) { | |||||||
| 	} | 	} | ||||||
| } | } | ||||||
|  |  | ||||||
|  | func TestLoadDoesNotOverride(t *testing.T) { | ||||||
|  | 	envFileName := "fixtures/plain.env" | ||||||
|  |  | ||||||
|  | 	// ensure NO overload | ||||||
|  | 	presets := map[string]string{ | ||||||
|  | 		"OPTION_A": "do_not_override", | ||||||
|  | 	} | ||||||
|  |  | ||||||
|  | 	expectedValues := map[string]string{ | ||||||
|  | 		"OPTION_A": "do_not_override", | ||||||
|  | 	} | ||||||
|  | 	loadEnvAndCompareValues(t, Load, envFileName, expectedValues, presets) | ||||||
|  | } | ||||||
|  |  | ||||||
|  | func TestOveroadDoesOverride(t *testing.T) { | ||||||
|  | 	envFileName := "fixtures/plain.env" | ||||||
|  |  | ||||||
|  | 	// ensure NO overload | ||||||
|  | 	presets := map[string]string{ | ||||||
|  | 		"OPTION_A": "do_not_override", | ||||||
|  | 	} | ||||||
|  |  | ||||||
|  | 	expectedValues := map[string]string{ | ||||||
|  | 		"OPTION_A": "1", | ||||||
|  | 	} | ||||||
|  | 	loadEnvAndCompareValues(t, Overload, envFileName, expectedValues, presets) | ||||||
|  | } | ||||||
|  |  | ||||||
| func TestLoadPlainEnv(t *testing.T) { | func TestLoadPlainEnv(t *testing.T) { | ||||||
| 	envFileName := "fixtures/plain.env" | 	envFileName := "fixtures/plain.env" | ||||||
| 	expectedValues := map[string]string{ | 	expectedValues := map[string]string{ | ||||||
| @ -81,7 +130,7 @@ func TestLoadPlainEnv(t *testing.T) { | |||||||
| 		"OPTION_E": "5", | 		"OPTION_E": "5", | ||||||
| 	} | 	} | ||||||
|  |  | ||||||
| 	loadEnvAndCompareValues(t, envFileName, expectedValues) | 	loadEnvAndCompareValues(t, Load, envFileName, expectedValues, noopPresets) | ||||||
| } | } | ||||||
|  |  | ||||||
| func TestLoadExportedEnv(t *testing.T) { | func TestLoadExportedEnv(t *testing.T) { | ||||||
| @ -91,7 +140,7 @@ func TestLoadExportedEnv(t *testing.T) { | |||||||
| 		"OPTION_B": "\n", | 		"OPTION_B": "\n", | ||||||
| 	} | 	} | ||||||
|  |  | ||||||
| 	loadEnvAndCompareValues(t, envFileName, expectedValues) | 	loadEnvAndCompareValues(t, Load, envFileName, expectedValues, noopPresets) | ||||||
| } | } | ||||||
|  |  | ||||||
| func TestLoadEqualsEnv(t *testing.T) { | func TestLoadEqualsEnv(t *testing.T) { | ||||||
| @ -100,7 +149,7 @@ func TestLoadEqualsEnv(t *testing.T) { | |||||||
| 		"OPTION_A": "postgres://localhost:5432/database?sslmode=disable", | 		"OPTION_A": "postgres://localhost:5432/database?sslmode=disable", | ||||||
| 	} | 	} | ||||||
|  |  | ||||||
| 	loadEnvAndCompareValues(t, envFileName, expectedValues) | 	loadEnvAndCompareValues(t, Load, envFileName, expectedValues, noopPresets) | ||||||
| } | } | ||||||
|  |  | ||||||
| func TestLoadQuotedEnv(t *testing.T) { | func TestLoadQuotedEnv(t *testing.T) { | ||||||
| @ -116,7 +165,7 @@ func TestLoadQuotedEnv(t *testing.T) { | |||||||
| 		"OPTION_H": "\n", | 		"OPTION_H": "\n", | ||||||
| 	} | 	} | ||||||
|  |  | ||||||
| 	loadEnvAndCompareValues(t, envFileName, expectedValues) | 	loadEnvAndCompareValues(t, Load, envFileName, expectedValues, noopPresets) | ||||||
| } | } | ||||||
|  |  | ||||||
| func TestActualEnvVarsAreLeftAlone(t *testing.T) { | func TestActualEnvVarsAreLeftAlone(t *testing.T) { | ||||||
| @ -220,3 +269,20 @@ func TestLinesToIgnore(t *testing.T) { | |||||||
| 		t.Error("ignoring a perfectly valid line to parse") | 		t.Error("ignoring a perfectly valid line to parse") | ||||||
| 	} | 	} | ||||||
| } | } | ||||||
|  |  | ||||||
|  | func TestErrorReadDirectory(t *testing.T) { | ||||||
|  | 	envFileName := "fixtures/" | ||||||
|  | 	envMap, err := Read(envFileName) | ||||||
|  |  | ||||||
|  | 	if err == nil { | ||||||
|  | 		t.Errorf("Expected error, got %v", envMap) | ||||||
|  | 	} | ||||||
|  | } | ||||||
|  |  | ||||||
|  | func TestErrorParsing(t *testing.T) { | ||||||
|  | 	envFileName := "fixtures/invalid1.env" | ||||||
|  | 	envMap, err := Read(envFileName) | ||||||
|  | 	if err == nil { | ||||||
|  | 		t.Errorf("Expected error, got %v", envMap) | ||||||
|  | 	} | ||||||
|  | } | ||||||
|  | |||||||
| @ -1 +0,0 @@ | |||||||
| box: pjvds/golang |  | ||||||
		Reference in New Issue
	
	Block a user
	