go rewrite
This commit is contained in:
parent
3a9d220140
commit
d4290ff553
21 changed files with 437 additions and 552 deletions
60
main.go
Normal file
60
main.go
Normal file
|
@ -0,0 +1,60 @@
|
|||
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")
|
||||
}
|
||||
}
|
Reference in a new issue