forked from coop-cloud-mirrors/godotenv
add overload flag (#200)
* add -o flag increases compatibility with the ruby command * update README
This commit is contained in:
@ -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)
|
||||||
}
|
}
|
||||||
|
10
godotenv.go
10
godotenv.go
@ -125,9 +125,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