Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add overload flag #200

Merged
merged 2 commits into from Feb 4, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
2 changes: 2 additions & 0 deletions README.md
Expand Up @@ -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`

By default, it won't override existing environment variables; you can do that with the `-o` flag.

### Writing Env Files

Godotenv can also write a map representing the environment to a correctly-formatted and escaped file
Expand Down
6 changes: 4 additions & 2 deletions cmd/godotenv/cmd.go
Expand Up @@ -15,13 +15,15 @@ func main() {
flag.BoolVar(&showHelp, "h", false, "show help")
var rawEnvFilenames string
flag.StringVar(&rawEnvFilenames, "f", "", "comma separated paths to .env files")
var overload bool
flag.BoolVar(&overload, "o", false, "override existing .env variables")

flag.Parse()

usage := `
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
COMMAND_ARGS: command and args you want to run
Expand All @@ -47,7 +49,7 @@ example
cmd := args[0]
cmdArgs := args[1:]

err := godotenv.Exec(envFilenames, cmd, cmdArgs)
err := godotenv.Exec(envFilenames, cmd, cmdArgs, overload)
if err != nil {
log.Fatal(err)
}
Expand Down
10 changes: 7 additions & 3 deletions godotenv.go
Expand Up @@ -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().
//
// 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.
func Exec(filenames []string, cmd string, cmdArgs []string) error {
if err := Load(filenames...); err != nil {
// that you use `Load()`, `Overload()` or `Read()` and the `os/exec` package yourself.
func Exec(filenames []string, cmd string, cmdArgs []string, overload bool) error {
op := Load
if overload {
op = Overload
}
if err := op(filenames...); err != nil {
return err
}

Expand Down