5
0
mirror of https://github.com/wailsapp/wails.git synced 2025-05-02 21:43:08 +08:00
wails/v2/internal/system/operatingsystem/os_linux.go
Eng Zer Jun ef8d7d2fd7
refactor: move from io/ioutil to io and os packages
The io/ioutil package has been deprecated as of Go 1.16, see
https://golang.org/doc/go1.16#ioutil. This commit replaces the existing
io/ioutil functions with their new definitions in io and os packages.

Signed-off-by: Eng Zer Jun <engzerjun@gmail.com>
2021-11-25 12:15:43 +08:00

52 lines
1.1 KiB
Go

//go:build linux
// +build linux
package operatingsystem
import (
"fmt"
"os"
"strings"
)
// platformInfo is the platform specific method to get system information
func platformInfo() (*OS, error) {
_, err := os.Stat("/etc/os-release")
if os.IsNotExist(err) {
return nil, fmt.Errorf("unable to read system information")
}
osRelease, _ := os.ReadFile("/etc/os-release")
return parseOsRelease(string(osRelease)), nil
}
func parseOsRelease(osRelease string) *OS {
// Default value
var result OS
result.ID = "Unknown"
result.Name = "Unknown"
result.Version = "Unknown"
// Split into lines
lines := strings.Split(osRelease, "\n")
// Iterate lines
for _, line := range lines {
// Split each line by the equals char
splitLine := strings.SplitN(line, "=", 2)
// Check we have
if len(splitLine) != 2 {
continue
}
switch splitLine[0] {
case "ID":
result.ID = strings.ToLower(strings.Trim(splitLine[1], "\""))
case "NAME":
result.Name = strings.Trim(splitLine[1], "\"")
case "VERSION_ID":
result.Version = strings.Trim(splitLine[1], "\"")
}
}
return &result
}