Skip to content

Commit

Permalink
opts/envfile: trim all leading whitespace in a line
Browse files Browse the repository at this point in the history
Signed-off-by: Matt Robenolt <matt@ydekproductions.com>
  • Loading branch information
mattrobenolt committed Aug 22, 2015
1 parent e6be51a commit f1988f0
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 5 deletions.
7 changes: 3 additions & 4 deletions opts/envfile.go
Expand Up @@ -26,13 +26,12 @@ func ParseEnvFile(filename string) ([]string, error) {
lines := []string{}
scanner := bufio.NewScanner(fh)
for scanner.Scan() {
line := scanner.Text()
// 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)
variable := data[0]

if !EnvironmentVariableRegexp.MatchString(variable) {
return []string{}, ErrBadEnvVariable{fmt.Sprintf("variable '%s' is not a valid environment variable", variable)}
Expand Down
7 changes: 6 additions & 1 deletion opts/envfile_test.go
Expand Up @@ -29,7 +29,12 @@ func TestParseEnvFileGoodFile(t *testing.T) {
_foobar=foobaz
`

// Adding a newline + a line with pure whitespace.
// This is being done like this instead of the block above
// because it's common for editors to trim trailing whitespace
// from lines, which becomes annoying since that's the
// exact thing we need to test.
content += "\n \t "
tmpFile := tmpFileWithContent(content, t)
defer os.Remove(tmpFile)

Expand Down

0 comments on commit f1988f0

Please sign in to comment.