Files
rpi5-health/internal/cli/cli.go
T
2026-10-06 02:24:44 +02:00

80 lines
2.0 KiB
Go

package cli
import (
"bufio"
"fmt"
"log"
"os"
loadaverage "rpi5-health/internal/load_average"
"rpi5-health/internal/memory"
"rpi5-health/internal/processor"
"rpi5-health/internal/temperature"
"rpi5-health/internal/uptime"
"strings"
)
func Listen() {
reader := bufio.NewScanner(os.Stdin)
for {
fmt.Print("\n> ")
if !reader.Scan() {
log.Println("Program terminated (SIGINT)")
return
}
if err := reader.Err(); err != nil {
log.Printf("Console input error: %v\n", err)
return
}
text := strings.TrimSpace(reader.Text())
switch text {
case "exit":
return
case "help":
fmt.Print(`
exit - Exit the program
meminfo - Print memory information
cpuinfo - Print cpu information
uptime - Print system uptime
loadavg - Print cpu load average information`)
fmt.Println()
case "meminfo":
memInfo, _ := memory.GetRamInformation()
const GB = 1024 * 1024 * 1024
fmt.Printf("Total memory: %.2fGB\n", float64(memInfo.SystemMemory)/GB)
fmt.Printf("Available memory: %.2fGB\n", float64(memInfo.AvailableMemory)/GB)
fmt.Printf("Used memory: %.2fGB\n", float64(memInfo.GetUsedMemory())/GB)
case "cpuinfo":
_, kernel, usage, idle := processor.GetCPUInformation().Usage()
fmt.Printf("Total time spent idling: %.2f%%\n", idle)
fmt.Printf("Total time spent in kernel mode: %.2f%%\n", kernel)
fmt.Printf("Total time spent in user mode: %.2f%%\n", usage)
case "uptime":
uptime, _ := uptime.GetUptimeInformation()
fmt.Printf("Total uptime: %s\n", uptime.FormatSeconds())
case "loadavg":
loadAvg, _ := loadaverage.GetLoadAverage()
fmt.Printf("Load average: L1: %.2f | L5: %.2f | L15: %.2f\n", loadAvg.Load1, loadAvg.Load5, loadAvg.Load15)
case "temperature":
temp, _ := temperature.GetTemperatureInformation()
if temp.Supported {
fmt.Printf("Current temperature: %.2fC°", temp.Celsius)
} else {
fmt.Printf("Temperature reading is unsupported. Try installing Libre Hardware Monitor, or running as Administrator.")
}
}
}
}