[config] upgrade to godotenv 1.2.0
This commit is contained in:
8
plugins/config/glide.lock
generated
8
plugins/config/glide.lock
generated
@@ -1,8 +1,8 @@
|
||||
hash: 84462ba8482aa8c12a928ab1851f212d558df5036311ec22469fdf86b77cbcad
|
||||
updated: 2017-07-25T23:17:54.615885635-04:00
|
||||
hash: dd48defef474bdf5c6e47281a256ca941d71fe1e637d11443fb49acf3e4b5326
|
||||
updated: 2017-09-23T16:25:15.559883959-04:00
|
||||
imports:
|
||||
- name: github.com/alexquick/godotenv
|
||||
version: 84c21eea73c643434a7b128dab043ffea44b80d9
|
||||
- name: github.com/joho/godotenv
|
||||
version: a79fa1e548e2c689c241d10173efd51e5d689d5b
|
||||
- name: github.com/ryanuber/columnize
|
||||
version: abc90934186a77966e2beeac62ed966aac0561d5
|
||||
testImports: []
|
||||
|
||||
@@ -1,8 +1,8 @@
|
||||
package: github.com/dokku/dokku/plugins/config
|
||||
ignore:
|
||||
ignore:
|
||||
- github.com/dokku/dokku/plugins/common
|
||||
- github.com/onsi/gomega
|
||||
import:
|
||||
- package: github.com/ryanuber/columnize
|
||||
- package: github.com/alexquick/godotenv
|
||||
version: ^1.1.0
|
||||
- package: github.com/joho/godotenv
|
||||
version: ^1.2.0
|
||||
|
||||
@@ -12,8 +12,8 @@ import (
|
||||
|
||||
"os"
|
||||
|
||||
godotenv "github.com/alexquick/godotenv"
|
||||
common "github.com/dokku/dokku/plugins/common"
|
||||
godotenv "github.com/joho/godotenv"
|
||||
)
|
||||
|
||||
//ExportFormat types of possible exports
|
||||
@@ -60,7 +60,7 @@ func (e *Env) Export(format ExportFormat) string {
|
||||
|
||||
//EnvfileString returns the contents of this Env in dotenv format
|
||||
func (e *Env) EnvfileString() string {
|
||||
rep, _ := godotenv.WriteString(e.Map())
|
||||
rep, _ := godotenv.Marshal(e.Map())
|
||||
return rep
|
||||
}
|
||||
|
||||
@@ -138,7 +138,7 @@ func LoadGlobal() (*Env, error) {
|
||||
|
||||
//NewFromString creates an env from the given ENVFILE contents representation
|
||||
func NewFromString(rep string) (*Env, error) {
|
||||
envMap, err := godotenv.ReadString(rep)
|
||||
envMap, err := godotenv.Unmarshal(rep)
|
||||
env := &Env{
|
||||
"<unknown>",
|
||||
"",
|
||||
|
||||
@@ -1,4 +1,4 @@
|
||||
# GoDotEnv [](https://travis-ci.org/joho/godotenv) [](https://ci.appveyor.com/project/joho/godotenv)
|
||||
# GoDotEnv [](https://travis-ci.org/joho/godotenv) [](https://ci.appveyor.com/project/joho/godotenv) [](https://goreportcard.com/report/github.com/joho/godotenv)
|
||||
|
||||
A Go (golang) port of the Ruby dotenv project (which loads env vars from a .env file)
|
||||
|
||||
@@ -96,6 +96,20 @@ myEnv, err := godotenv.Read()
|
||||
s3Bucket := myEnv["S3_BUCKET"]
|
||||
```
|
||||
|
||||
... or from an `io.Reader` instead of a local file
|
||||
|
||||
```go
|
||||
reader := getRemoteFile()
|
||||
myEnv, err := godotenv.Parse(reader)
|
||||
```
|
||||
|
||||
... or from a `string` if you so desire
|
||||
|
||||
```go
|
||||
content := getRemoteFileContent()
|
||||
myEnv, err := godotenv.Unmarshal(content)
|
||||
```
|
||||
|
||||
### Command Mode
|
||||
|
||||
Assuming you've installed the command as above and you've got `$GOPATH/bin` in your `$PATH`
|
||||
@@ -106,6 +120,22 @@ 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`
|
||||
|
||||
### Writing Env Files
|
||||
|
||||
Godotenv can also write a map representing the environment to a correctly-formatted and escaped file
|
||||
|
||||
```go
|
||||
env, err := godotenv.Unmarshal("KEY=value")
|
||||
err := godotenv.Write(env, "./.env")
|
||||
```
|
||||
|
||||
... or to a string
|
||||
|
||||
```go
|
||||
env, err := godotenv.Unmarshal("KEY=value")
|
||||
content, err := godotenv.Marshal(env)
|
||||
```
|
||||
|
||||
## Contributing
|
||||
|
||||
Contributions are most welcome! The parser itself is pretty stupidly naive and I wouldn't be surprised if it breaks with edge cases.
|
||||
@@ -10,7 +10,7 @@
|
||||
//
|
||||
// godotenv.Load()
|
||||
//
|
||||
// and all the env vars declared in .env will be avaiable through os.Getenv("SOME_ENV_VAR")
|
||||
// and all the env vars declared in .env will be available through os.Getenv("SOME_ENV_VAR")
|
||||
package godotenv
|
||||
|
||||
import (
|
||||
@@ -95,9 +95,37 @@ func Read(filenames ...string) (envMap map[string]string, err error) {
|
||||
return
|
||||
}
|
||||
|
||||
// ReadString parses an environment file from dotenv file contents returning it as a map
|
||||
func ReadString(content string) (envMap map[string]string, err error) {
|
||||
return read(strings.NewReader(content))
|
||||
// Parse reads an env file from io.Reader, returning a map of keys and values.
|
||||
func Parse(r io.Reader) (envMap map[string]string, err error) {
|
||||
envMap = make(map[string]string)
|
||||
|
||||
var lines []string
|
||||
scanner := bufio.NewScanner(r)
|
||||
for scanner.Scan() {
|
||||
lines = append(lines, scanner.Text())
|
||||
}
|
||||
|
||||
if err = scanner.Err(); err != nil {
|
||||
return
|
||||
}
|
||||
|
||||
for _, fullLine := range lines {
|
||||
if !isIgnoredLine(fullLine) {
|
||||
var key, value string
|
||||
key, value, err = parseLine(fullLine)
|
||||
|
||||
if err != nil {
|
||||
return
|
||||
}
|
||||
envMap[key] = value
|
||||
}
|
||||
}
|
||||
return
|
||||
}
|
||||
|
||||
//Unmarshal reads an env file from a string, returning a map of keys and values.
|
||||
func Unmarshal(str string) (envMap map[string]string, err error) {
|
||||
return Parse(strings.NewReader(str))
|
||||
}
|
||||
|
||||
// Exec loads env vars from the specified filenames (empty map falls back to default)
|
||||
@@ -119,7 +147,7 @@ func Exec(filenames []string, cmd string, cmdArgs []string) error {
|
||||
|
||||
// Write serializes the given environment and writes it to a file
|
||||
func Write(envMap map[string]string, filename string) error {
|
||||
content, error := WriteString(envMap)
|
||||
content, error := Marshal(envMap)
|
||||
if error != nil {
|
||||
return error
|
||||
}
|
||||
@@ -131,10 +159,9 @@ func Write(envMap map[string]string, filename string) error {
|
||||
return err
|
||||
}
|
||||
|
||||
// WriteString outputs the given environment as a dotenv-formatted environment file.
|
||||
//
|
||||
// Marshal outputs the given environment as a dotenv-formatted environment file.
|
||||
// Each line is in the format: KEY="VALUE" where VALUE is backslash-escaped.
|
||||
func WriteString(envMap map[string]string) (string, error) {
|
||||
func Marshal(envMap map[string]string) (string, error) {
|
||||
lines := make([]string, 0, len(envMap))
|
||||
for k, v := range envMap {
|
||||
lines = append(lines, fmt.Sprintf(`%s="%s"`, k, doubleQuoteEscape(v)))
|
||||
@@ -178,34 +205,8 @@ func readFile(filename string) (envMap map[string]string, err error) {
|
||||
return
|
||||
}
|
||||
defer file.Close()
|
||||
return read(file)
|
||||
}
|
||||
|
||||
func read(r io.Reader) (envMap map[string]string, err error) {
|
||||
envMap = make(map[string]string)
|
||||
|
||||
var lines []string
|
||||
scanner := bufio.NewScanner(r)
|
||||
for scanner.Scan() {
|
||||
lines = append(lines, scanner.Text())
|
||||
}
|
||||
|
||||
if err = scanner.Err(); err != nil {
|
||||
return
|
||||
}
|
||||
|
||||
for _, fullLine := range lines {
|
||||
if !isIgnoredLine(fullLine) {
|
||||
var key, value string
|
||||
key, value, err = parseLine(fullLine)
|
||||
|
||||
if err != nil {
|
||||
return
|
||||
}
|
||||
envMap[key] = value
|
||||
}
|
||||
}
|
||||
return
|
||||
return Parse(file)
|
||||
}
|
||||
|
||||
func parseLine(line string) (key string, value string, err error) {
|
||||
@@ -1,6 +1,7 @@
|
||||
package godotenv
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"fmt"
|
||||
"os"
|
||||
"reflect"
|
||||
@@ -96,6 +97,23 @@ func TestReadPlainEnv(t *testing.T) {
|
||||
}
|
||||
}
|
||||
|
||||
func TestParse(t *testing.T) {
|
||||
envMap, err := Parse(bytes.NewReader([]byte("ONE=1\nTWO='2'\nTHREE = \"3\"")))
|
||||
expectedValues := map[string]string{
|
||||
"ONE": "1",
|
||||
"TWO": "2",
|
||||
"THREE": "3",
|
||||
}
|
||||
if err != nil {
|
||||
t.Fatalf("error parsing env: %v", err)
|
||||
}
|
||||
for key, value := range expectedValues {
|
||||
if envMap[key] != value {
|
||||
t.Errorf("expected %s to be %s, got %s", key, value, envMap[key])
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func TestLoadDoesNotOverride(t *testing.T) {
|
||||
envFileName := "fixtures/plain.env"
|
||||
|
||||
@@ -313,8 +331,8 @@ func TestErrorParsing(t *testing.T) {
|
||||
|
||||
func TestWrite(t *testing.T) {
|
||||
writeAndCompare := func(env string, expected string) {
|
||||
envMap, _ := ReadString(env)
|
||||
actual, _ := WriteString(envMap)
|
||||
envMap, _ := Unmarshal(env)
|
||||
actual, _ := Marshal(envMap)
|
||||
if expected != actual {
|
||||
t.Errorf("Expected '%v' (%v) to write as '%v', got '%v' instead.", env, envMap, expected, actual)
|
||||
}
|
||||
@@ -330,23 +348,26 @@ func TestWrite(t *testing.T) {
|
||||
writeAndCompare(`key=va'lu'e`, `key="va'lu'e"`)
|
||||
// newlines, backslashes, and some other special chars are escaped
|
||||
writeAndCompare(`foo="$ba\n\r\\r!"`, `foo="\$ba\n\r\\r\!"`)
|
||||
// lines should be sorted
|
||||
writeAndCompare("foo=bar\nbaz=buzz", "baz=\"buzz\"\nfoo=\"bar\"")
|
||||
|
||||
}
|
||||
|
||||
func TestRoundtrip(t *testing.T) {
|
||||
fixtures := []string{"equals.env", "exported.env", "invalid1.env", "plain.env", "quoted.env"}
|
||||
fixtures := []string{"equals.env", "exported.env", "plain.env", "quoted.env"}
|
||||
for _, fixture := range fixtures {
|
||||
fixtureFilename := fmt.Sprintf("fixtures/%s", fixture)
|
||||
env, err := readFile(fixtureFilename)
|
||||
if err != nil {
|
||||
continue
|
||||
t.Errorf("Expected '%s' to read without error (%v)", fixtureFilename, err)
|
||||
}
|
||||
rep, err := WriteString(env)
|
||||
rep, err := Marshal(env)
|
||||
if err != nil {
|
||||
continue
|
||||
t.Errorf("Expected '%s' to Marshal (%v)", fixtureFilename, err)
|
||||
}
|
||||
roundtripped, err := ReadString(rep)
|
||||
roundtripped, err := Unmarshal(rep)
|
||||
if err != nil {
|
||||
continue
|
||||
t.Errorf("Expected '%s' to Mashal and Unmarshal (%v)", fixtureFilename, err)
|
||||
}
|
||||
if !reflect.DeepEqual(env, roundtripped) {
|
||||
t.Errorf("Expected '%s' to roundtrip as '%v', got '%v' instead", fixtureFilename, env, roundtripped)
|
||||
Reference in New Issue
Block a user