Compare commits

..

6 Commits

Author SHA1 Message Date
b311b2657d Fix: ioutil.ReadAll() is deprecated, so removed it's dependency (#202) 2023-02-04 11:10:05 +11:00
4321598b05 add overload flag (#200)
* add -o flag

increases compatibility with the ruby command

* update README
2023-02-04 11:00:21 +11:00
32a3b9b960 fix whitespace with gofmt (#203) 2023-02-04 10:58:06 +11:00
06bf2d6190 Update CI to test go 1.20 (#201) 2023-02-02 12:05:32 +11:00
cc9e9b7de7 Multiline string support (#156)
* refactor dotenv parser in order to support multi-line variable values declaration

Signed-off-by: x1unix <denis0051@gmail.com>

* Add multi-line var values test case and update comment test

Signed-off-by: x1unix <denis0051@gmail.com>

* Expand fixture tests to include multiline strings

* Update go versions to test against

* Switch to GOINSECURE for power8 CI task

* When tests fail, show source version of string (inc special chars)

* Update parser.go

Co-authored-by: Austin Sasko <austintyler0239@yahoo.com>

* Fix up bad merge

* Add a full fixture for comments for extra piece of mind

* Fix up some lint/staticcheck recommendations

* Test against go 1.19 too

Signed-off-by: x1unix <denis0051@gmail.com>
Co-authored-by: x1unix <denis0051@gmail.com>
Co-authored-by: Austin Sasko <austintyler0239@yahoo.com>
2023-01-27 13:14:16 +11:00
0f21d20acb fix tiny details (#199)
* remove empty line

* remove unnecessary assignments

following commit 2ed25fcb28.
2023-01-27 13:01:43 +11:00
5 changed files with 24 additions and 16 deletions

View File

@ -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:

View File

@ -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

View 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)
} }

View File

@ -1,2 +1 @@
export OPTION_A='postgres://localhost:5432/database?sslmode=disable' export OPTION_A='postgres://localhost:5432/database?sslmode=disable'

View File

@ -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
} }