package system import ( "os" "os/exec" "path/filepath" "regexp" "sort" "strconv" "strings" ) // Runtimes describes the language runtimes available on the host. type Runtimes struct { PHP []string `json:"php"` Node []string `json:"node"` NodeCurrent string `json:"node_current"` Go string `json:"go"` NPM bool `json:"npm"` PNPM bool `json:"pnpm"` Yarn bool `json:"yarn"` } var phpSock = regexp.MustCompile(`^php(\d+\.\d+)-fpm\.sock$`) // DetectRuntimes scans the standard install locations and PATH. func DetectRuntimes() Runtimes { rt := Runtimes{PHP: []string{}, Node: []string{}} // PHP: Debian sockets plus the panel layout. seenPHP := map[string]bool{} if entries, err := os.ReadDir("/run/php"); err == nil { for _, e := range entries { if m := phpSock.FindStringSubmatch(e.Name()); m != nil && !seenPHP[m[1]] { seenPHP[m[1]] = true rt.PHP = append(rt.PHP, m[1]) } } } if entries, err := os.ReadDir("/www/server/php"); err == nil { for _, e := range entries { if !e.IsDir() || seenPHP[e.Name()] { continue } if _, err := os.Stat(filepath.Join("/www/server/php", e.Name(), "bin/php")); err == nil { seenPHP[e.Name()] = true rt.PHP = append(rt.PHP, e.Name()) } } } // Remi (RHEL) exposes one php-fpm socket and the version only through the // CLI, so fall back to `php -v` when no per-version sockets were found. if len(rt.PHP) == 0 { if out, err := exec.Command("php", "-v").Output(); err == nil { if m := regexp.MustCompile(`PHP ([0-9]+\.[0-9]+)`).FindStringSubmatch(string(out)); m != nil { rt.PHP = append(rt.PHP, m[1]) } } } sortVersions(rt.PHP) // Node: versioned directories then the PATH binary. seenNode := map[string]bool{} if entries, err := os.ReadDir("/www/server/node"); err == nil { for _, e := range entries { if !e.IsDir() { continue } version := strings.TrimPrefix(e.Name(), "v") if _, err := os.Stat(filepath.Join("/www/server/node", e.Name(), "bin/node")); err == nil && !seenNode[version] { seenNode[version] = true rt.Node = append(rt.Node, version) } } } if Lookup("node") { rt.NodeCurrent = commandVersion("node", "-v") if rt.NodeCurrent != "" && !seenNode[strings.TrimPrefix(rt.NodeCurrent, "v")] { rt.Node = append(rt.Node, strings.TrimPrefix(rt.NodeCurrent, "v")) } } sortVersions(rt.Node) if Lookup("go") { rt.Go = commandVersion("go", "version") } rt.NPM = Lookup("npm") rt.PNPM = Lookup("pnpm") rt.Yarn = Lookup("yarn") return rt } func commandVersion(bin string, args ...string) string { out, err := exec.Command(bin, args...).Output() if err != nil { return "" } return strings.TrimSpace(string(out)) } // sortVersions orders "8.2" / "18.20.4" descending. func sortVersions(list []string) { sort.Slice(list, func(i, j int) bool { return compareVersion(list[i], list[j]) > 0 }) } func compareVersion(a, b string) int { pa := strings.Split(strings.TrimPrefix(a, "v"), ".") pb := strings.Split(strings.TrimPrefix(b, "v"), ".") for i := 0; i < len(pa) || i < len(pb); i++ { var na, nb int if i < len(pa) { na, _ = strconv.Atoi(pa[i]) } if i < len(pb) { nb, _ = strconv.Atoi(pb[i]) } if na != nb { if na > nb { return 1 } return -1 } } return 0 }