go rewrite

This commit is contained in:
raf 2024-01-11 22:51:07 +03:00
commit d4290ff553
No known key found for this signature in database
GPG key ID: 02D1DD3FA08B6B29
21 changed files with 437 additions and 552 deletions

View file

@ -0,0 +1,71 @@
package battery
import (
"os"
"strings"
"time"
"gomon/internal/exec"
"gomon/internal/logger"
"gomon/internal/model"
)
// battery monitor service
func Monitor(bat model.Battery, profiles ...string) error {
// wait a while if needed
startupWait := os.Getenv("STARTUP_WAIT")
if startupWait != "" {
duration, err := time.ParseDuration(startupWait)
if err != nil {
logger.Error("Invalid STARTUP_WAIT duration")
return nil
}
time.Sleep(duration)
}
// start the monitor loop
var prevProfile string
for {
// read the current state
batteryStatus, err := os.ReadFile(bat.Path + "/status")
if err != nil {
logger.Error("Failed to read battery status")
return nil
}
currentProfile := "performance"
if strings.TrimSpace(string(batteryStatus)) == "Discharging" {
currentProfile = "balanced"
}
// set the new profile
if currentProfile != prevProfile {
logger.Info("Setting power profile to %s for battery %s", currentProfile, bat.Path)
var commandArgs []string
if bat.Command != "" {
commandArgs = strings.Split(bat.Command, " ")
} else {
commandArgs = []string{"powerprofilesctl", "set", currentProfile}
}
err = exec.ExecCommand(commandArgs[0], commandArgs[1:]...)
if err != nil {
logger.Error("Failed to execute command")
return nil
}
// execute the extra command
// if any
if bat.ExtraCommand != "" {
extraCommandArgs := strings.Split(bat.ExtraCommand, " ")
err = exec.ExecCommand(extraCommandArgs[0], extraCommandArgs[1:]...)
if err != nil {
logger.Error("Failed to execute extra command")
return nil
}
}
}
prevProfile = currentProfile
// wait for the next power change event
time.Sleep(1 * time.Second)
}
}

29
internal/config/config.go Normal file
View file

@ -0,0 +1,29 @@
package config
import (
"encoding/json"
"os"
"gomon/internal/logger"
"gomon/internal/model"
)
// Load loads the battery configuration from a file.
func Load(filename string) (model.BatteryConfig, error) {
file, err := os.Open(filename)
if err != nil {
logger.Error("Failed to open file")
return model.BatteryConfig{}, err
}
defer file.Close()
var config model.BatteryConfig
decoder := json.NewDecoder(file)
err = decoder.Decode(&config)
if err != nil {
logger.Error("Failed to decode JSON")
return model.BatteryConfig{}, err
}
return config, nil
}

21
internal/exec/exec.go Normal file
View file

@ -0,0 +1,21 @@
package exec
import (
"os/exec"
"gomon/internal/logger"
)
// execute a command with args and returns its output
// this will be used primarily for executing external commands if configured
// or executing bash commands verbatim
func ExecCommand(name string, arg ...string) error {
cmd := exec.Command(name, arg...)
err := cmd.Run()
if err != nil {
logger.Error("Failed to execute command")
return err
}
return nil
}

24
internal/logger/logger.go Normal file
View file

@ -0,0 +1,24 @@
package logger
import (
"github.com/sirupsen/logrus"
)
var Log *logrus.Logger
func init() {
Log = logrus.New()
Log.SetFormatter(&logrus.JSONFormatter{})
}
func Info(format string, v ...interface{}) {
Log.Infof(format, v...)
}
func Error(format string, v ...interface{}) {
Log.Errorf(format, v...)
}
func Fatal(format string, v ...interface{}) {
Log.Fatalf(format, v...)
}

11
internal/model/model.go Normal file
View file

@ -0,0 +1,11 @@
package model
type Battery struct {
Path string `json:"path"`
Command string `json:"command"`
ExtraCommand string `json:"extraCommand"`
}
type BatteryConfig struct {
BatPaths []Battery `json:"batPaths"`
}