make each reader func return a func, as well as add an exec reader.

This commit is contained in:
frosty 2024-08-20 21:58:40 +00:00
parent 781b804a33
commit 4653550119
9 changed files with 119 additions and 17 deletions

View file

@ -30,16 +30,23 @@ var (
var modules = []Module{
{
Func: readers.ReadUptime,
Func: readers.ReadExec("statusbar cpu"),
Interval: 5 * time.Second,
},
{
Func: readers.ReadExec("statusbar volume"),
Signal: 1,
},
{
Func: readers.ReadExec("statusbar battery"),
Interval: 60 * time.Second,
},
{
Func: readers.ReadExec("statusbar date"),
Interval: 1 * time.Second,
},
{
Func: readers.ReadLoad,
Func: readers.ReadExec("statusbar loadavg"),
Interval: 5 * time.Second,
},
{
Func: readers.ReadMemory,
Interval: 5 * time.Second,
Template: "{{.UsedPretty}} / {{.TotalPretty}}",
},
}

View file

@ -116,7 +116,7 @@ func BatteryTechnologyFromStr(technologyStr string) BatteryTechnology {
return TechnologyUnknown
}
func ReadBattery(batteryName string) (interface{}, error) {
func readBattery(batteryName string) (interface{}, error) {
capacityPath := fmt.Sprintf("/sys/class/power_supply/%s/capacity", batteryName)
statusPath := fmt.Sprintf("/sys/class/power_supply/%s/status", batteryName)
technologyPath := fmt.Sprintf("/sys/class/power_supply/%s/technology", batteryName)
@ -169,3 +169,9 @@ func ReadBattery(batteryName string) (interface{}, error) {
Technology: BatteryTechnologyFromStr(batteryTechnology),
}, nil
}
func ReadBattery(batteryName string) func() (interface{}, error) {
return func() (interface{}, error) {
return readBattery(batteryName)
}
}

View file

@ -25,7 +25,7 @@ import (
type CpuTemperatureInfo float32
func ReadCpuTemperature(hwmonName, tempName string) (interface{}, error) {
func readCpuTemperature(hwmonName, tempName string) (interface{}, error) {
tempPath := fmt.Sprintf("/sys/class/hwmon/%s/%s_input", hwmonName, tempName)
file, err := os.Open(tempPath)
@ -49,3 +49,9 @@ func ReadCpuTemperature(hwmonName, tempName string) (interface{}, error) {
return CpuTemperatureInfo(cpuTemperature), nil
}
func ReadCpuTemperature(hwmonName, tempName string) func() (interface{}, error) {
return func() (interface{}, error) {
return readCpuTemperature(hwmonName, tempName)
}
}

View file

@ -34,7 +34,7 @@ func (cu CpuUsageInfo) String() string {
return fmt.Sprintf("%d%%", uint(cu.UsagePercent))
}
func ReadCpuUsage() (interface{}, error) {
func readCpuUsage() (interface{}, error) {
file, err := os.Open("/proc/stat")
if err != nil {
return CpuUsageInfo{}, err
@ -81,3 +81,9 @@ func ReadCpuUsage() (interface{}, error) {
UsagePercent: float64(cpuInUse) * 100 / float64(cpuTotal),
}, nil
}
func ReadCpuUsage() func() (interface{}, error) {
return func() (interface{}, error) {
return readCpuUsage()
}
}

53
lib/readers/exec.go Normal file
View file

@ -0,0 +1,53 @@
// modbot is a system information agregator
// Copyright (C) 2024 frosty <inthishouseofcards@gmail.com>
//
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU Affero General Public License as published
// by the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU Affero General Public License for more details.
//
// You should have received a copy of the GNU Affero General Public License
// along with this program. If not, see <https://www.gnu.org/licenses/>.
package readers
import (
"bytes"
"os/exec"
"strings"
)
type ExecInfo string
func readExec(command string) (interface{}, error) {
args := []string{"sh", "-c", command}
var stdout, stderr bytes.Buffer
cmd := exec.Command(args[0], args[1:]...)
cmd.Stdout = &stdout
cmd.Stderr = &stderr
if err := cmd.Run(); err != nil {
return ExecInfo(""), err
}
outputLines := strings.Split(stdout.String(), "\n")
if len(outputLines) == 0 {
return ExecInfo(""), nil
}
outputString := outputLines[0]
return ExecInfo(outputString), nil
}
func ReadExec(command string) func() (interface{}, error) {
return func() (interface{}, error) {
return readExec(command)
}
}

View file

@ -33,7 +33,7 @@ func (l LoadInfo) String() string {
return fmt.Sprintf("%v %v %v", l.OneMinute, l.FiveMinute, l.FifteenMinute)
}
func ReadLoad() (interface{}, error) {
func readLoad() (interface{}, error) {
const loadPath = "/proc/loadavg"
file, err := os.Open(loadPath)
@ -60,3 +60,9 @@ func ReadLoad() (interface{}, error) {
FifteenMinute: fields[2],
}, nil
}
func ReadLoad() func() (interface{}, error) {
return func() (interface{}, error) {
return readLoad()
}
}

View file

@ -37,7 +37,7 @@ type MemoryInfo struct {
UsedPretty string
}
func ReadMemory() (interface{}, error) {
func readMemory() (interface{}, error) {
file, err := os.Open("/proc/meminfo")
if err != nil {
return MemoryInfo{}, err
@ -89,3 +89,9 @@ func ReadMemory() (interface{}, error) {
UsedPretty: ui.PrettifyKib(memUsed, 2),
}, nil
}
func ReadMemory() func() (interface{}, error) {
return func() (interface{}, error) {
return readMemory()
}
}

View file

@ -29,7 +29,7 @@ type OsInfo struct {
Version string
}
func ReadOs() (interface{}, error) {
func readOs() (interface{}, error) {
const osPath = "/lib/os-release"
file, err := os.Open(osPath)
@ -70,3 +70,9 @@ func ReadOs() (interface{}, error) {
Version: osVersion,
}, nil
}
func ReadOs() func() (interface{}, error) {
return func() (interface{}, error) {
return readOs()
}
}

View file

@ -65,7 +65,7 @@ func (u UptimeInfo) String() string {
return builder.String()
}
func ReadUptime() (interface{}, error) {
func readUptime() (interface{}, error) {
const uptimePath = "/proc/uptime"
file, err := os.Open(uptimePath)
@ -92,3 +92,9 @@ func ReadUptime() (interface{}, error) {
return UptimeInfo(uptime), nil
}
func ReadUptime() func() (interface{}, error) {
return func() (interface{}, error) {
return readUptime()
}
}