Skip to content

Commit

Permalink
fix: trim quote characters in osinfo
Browse files Browse the repository at this point in the history
Some distributions may use quotes in their os-info file (ex: centos).
The osinfo helpers must handle these cases for correct parsing.
  • Loading branch information
NDStrahilevitz committed Aug 1, 2023
1 parent 0238ec3 commit 4ff3e2e
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 2 deletions.
6 changes: 4 additions & 2 deletions helpers/osinfo.go
Original file line number Diff line number Diff line change
Expand Up @@ -160,7 +160,7 @@ type OSInfo struct {

// GetOSReleaseFieldValue provides access to internal OSInfo OSReleaseField's
func (btfi *OSInfo) GetOSReleaseFieldValue(value OSReleaseField) string {
return btfi.osReleaseFieldValues[value]
return strings.Trim(btfi.osReleaseFieldValues[value], "\"")
}

// GetOSReleaseFilePath provides the path for the used os-release file as it might
Expand Down Expand Up @@ -226,7 +226,9 @@ func (btfi *OSInfo) discoverOSDistro() error {
}
btfi.osReleaseFieldValues[keyID] = val[1]
if keyID == OS_ID {
btfi.osReleaseID = stringToOSReleaseID[strings.ToLower(val[1])]
osVal := strings.ToLower(val[1])
osVal = strings.Trim(osVal, "\"") // trim potential parentheses (in centos)
btfi.osReleaseID = stringToOSReleaseID[osVal]
}
}

Expand Down
7 changes: 7 additions & 0 deletions helpers/osinfo_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,13 @@ func TestGetOSInfo(t *testing.T) {
expectedOSReleaseID: UBUNTU,
expectedError: nil,
},
{
testName: "env os-release filepath",
osReleaseFilePath: "testdata/os-release-centos",
expectedOSReleaseFilePath: "testdata/os-release-centos",
expectedOSReleaseID: CENTOS,
expectedError: nil,
},
{
testName: "default os-release filepath",
osReleaseFilePath: "",
Expand Down
13 changes: 13 additions & 0 deletions helpers/testdata/os-release-centos
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
NAME="CentOS Stream"
VERSION="8"
ID="centos"
ID_LIKE="rhel fedora"
VERSION_ID="8"
PLATFORM_ID="platform:el8"
PRETTY_NAME="CentOS Stream 8"
ANSI_COLOR="0;31"
CPE_NAME="cpe:/o:centos:centos:8"
HOME_URL="https://centos.org/"
BUG_REPORT_URL="https://bugzilla.redhat.com/"
REDHAT_SUPPORT_PRODUCT="Red Hat Enterprise Linux 8"
REDHAT_SUPPORT_PRODUCT_VERSION="CentOS Stream"

0 comments on commit 4ff3e2e

Please sign in to comment.