Skip to content

Commit

Permalink
[system] More fields support from 'os-release' file
Browse files Browse the repository at this point in the history
  • Loading branch information
andyone committed Jul 5, 2022
1 parent 9c268f2 commit 472b44a
Show file tree
Hide file tree
Showing 4 changed files with 52 additions and 24 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
### 12.50.0

* `[fmtc]` Added methods `TPrint`, `LPrint` and `TLPrint`
* `[system]` More fields support from `os-release` file
* `[fmtc]` Added more usage examples

### 12.49.0
Expand Down
4 changes: 4 additions & 0 deletions system/info.go
Original file line number Diff line number Diff line change
Expand Up @@ -156,6 +156,10 @@ type OSInfo struct {
VersionCodename string `json:"version_codename"`
ID string `json:"id"`
IDLike string `json:"id_like"`
PlatformID string `json:"platform_id"`
Variant string `json:"variant"`
VariantID string `json:"variant_id"`
CPEName string `json:"cpe_name"`
HomeURL string `json:"home_url"`
BugReportURL string `json:"bugreport_url"`
SupportURL string `json:"support_url"`
Expand Down
59 changes: 36 additions & 23 deletions system/info_linux.go
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ func GetSystemInfo() (*SystemInfo, error) {
Hostname: byteSliceToString(info.Nodename),
OS: byteSliceToString(info.Sysname),
Distribution: formatDistName(osInfo.Name),
Version: osInfo.Version,
Version: osInfo.VersionID,
Kernel: byteSliceToString(info.Release),
Arch: byteSliceToString(info.Machine),
ArchBits: getCPUArchBits(),
Expand All @@ -68,35 +68,48 @@ func GetOSInfo() (*OSInfo, error) {
name := strutil.ReadField(line, 0, false, "=")
value := strings.Trim(strutil.ReadField(line, 1, false, "="), "\"")

switch name {
case "NAME":
info.Name = value
case "VERSION":
info.Version = value
case "ID":
info.ID = value
case "ID_LIKE":
info.IDLike = value
case "VERSION_ID":
info.VersionID = value
case "VERSION_CODENAME":
info.VersionCodename = value
case "PRETTY_NAME":
info.PrettyName = value
case "HOME_URL":
info.HomeURL = value
case "BUG_REPORT_URL":
info.BugReportURL = value
case "SUPPORT_URL":
info.SupportURL = value
}
applyOSInfo(info, name, value)
}

return info, nil
}

// ////////////////////////////////////////////////////////////////////////////////// //

// applyOSInfo applies record from os-release
func applyOSInfo(info *OSInfo, name, value string) {
switch name {
case "NAME":
info.Name = value
case "VERSION":
info.Version = value
case "ID":
info.ID = value
case "ID_LIKE":
info.IDLike = value
case "PRETTY_NAME":
info.PrettyName = value
case "VERSION_ID":
info.VersionID = value
case "PLATFORM_ID":
info.PlatformID = value
case "VARIANT":
info.Variant = value
case "VARIANT_ID":
info.VariantID = value
case "CPE_NAME":
info.CPEName = value
case "VERSION_CODENAME":
info.VersionCodename = value
case "HOME_URL":
info.HomeURL = value
case "BUG_REPORT_URL":
info.BugReportURL = value
case "SUPPORT_URL":
info.SupportURL = value
}
}

// formatDistName formats distribution name
func formatDistName(name string) string {
switch strings.ToUpper(name) {
Expand Down
12 changes: 11 additions & 1 deletion system/info_linux_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -620,7 +620,13 @@ VERSION_ID="20.10"
HOME_URL="https://www.ubuntu.com/"
SUPPORT_URL="https://help.ubuntu.com/"
BUG_REPORT_URL="https://bugs.launchpad.net/ubuntu/"
VERSION_CODENAME=groovy`)
VERSION_CODENAME=groovy
VARIANT="Server"
VARIANT_ID="server"
PLATFORM_ID="platform:el8"
CPE_NAME="cpe:/o:oracle:linux:8:6:server"
`)

sysInfo, err = GetSystemInfo()

Expand All @@ -636,6 +642,10 @@ VERSION_CODENAME=groovy`)
c.Assert(osInfo.VersionCodename, Equals, "groovy")
c.Assert(osInfo.ID, Equals, "ubuntu")
c.Assert(osInfo.IDLike, Equals, "debian")
c.Assert(osInfo.PlatformID, Equals, "platform:el8")
c.Assert(osInfo.Variant, Equals, "Server")
c.Assert(osInfo.VariantID, Equals, "server")
c.Assert(osInfo.CPEName, Equals, "cpe:/o:oracle:linux:8:6:server")
c.Assert(osInfo.HomeURL, Equals, "https://www.ubuntu.com/")
c.Assert(osInfo.BugReportURL, Equals, "https://bugs.launchpad.net/ubuntu/")
c.Assert(osInfo.SupportURL, Equals, "https://help.ubuntu.com/")
Expand Down

0 comments on commit 472b44a

Please sign in to comment.