forked from coop-cloud-mirrors/godotenv
Compare commits
4 Commits
1.5.0-beta
...
v1.5.0
Author | SHA1 | Date | |
---|---|---|---|
b311b2657d | |||
4321598b05 | |||
32a3b9b960 | |||
06bf2d6190 |
2
.github/workflows/ci.yml
vendored
2
.github/workflows/ci.yml
vendored
@ -8,7 +8,7 @@ jobs:
|
||||
strategy:
|
||||
fail-fast: false
|
||||
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 ]
|
||||
name: ${{ matrix.os }} Go ${{ matrix.go }} Tests
|
||||
steps:
|
||||
|
@ -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
|
||||
|
@ -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
|
||||
@ -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)
|
||||
}
|
||||
|
25
godotenv.go
25
godotenv.go
@ -4,20 +4,20 @@
|
||||
//
|
||||
// The TL;DR is that you make a .env file that looks something like
|
||||
//
|
||||
// SOME_ENV_VAR=somevalue
|
||||
// SOME_ENV_VAR=somevalue
|
||||
//
|
||||
// and then in your go code you can call
|
||||
//
|
||||
// godotenv.Load()
|
||||
// godotenv.Load()
|
||||
//
|
||||
// and all the env vars declared in .env will be available through os.Getenv("SOME_ENV_VAR")
|
||||
package godotenv
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"errors"
|
||||
"fmt"
|
||||
"io"
|
||||
"io/ioutil"
|
||||
"os"
|
||||
"os/exec"
|
||||
"regexp"
|
||||
@ -30,12 +30,13 @@ const doubleQuoteSpecialChars = "\\\n\r\"!$`"
|
||||
|
||||
// Parse reads an env file from io.Reader, returning a map of keys and values.
|
||||
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 {
|
||||
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.
|
||||
@ -46,7 +47,7 @@ func Parse(r io.Reader) (map[string]string, error) {
|
||||
//
|
||||
// You can otherwise tell it which files to load (there can be more than one) like:
|
||||
//
|
||||
// godotenv.Load("fileone", "filetwo")
|
||||
// godotenv.Load("fileone", "filetwo")
|
||||
//
|
||||
// It's important to note that it WILL NOT OVERRIDE an env variable that already exists - consider the .env file to set dev vars or sensible defaults.
|
||||
func Load(filenames ...string) (err error) {
|
||||
@ -69,7 +70,7 @@ func Load(filenames ...string) (err error) {
|
||||
//
|
||||
// You can otherwise tell it which files to load (there can be more than one) like:
|
||||
//
|
||||
// godotenv.Overload("fileone", "filetwo")
|
||||
// godotenv.Overload("fileone", "filetwo")
|
||||
//
|
||||
// It's important to note this WILL OVERRIDE an env variable that already exists - consider the .env file to forcefully set all vars.
|
||||
func Overload(filenames ...string) (err error) {
|
||||
@ -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().
|
||||
//
|
||||
// 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
|
||||
}
|
||||
|
||||
|
Reference in New Issue
Block a user