Skip to content

Commit 6ec03bc

Browse files
authored
Merge pull request #105 from babbageclunk/windows-newlines
lp:1596045 handle CRLF in mongo version line The old code was splitting on \n which meant that a \r character was left on the version line on windows. Using a bufio.Scanner handles the newlines in a cross-platform way. (Review request: http://reviews.vapour.ws/r/5163/)
2 parents b718b71 + ccf839b commit 6ec03bc

File tree

1 file changed

+10
-3
lines changed

1 file changed

+10
-3
lines changed

mgo.go

+10-3
Original file line numberDiff line numberDiff line change
@@ -338,9 +338,16 @@ func detectMongoVersion() (version.Number, error) {
338338
if err != nil {
339339
return version.Zero, errors.Trace(err)
340340
}
341-
parts := strings.SplitN(string(output), "\n", 2)
342-
// There's guaranteed to be at least one element, even if output's empty.
343-
versionLine := parts[0]
341+
// Read the first line of the output with a scanner (to handle
342+
// newlines in a cross-platform way).
343+
scanner := bufio.NewScanner(bytes.NewReader(output))
344+
versionLine := ""
345+
if scanner.Scan() {
346+
versionLine = scanner.Text()
347+
}
348+
if scanner.Err() != nil {
349+
return version.Zero, errors.Trace(scanner.Err())
350+
}
344351
if !strings.HasPrefix(versionLine, versionLinePrefix) {
345352
return version.Zero, errors.New("couldn't get mongod version - no version line")
346353
}

0 commit comments

Comments
 (0)