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

Signed-off-by: Sebastiaan van Stijn <github@gone.nl>
This commit is contained in:
Sebastiaan van Stijn 2022-11-01 12:09:50 +01:00
parent 451b8579ef
commit 6059d38f21
No known key found for this signature in database
GPG key ID: 76698F39D527CE8C

View file

@ -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
}
}