Skip to content

Commit 4321598

Browse files
authored
add overload flag (#200)
* add -o flag increases compatibility with the ruby command * update README
1 parent 32a3b9b commit 4321598

File tree

3 files changed

+13
-5
lines changed

3 files changed

+13
-5
lines changed

README.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -153,6 +153,8 @@ godotenv -f /some/path/to/.env some_command with some args
153153

154154
If you don't specify `-f` it will fall back on the default of loading `.env` in `PWD`
155155

156+
By default, it won't override existing environment variables; you can do that with the `-o` flag.
157+
156158
### Writing Env Files
157159

158160
Godotenv can also write a map representing the environment to a correctly-formatted and escaped file

cmd/godotenv/cmd.go

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,13 +15,15 @@ func main() {
1515
flag.BoolVar(&showHelp, "h", false, "show help")
1616
var rawEnvFilenames string
1717
flag.StringVar(&rawEnvFilenames, "f", "", "comma separated paths to .env files")
18+
var overload bool
19+
flag.BoolVar(&overload, "o", false, "override existing .env variables")
1820

1921
flag.Parse()
2022

2123
usage := `
2224
Run a process with an env setup from a .env file
2325
24-
godotenv [-f ENV_FILE_PATHS] COMMAND_ARGS
26+
godotenv [-o] [-f ENV_FILE_PATHS] COMMAND_ARGS
2527
2628
ENV_FILE_PATHS: comma separated paths to .env files
2729
COMMAND_ARGS: command and args you want to run
@@ -47,7 +49,7 @@ example
4749
cmd := args[0]
4850
cmdArgs := args[1:]
4951

50-
err := godotenv.Exec(envFilenames, cmd, cmdArgs)
52+
err := godotenv.Exec(envFilenames, cmd, cmdArgs, overload)
5153
if err != nil {
5254
log.Fatal(err)
5355
}

godotenv.go

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -125,9 +125,13 @@ func UnmarshalBytes(src []byte) (map[string]string, error) {
125125
// Simply hooks up os.Stdin/err/out to the command and calls Run().
126126
//
127127
// If you want more fine grained control over your command it's recommended
128-
// that you use `Load()` or `Read()` and the `os/exec` package yourself.
129-
func Exec(filenames []string, cmd string, cmdArgs []string) error {
130-
if err := Load(filenames...); err != nil {
128+
// that you use `Load()`, `Overload()` or `Read()` and the `os/exec` package yourself.
129+
func Exec(filenames []string, cmd string, cmdArgs []string, overload bool) error {
130+
op := Load
131+
if overload {
132+
op = Overload
133+
}
134+
if err := op(filenames...); err != nil {
131135
return err
132136
}
133137

0 commit comments

Comments
 (0)