package system import ( "bufio" "os" "runtime" "strings" ) // Distro describes the host operating system family. type Distro struct { ID string `json:"id"` // ubuntu, debian, rocky, centos... Version string `json:"version"` PrettyName string `json:"pretty_name"` Kernel string `json:"kernel"` Arch string `json:"arch"` Family string `json:"family"` // debian | rhel | unknown Supported bool `json:"supported"` Systemd bool `json:"systemd"` PanelOS string `json:"panel_os"` } // Detect reads /etc/os-release and reports the package manager family. func Detect() Distro { d := Distro{Arch: runtime.GOARCH, PanelOS: runtime.GOOS} d.Kernel = readTrim("/proc/sys/kernel/osrelease") if d.Kernel == "" { d.Kernel = runtime.GOOS } f, err := os.Open("/etc/os-release") if err != nil { d.ID = runtime.GOOS d.PrettyName = runtime.GOOS return d } defer f.Close() sc := bufio.NewScanner(f) for sc.Scan() { line := strings.TrimSpace(sc.Text()) key, val, ok := strings.Cut(line, "=") if !ok { continue } val = strings.Trim(val, `"'`) switch key { case "ID": d.ID = val case "VERSION_ID": d.Version = val case "PRETTY_NAME": d.PrettyName = val } } if err := sc.Err(); err != nil && d.ID == "" { // A truncated os-release should still yield a usable identifier. d.ID = runtime.GOOS d.PrettyName = runtime.GOOS } switch d.ID { case "ubuntu", "debian", "linuxmint", "raspbian": d.Family = "debian" case "centos", "rhel", "rocky", "almalinux", "fedora", "openeuler", "anolis": d.Family = "rhel" default: d.Family = "unknown" } d.Supported = d.Family == "debian" || d.Family == "rhel" if st, err := os.Stat("/run/systemd/system"); err == nil && st.IsDir() { d.Systemd = true } return d } // PackageManager returns apt-get / dnf / yum for the detected family. func (d Distro) PackageManager() string { switch d.Family { case "debian": return "apt-get" case "rhel": if Lookup("dnf") { return "dnf" } return "yum" } return "" } // ServiceManager returns "systemd" or "" when unsupported. func (d Distro) ServiceManager() string { if d.Systemd { return "systemd" } return "" } func readTrim(path string) string { b, err := os.ReadFile(path) if err != nil { return "" } return strings.TrimSpace(string(b)) }