miaopanel
1package system
2
3import (
4 "os"
5 "os/exec"
6 "path/filepath"
7 "regexp"
8 "sort"
9 "strconv"
10 "strings"
11)
12
13// Runtimes describes the language runtimes available on the host.
14type Runtimes struct {
15 PHP []string `json:"php"`
16 Node []string `json:"node"`
17 NodeCurrent string `json:"node_current"`
18 Go string `json:"go"`
19 NPM bool `json:"npm"`
20 PNPM bool `json:"pnpm"`
21 Yarn bool `json:"yarn"`
22}
23
24var phpSock = regexp.MustCompile(`^php(\d+\.\d+)-fpm\.sock$`)
25
26// DetectRuntimes scans the standard install locations and PATH.
27func DetectRuntimes() Runtimes {
28 rt := Runtimes{PHP: []string{}, Node: []string{}}
29
30 // PHP: Debian sockets plus the panel layout.
31 seenPHP := map[string]bool{}
32 if entries, err := os.ReadDir("/run/php"); err == nil {
33 for _, e := range entries {
34 if m := phpSock.FindStringSubmatch(e.Name()); m != nil && !seenPHP[m[1]] {
35 seenPHP[m[1]] = true
36 rt.PHP = append(rt.PHP, m[1])
37 }
38 }
39 }
40 if entries, err := os.ReadDir("/www/server/php"); err == nil {
41 for _, e := range entries {
42 if !e.IsDir() || seenPHP[e.Name()] {
43 continue
44 }
45 if _, err := os.Stat(filepath.Join("/www/server/php", e.Name(), "bin/php")); err == nil {
46 seenPHP[e.Name()] = true
47 rt.PHP = append(rt.PHP, e.Name())
48 }
49 }
50 }
51 // Remi (RHEL) exposes one php-fpm socket and the version only through the
52 // CLI, so fall back to `php -v` when no per-version sockets were found.
53 if len(rt.PHP) == 0 {
54 if out, err := exec.Command("php", "-v").Output(); err == nil {
55 if m := regexp.MustCompile(`PHP ([0-9]+\.[0-9]+)`).FindStringSubmatch(string(out)); m != nil {
56 rt.PHP = append(rt.PHP, m[1])
57 }
58 }
59 }
60 sortVersions(rt.PHP)
61
62 // Node: versioned directories then the PATH binary.
63 seenNode := map[string]bool{}
64 if entries, err := os.ReadDir("/www/server/node"); err == nil {
65 for _, e := range entries {
66 if !e.IsDir() {
67 continue
68 }
69 version := strings.TrimPrefix(e.Name(), "v")
70 if _, err := os.Stat(filepath.Join("/www/server/node", e.Name(), "bin/node")); err == nil && !seenNode[version] {
71 seenNode[version] = true
72 rt.Node = append(rt.Node, version)
73 }
74 }
75 }
76 if Lookup("node") {
77 rt.NodeCurrent = commandVersion("node", "-v")
78 if rt.NodeCurrent != "" && !seenNode[strings.TrimPrefix(rt.NodeCurrent, "v")] {
79 rt.Node = append(rt.Node, strings.TrimPrefix(rt.NodeCurrent, "v"))
80 }
81 }
82 sortVersions(rt.Node)
83
84 if Lookup("go") {
85 rt.Go = commandVersion("go", "version")
86 }
87 rt.NPM = Lookup("npm")
88 rt.PNPM = Lookup("pnpm")
89 rt.Yarn = Lookup("yarn")
90 return rt
91}
92
93func commandVersion(bin string, args ...string) string {
94 out, err := exec.Command(bin, args...).Output()
95 if err != nil {
96 return ""
97 }
98 return strings.TrimSpace(string(out))
99}
100
101// sortVersions orders "8.2" / "18.20.4" descending.
102func sortVersions(list []string) {
103 sort.Slice(list, func(i, j int) bool { return compareVersion(list[i], list[j]) > 0 })
104}
105
106func compareVersion(a, b string) int {
107 pa := strings.Split(strings.TrimPrefix(a, "v"), ".")
108 pb := strings.Split(strings.TrimPrefix(b, "v"), ".")
109 for i := 0; i < len(pa) || i < len(pb); i++ {
110 var na, nb int
111 if i < len(pa) {
112 na, _ = strconv.Atoi(pa[i])
113 }
114 if i < len(pb) {
115 nb, _ = strconv.Atoi(pb[i])
116 }
117 if na != nb {
118 if na > nb {
119 return 1
120 }
121 return -1
122 }
123 }
124 return 0
125}