-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
60 lines (51 loc) · 1.38 KB
/
main.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
package main
import (
"sync"
"github.com/sirupsen/logrus"
"github.com/spf13/cobra"
"github.com/spf13/viper"
"batmon/internal/battery"
"batmon/internal/config"
"batmon/internal/model"
)
func main() {
var rootCmd = &cobra.Command{
Use: "batmon",
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")
}
}