forked from coop-cloud-mirrors/godotenv
		
	Compare commits
	
		
			12 Commits
		
	
	
		
			v1.5.0
			...
			multiline-
		
	
	| Author | SHA1 | Date | |
|---|---|---|---|
| 1e1ed32207 | |||
| d4cc462af7 | |||
| 02a6516148 | |||
| 08d75deb23 | |||
| c723940cb5 | |||
| 687a37d9c9 | |||
| a4061f306a | |||
| 22f9782344 | |||
| 2f66a86dc9 | |||
| 77e8b7d7d6 | |||
| dd57617d18 | |||
| 15b4dd39bb | 
							
								
								
									
										2
									
								
								.github/workflows/ci.yml
									
									
									
									
										vendored
									
									
								
							
							
						
						
									
										2
									
								
								.github/workflows/ci.yml
									
									
									
									
										vendored
									
									
								
							| @ -8,7 +8,7 @@ jobs: | |||||||
|     strategy: |     strategy: | ||||||
|       fail-fast: false |       fail-fast: false | ||||||
|       matrix: |       matrix: | ||||||
|         go: [ '1.20', '1.19', '1.18', '1.17', '1.16' ] |         go: [ '1.19', '1.18', '1.17', '1.16', '1.15' ] | ||||||
|         os: [ ubuntu-latest, macOS-latest, windows-latest ] |         os: [ ubuntu-latest, macOS-latest, windows-latest ] | ||||||
|     name: ${{ matrix.os }} Go ${{ matrix.go }} Tests |     name: ${{ matrix.os }} Go ${{ matrix.go }} Tests | ||||||
|     steps: |     steps: | ||||||
|  | |||||||
| @ -75,8 +75,8 @@ import _ "github.com/joho/godotenv/autoload" | |||||||
| While `.env` in the project root is the default, you don't have to be constrained, both examples below are 100% legit | While `.env` in the project root is the default, you don't have to be constrained, both examples below are 100% legit | ||||||
|  |  | ||||||
| ```go | ```go | ||||||
| godotenv.Load("somerandomfile") | _ = godotenv.Load("somerandomfile") | ||||||
| godotenv.Load("filenumberone.env", "filenumbertwo.env") | _ = godotenv.Load("filenumberone.env", "filenumbertwo.env") | ||||||
| ``` | ``` | ||||||
|  |  | ||||||
| If you want to be really fancy with your env file you can do comments and exports (below is a valid env file) | If you want to be really fancy with your env file you can do comments and exports (below is a valid env file) | ||||||
| @ -153,8 +153,6 @@ godotenv -f /some/path/to/.env some_command with some args | |||||||
|  |  | ||||||
| If you don't specify `-f` it will fall back on the default of loading `.env` in `PWD` | If you don't specify `-f` it will fall back on the default of loading `.env` in `PWD` | ||||||
|  |  | ||||||
| By default, it won't override existing environment variables; you can do that with the `-o` flag. |  | ||||||
|  |  | ||||||
| ### Writing Env Files | ### Writing Env Files | ||||||
|  |  | ||||||
| Godotenv can also write a map representing the environment to a correctly-formatted and escaped file | Godotenv can also write a map representing the environment to a correctly-formatted and escaped file | ||||||
|  | |||||||
| @ -15,15 +15,13 @@ func main() { | |||||||
| 	flag.BoolVar(&showHelp, "h", false, "show help") | 	flag.BoolVar(&showHelp, "h", false, "show help") | ||||||
| 	var rawEnvFilenames string | 	var rawEnvFilenames string | ||||||
| 	flag.StringVar(&rawEnvFilenames, "f", "", "comma separated paths to .env files") | 	flag.StringVar(&rawEnvFilenames, "f", "", "comma separated paths to .env files") | ||||||
| 	var overload bool |  | ||||||
| 	flag.BoolVar(&overload, "o", false, "override existing .env variables") |  | ||||||
|  |  | ||||||
| 	flag.Parse() | 	flag.Parse() | ||||||
|  |  | ||||||
| 	usage := ` | 	usage := ` | ||||||
| Run a process with an env setup from a .env file | Run a process with an env setup from a .env file | ||||||
|  |  | ||||||
| godotenv [-o] [-f ENV_FILE_PATHS] COMMAND_ARGS | godotenv [-f ENV_FILE_PATHS] COMMAND_ARGS | ||||||
|  |  | ||||||
| ENV_FILE_PATHS: comma separated paths to .env files | ENV_FILE_PATHS: comma separated paths to .env files | ||||||
| COMMAND_ARGS: command and args you want to run | COMMAND_ARGS: command and args you want to run | ||||||
| @ -49,7 +47,7 @@ example | |||||||
| 	cmd := args[0] | 	cmd := args[0] | ||||||
| 	cmdArgs := args[1:] | 	cmdArgs := args[1:] | ||||||
|  |  | ||||||
| 	err := godotenv.Exec(envFilenames, cmd, cmdArgs, overload) | 	err := godotenv.Exec(envFilenames, cmd, cmdArgs) | ||||||
| 	if err != nil { | 	if err != nil { | ||||||
| 		log.Fatal(err) | 		log.Fatal(err) | ||||||
| 	} | 	} | ||||||
|  | |||||||
| @ -1 +1,2 @@ | |||||||
| export OPTION_A='postgres://localhost:5432/database?sslmode=disable' | export OPTION_A='postgres://localhost:5432/database?sslmode=disable' | ||||||
|  |  | ||||||
|  | |||||||
							
								
								
									
										25
									
								
								godotenv.go
									
									
									
									
									
								
							
							
						
						
									
										25
									
								
								godotenv.go
									
									
									
									
									
								
							| @ -4,20 +4,20 @@ | |||||||
| // | // | ||||||
| // 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 available through os.Getenv("SOME_ENV_VAR") | // and all the env vars declared in .env will be available through os.Getenv("SOME_ENV_VAR") | ||||||
| package godotenv | package godotenv | ||||||
|  |  | ||||||
| import ( | import ( | ||||||
| 	"bytes" |  | ||||||
| 	"errors" | 	"errors" | ||||||
| 	"fmt" | 	"fmt" | ||||||
| 	"io" | 	"io" | ||||||
|  | 	"io/ioutil" | ||||||
| 	"os" | 	"os" | ||||||
| 	"os/exec" | 	"os/exec" | ||||||
| 	"regexp" | 	"regexp" | ||||||
| @ -30,13 +30,12 @@ const doubleQuoteSpecialChars = "\\\n\r\"!$`" | |||||||
|  |  | ||||||
| // Parse reads an env file from io.Reader, returning a map of keys and values. | // Parse reads an env file from io.Reader, returning a map of keys and values. | ||||||
| func Parse(r io.Reader) (map[string]string, error) { | func Parse(r io.Reader) (map[string]string, error) { | ||||||
| 	var buf bytes.Buffer | 	data, err := ioutil.ReadAll(r) | ||||||
| 	_, err := io.Copy(&buf, r) |  | ||||||
| 	if err != nil { | 	if err != nil { | ||||||
| 		return nil, err | 		return nil, err | ||||||
| 	} | 	} | ||||||
|  |  | ||||||
| 	return UnmarshalBytes(buf.Bytes()) | 	return UnmarshalBytes(data) | ||||||
| } | } | ||||||
|  |  | ||||||
| // Load will read your env file(s) and load them into ENV for this process. | // Load will read your env file(s) and load them into ENV for this process. | ||||||
| @ -47,7 +46,7 @@ func Parse(r io.Reader) (map[string]string, error) { | |||||||
| // | // | ||||||
| // 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) { | ||||||
| @ -70,7 +69,7 @@ func Load(filenames ...string) (err error) { | |||||||
| // | // | ||||||
| // 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.Overload("fileone", "filetwo") | //		godotenv.Overload("fileone", "filetwo") | ||||||
| // | // | ||||||
| // It's important to note this WILL OVERRIDE an env variable that already exists - consider the .env file to forcefully set all vars. | // It's important to note this WILL OVERRIDE an env variable that already exists - consider the .env file to forcefully set all vars. | ||||||
| func Overload(filenames ...string) (err error) { | func Overload(filenames ...string) (err error) { | ||||||
| @ -126,13 +125,9 @@ func UnmarshalBytes(src []byte) (map[string]string, error) { | |||||||
| // 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()`, `Overload()` 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, overload bool) error { | func Exec(filenames []string, cmd string, cmdArgs []string) error { | ||||||
| 	op := Load | 	if err := Load(filenames...); err != nil { | ||||||
| 	if overload { |  | ||||||
| 		op = Overload |  | ||||||
| 	} |  | ||||||
| 	if err := op(filenames...); err != nil { |  | ||||||
| 		return err | 		return err | ||||||
| 	} | 	} | ||||||
|  |  | ||||||
|  | |||||||
		Reference in New Issue
	
	Block a user
	