opts/envfile: trim all leading whitespace in a line

Signed-off-by: Matt Robenolt <matt@ydekproductions.com>
This commit is contained in:
Matt Robenolt 2015-08-21 13:28:01 -07:00 committed by Vincent Demeester
parent 091f800069
commit 869e08a12b
2 changed files with 9 additions and 5 deletions

View File

@ -26,13 +26,12 @@ func ParseEnvFile(filename string) ([]string, error) {
lines := []string{} lines := []string{}
scanner := bufio.NewScanner(fh) scanner := bufio.NewScanner(fh)
for scanner.Scan() { 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 '#' // line is not empty, and not starting with '#'
if len(line) > 0 && !strings.HasPrefix(line, "#") { if len(line) > 0 && !strings.HasPrefix(line, "#") {
data := strings.SplitN(line, "=", 2) data := strings.SplitN(line, "=", 2)
variable := data[0]
// trim the front of a variable, but nothing else
variable := strings.TrimLeft(data[0], whiteSpaces)
if !EnvironmentVariableRegexp.MatchString(variable) { if !EnvironmentVariableRegexp.MatchString(variable) {
return []string{}, ErrBadEnvVariable{fmt.Sprintf("variable '%s' is not a valid environment variable", variable)} return []string{}, ErrBadEnvVariable{fmt.Sprintf("variable '%s' is not a valid environment variable", variable)}

View File

@ -29,7 +29,12 @@ func TestParseEnvFileGoodFile(t *testing.T) {
_foobar=foobaz _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) tmpFile := tmpFileWithContent(content, t)
defer os.Remove(tmpFile) defer os.Remove(tmpFile)