Add 'custom' module

This commit is contained in:
2025-12-22 11:54:30 +02:00
parent 9ce8e5ef0b
commit c7bfaf7a31
4 changed files with 69 additions and 64 deletions
+33
View File
@@ -399,4 +399,37 @@ func initializeModuleMap() {
return builder.String() return builder.String()
}} }}
RegisterModule(monitorsModule) RegisterModule(monitorsModule)
// Custom module
customModule := StormfetchModule{stormfetchModuleConfig: stormfetchModuleConfig{Name: "custom"}, Execute: func(sm StormfetchModule) string {
shell, _ := sm.GetData("shell", "/bin/sh")
commandList, _ := sm.GetData("commands", make([]any, 0))
// Exeucte all commands
commandOutput := make(map[int]string)
for i, value := range commandList.([]any) {
command, ok := value.(string)
if !ok {
continue
}
commandOutput[i+1] = runCommand(command, shell.(string))
}
return os.Expand(sm.Format, func(s string) string {
if len(s) <= 4 || !strings.HasPrefix(s, "CMD_") {
return ""
}
commandIndexStr := strings.Split(s, "CMD_")[1]
commandIndex, err := strconv.Atoi(commandIndexStr)
if err != nil {
return ""
}
return commandOutput[commandIndex]
})
}}
RegisterModule(customModule)
} }
+4 -19
View File
@@ -138,21 +138,6 @@ func GetKernel() (string, string) {
} }
func GetInitSystem() string { func GetInitSystem() string {
runCommand := func(command string) string {
cmd := exec.Command("/bin/sh", "-c", command)
workdir, err := os.Getwd()
if err != nil {
return ""
}
cmd.Dir = workdir
cmd.Env = os.Environ()
out, err := cmd.Output()
if err != nil {
return ""
}
return strings.TrimSpace(string(out))
}
process, err := ps.FindProcess(1) process, err := ps.FindProcess(1)
if err != nil { if err != nil {
return "" return ""
@@ -161,19 +146,19 @@ func GetInitSystem() string {
// Special cases // Special cases
// OpenRC check // OpenRC check
if _, err := os.Stat("/usr/sbin/openrc"); err == nil { if _, err := os.Stat("/usr/sbin/openrc"); err == nil {
return "OpenRC " + runCommand("openrc --version | awk '{print $3}'") return "OpenRC " + runCommand("openrc --version | awk '{print $3}'", "/bin/sh")
} }
// Default PID 1 process name checking // Default PID 1 process name checking
switch process.Executable() { switch process.Executable() {
case "systemd": case "systemd":
return "Systemd " + runCommand("systemctl --version | head -n1 | awk '{print $2}'") return "Systemd " + runCommand("systemctl --version | head -n1 | awk '{print $2}'", "/bin/sh")
case "runit": case "runit":
return "Runit" return "Runit"
case "dinit": case "dinit":
return "Dinit " + runCommand("dinit --version | head -n1 | awk '{print substr($3, 1, length($3)-1)}'") return "Dinit " + runCommand("dinit --version | head -n1 | awk '{print substr($3, 1, length($3)-1)}'", "/bin/sh")
case "enit": case "enit":
return "Enit " + runCommand("enit --version | awk '{print $3}'") return "Enit " + runCommand("enit --version | awk '{print $3}'", "/bin/sh")
default: default:
return process.Executable() return process.Executable()
} }
+16 -45
View File
@@ -3,7 +3,6 @@ package main
import ( import (
"log" "log"
"os" "os"
"os/exec"
"path/filepath" "path/filepath"
"slices" "slices"
"strconv" "strconv"
@@ -19,20 +18,6 @@ type DEWM struct {
} }
func GetShell() string { func GetShell() string {
runCommand := func(command string) string {
cmd := exec.Command("/bin/sh", "-c", command)
workdir, err := os.Getwd()
if err != nil {
return ""
}
cmd.Dir = workdir
cmd.Env = os.Environ()
out, err := cmd.Output()
if err != nil {
return ""
}
return strings.TrimSpace(string(out))
}
file, err := os.ReadFile("/etc/passwd") file, err := os.ReadFile("/etc/passwd")
if err != nil { if err != nil {
return "" return ""
@@ -54,13 +39,13 @@ func GetShell() string {
case "dash": case "dash":
return "Dash" return "Dash"
case "bash": case "bash":
return "Bash " + runCommand("echo $BASH_VERSION") return "Bash " + runCommand("echo $BASH_VERSION", "/bin/sh")
case "zsh": case "zsh":
return "Zsh " + runCommand("$SHELL --version | awk '{print $2}'") return "Zsh " + runCommand("$SHELL --version | awk '{print $2}'", "/bin/sh")
case "fish": case "fish":
return "Fish " + runCommand("$SHELL --version | awk '{print $3}'") return "Fish " + runCommand("$SHELL --version | awk '{print $3}'", "/bin/sh")
case "nu": case "nu":
return "Nushell " + runCommand("$SHELL --version") return "Nushell " + runCommand("$SHELL --version", "/bin/sh")
default: default:
return "Unknown" return "Unknown"
} }
@@ -79,53 +64,39 @@ func GetDEWM() DEWM {
processExists := func(process string) bool { processExists := func(process string) bool {
return slices.Contains(executables, process) return slices.Contains(executables, process)
} }
runCommand := func(command string) string {
cmd := exec.Command("/bin/sh", "-c", command)
workdir, err := os.Getwd()
if err != nil {
return ""
}
cmd.Dir = workdir
cmd.Env = os.Environ()
out, err := cmd.Output()
if err != nil {
return ""
}
return strings.TrimSpace(string(out))
}
if processExists("plasmashell") { if processExists("plasmashell") {
dewm := DEWM{ dewm := DEWM{
Name: "KDE Plasma", Name: "KDE Plasma",
Type: "DE", Type: "DE",
Version: runCommand("plasmashell --version | awk '{print $2}'"), Version: runCommand("plasmashell --version | awk '{print $2}'", "/bin/sh"),
} }
return dewm return dewm
} else if processExists("gnome-session") { } else if processExists("gnome-session") {
dewm := DEWM{ dewm := DEWM{
Name: "Gnome", Name: "Gnome",
Type: "DE", Type: "DE",
Version: runCommand("gnome-shell --version | awk '{print $3}'"), Version: runCommand("gnome-shell --version | awk '{print $3}'", "/bin/sh"),
} }
return dewm return dewm
} else if processExists("xfce4-session") { } else if processExists("xfce4-session") {
dewm := DEWM{ dewm := DEWM{
Name: "XFCE", Name: "XFCE",
Type: "DE", Type: "DE",
Version: runCommand("xfce4-session --version | head -n1 | awk '{print $2}'"), Version: runCommand("xfce4-session --version | head -n1 | awk '{print $2}'", "/bin/sh"),
} }
return dewm return dewm
} else if processExists("cinnamon") { } else if processExists("cinnamon") {
dewm := DEWM{ dewm := DEWM{
Name: "Cinnamon", Name: "Cinnamon",
Type: "DE", Type: "DE",
Version: runCommand("cinnamon --version | awk '{print $3}'"), Version: runCommand("cinnamon --version | awk '{print $3}'", "/bin/sh"),
} }
return dewm return dewm
} else if processExists("mate-panel") { } else if processExists("mate-panel") {
dewm := DEWM{ dewm := DEWM{
Name: "MATE", Name: "MATE",
Type: "DE", Type: "DE",
Version: runCommand("mate-about --version | awk '{print $4}'"), Version: runCommand("mate-about --version | awk '{print $4}'", "/bin/sh"),
} }
return dewm return dewm
} else if processExists("lxsession") { } else if processExists("lxsession") {
@@ -139,23 +110,23 @@ func GetDEWM() DEWM {
dewm := DEWM{ dewm := DEWM{
Name: "LXQt", Name: "LXQt",
Type: "DE", Type: "DE",
Version: runCommand("lxqt-session --version | head -n1 | awk '{print $2}'"), Version: runCommand("lxqt-session --version | head -n1 | awk '{print $2}'", "/bin/sh"),
} }
return dewm return dewm
} else if processExists("i3") || processExists("i3-with-shmlog") { } else if processExists("i3") || processExists("i3-with-shmlog") {
dewm := DEWM{ dewm := DEWM{
Name: "i3", Name: "i3",
Type: "WM", Type: "WM",
Version: runCommand("i3 --version | awk '{print $3}'"), Version: runCommand("i3 --version | awk '{print $3}'", "/bin/sh"),
} }
return dewm return dewm
} else if processExists("sway") { } else if processExists("sway") {
dewm := DEWM{ dewm := DEWM{
Name: "Sway", Name: "Sway",
Type: "WM", Type: "WM",
Version: runCommand("sway --version | awk '{print $3}'"), Version: runCommand("sway --version | awk '{print $3}'", "/bin/sh"),
} }
if runCommand("sway --version | awk '{print $1}'") == "swayfx" { if runCommand("sway --version | awk '{print $1}'", "/bin/sh") == "swayfx" {
dewm.Name = "SwayFX" dewm.Name = "SwayFX"
} else { } else {
dewm.Name = "Sway" dewm.Name = "Sway"
@@ -165,21 +136,21 @@ func GetDEWM() DEWM {
dewm := DEWM{ dewm := DEWM{
Name: "Bspwm", Name: "Bspwm",
Type: "WM", Type: "WM",
Version: runCommand("bspwm -v"), Version: runCommand("bspwm -v", "/bin/sh"),
} }
return dewm return dewm
} else if processExists("Hyprland") { } else if processExists("Hyprland") {
dewm := DEWM{ dewm := DEWM{
Name: "Hyprland", Name: "Hyprland",
Type: "WM", Type: "WM",
Version: runCommand("hyprctl version | sed -n 3p | awk '{print $2}' | tr -d 'v,'"), Version: runCommand("hyprctl version | sed -n 3p | awk '{print $2}' | tr -d 'v,'", "/bin/sh"),
} }
return dewm return dewm
} else if processExists("icewm-session") { } else if processExists("icewm-session") {
dewm := DEWM{ dewm := DEWM{
Name: "IceWM", Name: "IceWM",
Type: "WM", Type: "WM",
Version: runCommand("icewm --version | awk '{print $2}'"), Version: runCommand("icewm --version | awk '{print $2}'", "/bin/sh"),
} }
return dewm return dewm
} }
+16
View File
@@ -4,6 +4,7 @@ import (
"fmt" "fmt"
"math" "math"
"os" "os"
"os/exec"
"regexp" "regexp"
"strings" "strings"
) )
@@ -56,3 +57,18 @@ func ReadKeyValueFile(filepath string) (map[string]string, error) {
} }
return ret, nil return ret, nil
} }
func runCommand(command string, shell string) string {
cmd := exec.Command(shell, "-c", command)
workdir, err := os.Getwd()
if err != nil {
return ""
}
cmd.Dir = workdir
cmd.Env = os.Environ()
out, err := cmd.Output()
if err != nil {
return ""
}
return strings.TrimSpace(string(out))
}