mirror of
https://github.com/joho/godotenv.git
synced 2025-07-17 05:54:03 +00:00
Merge c6e8ca9915
into cc9e9b7de7
This commit is contained in:
@ -151,7 +151,8 @@ Assuming you've installed the command as above and you've got `$GOPATH/bin` in y
|
|||||||
godotenv -f /some/path/to/.env some_command with some args
|
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`.
|
||||||
|
Optional flag `-s` removes all the loaded files if the command fails and returns the same exit code.
|
||||||
|
|
||||||
### Writing Env Files
|
### Writing Env Files
|
||||||
|
|
||||||
|
@ -4,28 +4,32 @@ import (
|
|||||||
"flag"
|
"flag"
|
||||||
"fmt"
|
"fmt"
|
||||||
"log"
|
"log"
|
||||||
|
"os"
|
||||||
|
"os/exec"
|
||||||
"strings"
|
"strings"
|
||||||
|
|
||||||
"github.com/joho/godotenv"
|
"github.com/joho/godotenv"
|
||||||
)
|
)
|
||||||
|
|
||||||
func main() {
|
func main() {
|
||||||
var showHelp bool
|
var showHelp, deleteFileOnError bool
|
||||||
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")
|
||||||
|
flag.BoolVar(&deleteFileOnError, "s", false, "delete .env files if the command fails, copy exit status")
|
||||||
|
|
||||||
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 [-f ENV_FILE_PATHS] [-s] COMMAND_ARGS
|
||||||
|
|
||||||
ENV_FILE_PATHS: comma separated paths to .env files
|
ENV_FILE_PATHS: comma separated paths to .env files (.env is the default)
|
||||||
COMMAND_ARGS: command and args you want to run
|
COMMAND_ARGS: command and args you want to run
|
||||||
|
|
||||||
|
-s delete .env files if the command fails, copy exit status
|
||||||
|
|
||||||
example
|
example
|
||||||
godotenv -f /path/to/something/.env,/another/path/.env fortune
|
godotenv -f /path/to/something/.env,/another/path/.env fortune
|
||||||
`
|
`
|
||||||
@ -49,6 +53,20 @@ example
|
|||||||
|
|
||||||
err := godotenv.Exec(envFilenames, cmd, cmdArgs)
|
err := godotenv.Exec(envFilenames, cmd, cmdArgs)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
|
if deleteFileOnError {
|
||||||
|
if len(envFilenames) == 0 {
|
||||||
|
envFilenames = []string{".env"}
|
||||||
|
}
|
||||||
|
for _, filename := range envFilenames {
|
||||||
|
fileErr := os.Remove(filename)
|
||||||
|
if fileErr != nil {
|
||||||
|
log.Println(fileErr)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
if state, ok := err.(*exec.ExitError); ok {
|
||||||
|
os.Exit(state.ExitCode())
|
||||||
|
}
|
||||||
|
}
|
||||||
log.Fatal(err)
|
log.Fatal(err)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
Reference in New Issue
Block a user