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

support newlines in parsing env files #990

Merged
merged 1 commit into from
Sep 13, 2016
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.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
74 changes: 4 additions & 70 deletions cmd/emp/env.go
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
package main

import (
"bufio"
"fmt"
"log"
"os"
"sort"
"strings"
"github.com/joho/godotenv"
)

var cmdEnv = &Command{
Expand Down Expand Up @@ -161,81 +161,15 @@ func runEnvLoad(cmd *Command, args []string) {
os.Exit(2)
}

parsedVars, err := ParseEnvFile(args[0])
parsedVars, err := godotenv.Read(args[0])
must(err)

config := make(map[string]*string)
for _, value := range parsedVars {
kv := strings.SplitN(value, "=", 2)
if len(kv) == 1 {
config[kv[0]] = new(string)
} else {
config[kv[0]] = &kv[1]
}
for key, value := range parsedVars {
config[key] = &value
}

_, err = client.ConfigVarUpdate(appname, config, message)
must(err)
log.Printf("Updated env vars from %s and restarted %s.", args[0], appname)
}

// Stripped from https://github.com/docker/docker/blob/3d13fddd2bc4d679f0eaa68b0be877e5a816ad53/runconfig/opts/envfile.go
//
// ParseEnvFile reads a file with environment variables enumerated by lines
//
// ``Environment variable names used by the utilities in the Shell and
// Utilities volume of IEEE Std 1003.1-2001 consist solely of uppercase
// letters, digits, and the '_' (underscore) from the characters defined in
// Portable Character Set and do not begin with a digit. *But*, other
// characters may be permitted by an implementation; applications shall
// tolerate the presence of such names.''
// -- http://pubs.opengroup.org/onlinepubs/009695399/basedefs/xbd_chap08.html
//
// As of #16585, it's up to application inside docker to validate or not
// environment variables, that's why we just strip leading whitespace and
// nothing more.
func ParseEnvFile(filename string) ([]string, error) {
fh, err := os.Open(filename)
if err != nil {
return []string{}, err
}
defer fh.Close()

lines := []string{}
scanner := bufio.NewScanner(fh)
for scanner.Scan() {
// trim the line from all leading whitespace first
line := strings.TrimLeft(scanner.Text(), whiteSpaces)
// line is not empty, and not starting with '#'
if len(line) > 0 && !strings.HasPrefix(line, "#") {
data := strings.SplitN(line, "=", 2)

// trim the front of a variable, but nothing else
variable := strings.TrimLeft(data[0], whiteSpaces)
if strings.ContainsAny(variable, whiteSpaces) {
return []string{}, ErrBadEnvVariable{fmt.Sprintf("variable '%s' has white spaces", variable)}
}

if len(data) > 1 {

// pass the value through, no trimming
lines = append(lines, fmt.Sprintf("%s=%s", variable, data[1]))
} else {
// if only a pass-through variable is given, clean it up.
lines = append(lines, fmt.Sprintf("%s=%s", strings.TrimSpace(line), os.Getenv(line)))
}
}
}
return lines, scanner.Err()
}

var whiteSpaces = " \t"

// ErrBadEnvVariable typed error for bad environment variable
type ErrBadEnvVariable struct {
msg string
}

func (e ErrBadEnvVariable) Error() string {
return fmt.Sprintf("poorly formatted environment: %s", e.msg)
}
21 changes: 21 additions & 0 deletions tests/cli/env_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,9 @@ package cli_test
import (
"testing"
"time"
"io/ioutil"
"os"
"fmt"
)

func TestConfig(t *testing.T) {
Expand Down Expand Up @@ -113,3 +116,21 @@ func TestConfigConsistency(t *testing.T) {
},
})
}

func TestEnvConfig(t *testing.T) {
file, err := ioutil.TempFile(os.TempDir(), "acme-inc.env")
if err != nil {
t.Fatal(err)
}
content := []byte("FOO=bar\\nmoarbar\n")
file.Write(content)

run(t, []Command{
DeployCommand("latest", "v1"),
{
fmt.Sprintf("env-load %s -a acme-inc", file.Name()),
fmt.Sprintf("Updated env vars from %s and restarted acme-inc.", file.Name()),
},
})
defer os.Remove(file.Name())
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can we move this defer a little higher (before the file.Write(content))? Not sure this will get run if the above test fails.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pulled this change in as b55d8b4.

}
23 changes: 23 additions & 0 deletions vendor/github.com/joho/godotenv/LICENCE

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

127 changes: 127 additions & 0 deletions vendor/github.com/joho/godotenv/README.md

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading