From a86c037a91b58218e76ca9118f82723ad7f9644a Mon Sep 17 00:00:00 2001 From: Alex Quick Date: Sat, 23 Sep 2017 16:27:25 -0400 Subject: [PATCH] [config] upgrade to godotenv 1.2.0 --- plugins/config/glide.lock | 8 +-- plugins/config/glide.yaml | 6 +- plugins/config/src/configenv/configenv.go | 6 +- .../{alexquick => joho}/godotenv/.gitignore | 0 .../{alexquick => joho}/godotenv/.travis.yml | 0 .../{alexquick => joho}/godotenv/LICENCE | 0 .../{alexquick => joho}/godotenv/README.md | 32 ++++++++- .../godotenv/autoload/autoload.go | 0 .../godotenv/cmd/godotenv/cmd.go | 0 .../godotenv/fixtures/equals.env | 0 .../godotenv/fixtures/exported.env | 0 .../godotenv/fixtures/invalid1.env | 0 .../godotenv/fixtures/plain.env | 0 .../godotenv/fixtures/quoted.env | 0 .../{alexquick => joho}/godotenv/godotenv.go | 71 ++++++++++--------- .../godotenv/godotenv_test.go | 37 +++++++--- 16 files changed, 106 insertions(+), 54 deletions(-) rename plugins/config/vendor/github.com/{alexquick => joho}/godotenv/.gitignore (100%) rename plugins/config/vendor/github.com/{alexquick => joho}/godotenv/.travis.yml (100%) rename plugins/config/vendor/github.com/{alexquick => joho}/godotenv/LICENCE (100%) rename plugins/config/vendor/github.com/{alexquick => joho}/godotenv/README.md (85%) rename plugins/config/vendor/github.com/{alexquick => joho}/godotenv/autoload/autoload.go (100%) rename plugins/config/vendor/github.com/{alexquick => joho}/godotenv/cmd/godotenv/cmd.go (100%) rename plugins/config/vendor/github.com/{alexquick => joho}/godotenv/fixtures/equals.env (100%) rename plugins/config/vendor/github.com/{alexquick => joho}/godotenv/fixtures/exported.env (100%) rename plugins/config/vendor/github.com/{alexquick => joho}/godotenv/fixtures/invalid1.env (100%) rename plugins/config/vendor/github.com/{alexquick => joho}/godotenv/fixtures/plain.env (100%) rename plugins/config/vendor/github.com/{alexquick => joho}/godotenv/fixtures/quoted.env (100%) rename plugins/config/vendor/github.com/{alexquick => joho}/godotenv/godotenv.go (92%) rename plugins/config/vendor/github.com/{alexquick => joho}/godotenv/godotenv_test.go (91%) diff --git a/plugins/config/glide.lock b/plugins/config/glide.lock index 025644fb0..eca79f070 100644 --- a/plugins/config/glide.lock +++ b/plugins/config/glide.lock @@ -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: [] diff --git a/plugins/config/glide.yaml b/plugins/config/glide.yaml index 2365d21e1..bf96c156b 100644 --- a/plugins/config/glide.yaml +++ b/plugins/config/glide.yaml @@ -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 diff --git a/plugins/config/src/configenv/configenv.go b/plugins/config/src/configenv/configenv.go index 0b376db9d..e0a8b0282 100644 --- a/plugins/config/src/configenv/configenv.go +++ b/plugins/config/src/configenv/configenv.go @@ -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{ "", "", diff --git a/plugins/config/vendor/github.com/alexquick/godotenv/.gitignore b/plugins/config/vendor/github.com/joho/godotenv/.gitignore similarity index 100% rename from plugins/config/vendor/github.com/alexquick/godotenv/.gitignore rename to plugins/config/vendor/github.com/joho/godotenv/.gitignore diff --git a/plugins/config/vendor/github.com/alexquick/godotenv/.travis.yml b/plugins/config/vendor/github.com/joho/godotenv/.travis.yml similarity index 100% rename from plugins/config/vendor/github.com/alexquick/godotenv/.travis.yml rename to plugins/config/vendor/github.com/joho/godotenv/.travis.yml diff --git a/plugins/config/vendor/github.com/alexquick/godotenv/LICENCE b/plugins/config/vendor/github.com/joho/godotenv/LICENCE similarity index 100% rename from plugins/config/vendor/github.com/alexquick/godotenv/LICENCE rename to plugins/config/vendor/github.com/joho/godotenv/LICENCE diff --git a/plugins/config/vendor/github.com/alexquick/godotenv/README.md b/plugins/config/vendor/github.com/joho/godotenv/README.md similarity index 85% rename from plugins/config/vendor/github.com/alexquick/godotenv/README.md rename to plugins/config/vendor/github.com/joho/godotenv/README.md index a6fb4953a..1a20c78dc 100644 --- a/plugins/config/vendor/github.com/alexquick/godotenv/README.md +++ b/plugins/config/vendor/github.com/joho/godotenv/README.md @@ -1,4 +1,4 @@ -# GoDotEnv [![Build Status](https://travis-ci.org/joho/godotenv.svg?branch=master)](https://travis-ci.org/joho/godotenv) [![Build status](https://ci.appveyor.com/api/projects/status/9v40vnfvvgde64u4?svg=true)](https://ci.appveyor.com/project/joho/godotenv) +# GoDotEnv [![Build Status](https://travis-ci.org/joho/godotenv.svg?branch=master)](https://travis-ci.org/joho/godotenv) [![Build status](https://ci.appveyor.com/api/projects/status/9v40vnfvvgde64u4?svg=true)](https://ci.appveyor.com/project/joho/godotenv) [![Go Report Card](https://goreportcard.com/badge/github.com/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. diff --git a/plugins/config/vendor/github.com/alexquick/godotenv/autoload/autoload.go b/plugins/config/vendor/github.com/joho/godotenv/autoload/autoload.go similarity index 100% rename from plugins/config/vendor/github.com/alexquick/godotenv/autoload/autoload.go rename to plugins/config/vendor/github.com/joho/godotenv/autoload/autoload.go diff --git a/plugins/config/vendor/github.com/alexquick/godotenv/cmd/godotenv/cmd.go b/plugins/config/vendor/github.com/joho/godotenv/cmd/godotenv/cmd.go similarity index 100% rename from plugins/config/vendor/github.com/alexquick/godotenv/cmd/godotenv/cmd.go rename to plugins/config/vendor/github.com/joho/godotenv/cmd/godotenv/cmd.go diff --git a/plugins/config/vendor/github.com/alexquick/godotenv/fixtures/equals.env b/plugins/config/vendor/github.com/joho/godotenv/fixtures/equals.env similarity index 100% rename from plugins/config/vendor/github.com/alexquick/godotenv/fixtures/equals.env rename to plugins/config/vendor/github.com/joho/godotenv/fixtures/equals.env diff --git a/plugins/config/vendor/github.com/alexquick/godotenv/fixtures/exported.env b/plugins/config/vendor/github.com/joho/godotenv/fixtures/exported.env similarity index 100% rename from plugins/config/vendor/github.com/alexquick/godotenv/fixtures/exported.env rename to plugins/config/vendor/github.com/joho/godotenv/fixtures/exported.env diff --git a/plugins/config/vendor/github.com/alexquick/godotenv/fixtures/invalid1.env b/plugins/config/vendor/github.com/joho/godotenv/fixtures/invalid1.env similarity index 100% rename from plugins/config/vendor/github.com/alexquick/godotenv/fixtures/invalid1.env rename to plugins/config/vendor/github.com/joho/godotenv/fixtures/invalid1.env diff --git a/plugins/config/vendor/github.com/alexquick/godotenv/fixtures/plain.env b/plugins/config/vendor/github.com/joho/godotenv/fixtures/plain.env similarity index 100% rename from plugins/config/vendor/github.com/alexquick/godotenv/fixtures/plain.env rename to plugins/config/vendor/github.com/joho/godotenv/fixtures/plain.env diff --git a/plugins/config/vendor/github.com/alexquick/godotenv/fixtures/quoted.env b/plugins/config/vendor/github.com/joho/godotenv/fixtures/quoted.env similarity index 100% rename from plugins/config/vendor/github.com/alexquick/godotenv/fixtures/quoted.env rename to plugins/config/vendor/github.com/joho/godotenv/fixtures/quoted.env diff --git a/plugins/config/vendor/github.com/alexquick/godotenv/godotenv.go b/plugins/config/vendor/github.com/joho/godotenv/godotenv.go similarity index 92% rename from plugins/config/vendor/github.com/alexquick/godotenv/godotenv.go rename to plugins/config/vendor/github.com/joho/godotenv/godotenv.go index e36dbbd9f..48ae78c5d 100644 --- a/plugins/config/vendor/github.com/alexquick/godotenv/godotenv.go +++ b/plugins/config/vendor/github.com/joho/godotenv/godotenv.go @@ -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) { diff --git a/plugins/config/vendor/github.com/alexquick/godotenv/godotenv_test.go b/plugins/config/vendor/github.com/joho/godotenv/godotenv_test.go similarity index 91% rename from plugins/config/vendor/github.com/alexquick/godotenv/godotenv_test.go rename to plugins/config/vendor/github.com/joho/godotenv/godotenv_test.go index 8b353c051..91f4363e9 100644 --- a/plugins/config/vendor/github.com/alexquick/godotenv/godotenv_test.go +++ b/plugins/config/vendor/github.com/joho/godotenv/godotenv_test.go @@ -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)