rpaste/modules/utility/system.go

86 lines
2.3 KiB
Go

package utility
import (
"bufio"
"os"
"regexp"
"strings"
)
// Not all of these are going to be used, but just in case. This is more of a
// reference just for me. If more features are eventually added to this utility
// it may make sense to eventually use these.
var (
regexPrettyName = regexp.MustCompile(`^PRETTY_NAME=(.*)$`)
regexID = regexp.MustCompile(`^ID=(.*)$`)
regexVersionID = regexp.MustCompile(`^VERSION_ID=(.*)$`)
regexRockyName = regexp.MustCompile(`^Rocky( Linux)? release`)
regexOtherEL = regexp.MustCompile(`^(CentOS|Fedora|Red Hat Enterprise|AlmaLinux|Scientific)( Linux)? release`)
regexRedHatName = regexp.MustCompile(`^(Rocky|CentOS|Fedora|Red Hat Enterprise|AlmaLinux|Scientific)( Linux)? release`)
VendorName string
SystemVersion string
SystemPrettyName string
pasteBin string
osReleaseID string
simpleOSID string
)
// Gets the distro id only
func GetDistroID() string {
file, err := os.Open("/etc/os-release")
if err != nil {
return ""
}
defer file.Close()
scanner := bufio.NewScanner(file)
for scanner.Scan() {
if m := regexID.FindStringSubmatch(scanner.Text()); m != nil {
VendorName = strings.Trim(m[1], `"`)
}
}
return VendorName
}
// Sets the pastebin based on the OS
func SetPasteOnOS() string {
osReleaseID = GetDistroID()
// The order should be: config -> this
switch osReleaseID {
case "rocky":
pasteBin = "rpaste"
case "redhat", "fedora", "centos", "almalinux", "scientific":
pasteBin = "fpaste"
case "opensuse-leap", "opensuse-tumbleweed", "sles":
// this will be on rpaste for now
pasteBin = "rpaste"
default:
pasteBin = "rpaste"
}
return pasteBin
}
// This is because there are several derivatives and some distros come from
// another. For example, rocky, rhel, alma, centos, fedora, they all generally
// operate the same (eg, sysinfo should work on all of them). But suse, this
// isn't that simple, unfortunately.
func GetGenericDistroID() string {
osReleaseID = GetDistroID()
switch osReleaseID {
case "rocky", "redhat", "fedora", "centos", "almalinux", "scientific":
simpleOSID = "redhat"
case "opensuse-leap", "opensuse-tumbleweed", "sles":
simpleOSID = "suse"
case "debian", "ubuntu":
simpleOSID = "debian"
default:
simpleOSID = "unknown"
}
return simpleOSID
}