rpaste/modules/utility/system.go

86 lines
2.3 KiB
Go
Raw Normal View History

2021-12-28 04:27:10 +00:00
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
2021-12-30 02:05:10 +00:00
// reference just for me. If more features are eventually added to this utility
// it may make sense to eventually use these.
2021-12-28 04:27:10 +00:00
var (
regexPrettyName = regexp.MustCompile(`^PRETTY_NAME=(.*)$`)
regexID = regexp.MustCompile(`^ID=(.*)$`)
regexVersionID = regexp.MustCompile(`^VERSION_ID=(.*)$`)
regexRockyName = regexp.MustCompile(`^Rocky( Linux)? release`)
2024-12-04 02:50:21 +00:00
regexOtherEL = regexp.MustCompile(`^(CentOS Stream|Fedora|Red Hat Enterprise|AlmaLinux|Scientific)( Linux)? release`)
regexRedHatName = regexp.MustCompile(`^(Rocky|CentOS Stream|Fedora|Red Hat Enterprise|AlmaLinux|Scientific)( Linux)? release`)
2021-12-28 04:27:10 +00:00
VendorName string
SystemVersion string
SystemPrettyName string
2021-12-29 07:40:36 +00:00
pasteBin string
2021-12-30 02:05:10 +00:00
osReleaseID string
simpleOSID string
2021-12-28 04:27:10 +00:00
)
2021-12-30 02:05:10 +00:00
// Gets the distro id only
func GetDistroID() string {
2021-12-28 04:27:10 +00:00
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], `"`)
}
}
2021-12-30 02:05:10 +00:00
return VendorName
}
// Sets the pastebin based on the OS
func SetPasteOnOS() string {
osReleaseID = GetDistroID()
2021-12-29 07:40:36 +00:00
// The order should be: config -> this
2021-12-30 02:05:10 +00:00
switch osReleaseID {
2021-12-28 04:27:10 +00:00
case "rocky":
2021-12-29 07:40:36 +00:00
pasteBin = "rpaste"
2021-12-28 04:27:10 +00:00
case "redhat", "fedora", "centos", "almalinux", "scientific":
2021-12-29 07:40:36 +00:00
pasteBin = "fpaste"
2021-12-30 02:05:10 +00:00
case "opensuse-leap", "opensuse-tumbleweed", "sles":
// this will be on rpaste for now
pasteBin = "rpaste"
2021-12-29 07:40:36 +00:00
default:
pasteBin = "rpaste"
2021-12-28 04:27:10 +00:00
}
2021-12-29 07:40:36 +00:00
return pasteBin
2021-12-28 04:27:10 +00:00
}
2021-12-30 02:05:10 +00:00
// 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
}