Compare commits

...

4 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
4 changed files with 22 additions and 13 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

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

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