mirror of
https://github.com/NotAShelf/batmon.git
synced 2024-11-26 15:16:47 +00:00
61 lines
1.4 KiB
Go
61 lines
1.4 KiB
Go
|
package main
|
||
|
|
||
|
import (
|
||
|
"sync"
|
||
|
|
||
|
"github.com/sirupsen/logrus"
|
||
|
"github.com/spf13/cobra"
|
||
|
"github.com/spf13/viper"
|
||
|
|
||
|
"gomon/internal/battery"
|
||
|
"gomon/internal/config"
|
||
|
"gomon/internal/model"
|
||
|
)
|
||
|
|
||
|
func main() {
|
||
|
var rootCmd = &cobra.Command{
|
||
|
Use: "gomon",
|
||
|
Short: "TODO",
|
||
|
Long: `TODO`,
|
||
|
Run: func(cmd *cobra.Command, args []string) {
|
||
|
// load the battery configuration
|
||
|
config, err := config.Load(viper.GetString("config"))
|
||
|
if err != nil {
|
||
|
logrus.WithFields(logrus.Fields{
|
||
|
"error": err,
|
||
|
}).Fatal("Failed to load configuration")
|
||
|
}
|
||
|
|
||
|
// create a WaitGroup to wait for all goroutines to finish
|
||
|
var wg sync.WaitGroup
|
||
|
wg.Add(len(config.BatPaths))
|
||
|
|
||
|
// start a goroutine for each battery
|
||
|
for _, bat := range config.BatPaths {
|
||
|
go func(bat model.Battery) {
|
||
|
defer wg.Done()
|
||
|
err := battery.Monitor(bat)
|
||
|
if err != nil {
|
||
|
logrus.WithFields(logrus.Fields{
|
||
|
"error": err,
|
||
|
"battery": bat.Path,
|
||
|
}).Error("Failed to monitor battery")
|
||
|
}
|
||
|
}(bat)
|
||
|
}
|
||
|
|
||
|
// wait for all goroutines to finish
|
||
|
wg.Wait()
|
||
|
},
|
||
|
}
|
||
|
|
||
|
rootCmd.PersistentFlags().StringP("config", "c", "config.json", "Path to the configuration file")
|
||
|
viper.BindPFlag("config", rootCmd.PersistentFlags().Lookup("config"))
|
||
|
|
||
|
if err := rootCmd.Execute(); err != nil {
|
||
|
logrus.WithFields(logrus.Fields{
|
||
|
"error": err,
|
||
|
}).Fatal("Failed to execute root command")
|
||
|
}
|
||
|
}
|