pkg/parsers/operatingsystem: don't use strings.SplitN()
We're looking for a specific prefix, so remove the prefix instead. Also remove
redundant error-wrapping, as `os.Open()` already provides details in the error
returned;
open /no/such/file: no such file or directory
open /etc/os-release: permission denied
@@ -5,7 +5,6 @@ package operatingsystem // import "github.com/docker/docker/pkg/parsers/operatin
import (
"bufio"
"bytes"
- "fmt"
"os"
"strings"
)
@@ -44,23 +43,22 @@ func getValueFromOsRelease(key string) (string, error) {
osReleaseFile, err := os.Open(etcOsRelease)
if err != nil {
if !os.IsNotExist(err) {
- return "", fmt.Errorf("Error opening %s: %v", etcOsRelease, err)
+ return "", err
}
osReleaseFile, err = os.Open(altOsRelease)
if err != nil {
- return "", fmt.Errorf("Error opening %s: %v", altOsRelease, err)
+ return "", err
}
}
defer osReleaseFile.Close()
var value string
- keyWithTrailingEqual := key + "="
scanner := bufio.NewScanner(osReleaseFile)
for scanner.Scan() {
line := scanner.Text()
- if strings.HasPrefix(line, keyWithTrailingEqual) {
- data := strings.SplitN(line, "=", 2)
- value = strings.Trim(data[1], `"' `) // remove leading/trailing quotes and whitespace
+ if strings.HasPrefix(line, key+"=") {
+ value = strings.TrimPrefix(line, key+"=")
+ value = strings.Trim(value, `"' `) // remove leading/trailing quotes and whitespace
}
}
