forked from coop-cloud-mirrors/godotenv
Compare commits
6 Commits
multiline-
...
v1.5.0
Author | SHA1 | Date | |
---|---|---|---|
b311b2657d | |||
4321598b05 | |||
32a3b9b960 | |||
06bf2d6190 | |||
cc9e9b7de7 | |||
0f21d20acb |
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.19', '1.18', '1.17', '1.16', '1.15' ]
|
go: [ '1.20', '1.19', '1.18', '1.17', '1.16' ]
|
||||||
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,6 +153,8 @@ 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,13 +15,15 @@ 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 [-f ENV_FILE_PATHS] COMMAND_ARGS
|
godotenv [-o] [-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
|
||||||
@ -47,7 +49,7 @@ example
|
|||||||
cmd := args[0]
|
cmd := args[0]
|
||||||
cmdArgs := args[1:]
|
cmdArgs := args[1:]
|
||||||
|
|
||||||
err := godotenv.Exec(envFilenames, cmd, cmdArgs)
|
err := godotenv.Exec(envFilenames, cmd, cmdArgs, overload)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Fatal(err)
|
log.Fatal(err)
|
||||||
}
|
}
|
||||||
|
@ -1,2 +1 @@
|
|||||||
export OPTION_A='postgres://localhost:5432/database?sslmode=disable'
|
export OPTION_A='postgres://localhost:5432/database?sslmode=disable'
|
||||||
|
|
||||||
|
17
godotenv.go
17
godotenv.go
@ -14,10 +14,10 @@
|
|||||||
package godotenv
|
package godotenv
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"bytes"
|
||||||
"errors"
|
"errors"
|
||||||
"fmt"
|
"fmt"
|
||||||
"io"
|
"io"
|
||||||
"io/ioutil"
|
|
||||||
"os"
|
"os"
|
||||||
"os/exec"
|
"os/exec"
|
||||||
"regexp"
|
"regexp"
|
||||||
@ -30,12 +30,13 @@ 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) {
|
||||||
data, err := ioutil.ReadAll(r)
|
var buf bytes.Buffer
|
||||||
|
_, err := io.Copy(&buf, r)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
|
|
||||||
return UnmarshalBytes(data)
|
return UnmarshalBytes(buf.Bytes())
|
||||||
}
|
}
|
||||||
|
|
||||||
// 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.
|
||||||
@ -125,9 +126,13 @@ 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()` or `Read()` and the `os/exec` package yourself.
|
// that you use `Load()`, `Overload()` 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, overload bool) error {
|
||||||
if err := Load(filenames...); err != nil {
|
op := Load
|
||||||
|
if overload {
|
||||||
|
op = Overload
|
||||||
|
}
|
||||||
|
if err := op(filenames...); err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Reference in New Issue
Block a user