forked from fasmide/deflux
-
Notifications
You must be signed in to change notification settings - Fork 2
/
main.go
48 lines (40 loc) · 1.23 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
package main
import (
"flag"
"fmt"
"github.com/fixje/deflux/pkg/config"
"github.com/fixje/deflux/pkg/deflux"
"log/slog"
"os"
)
func main() {
flagLoglevel := flag.String("loglevel", "warn", "debug | error | warn | info")
flagConfigGen := flag.Bool("config-gen", false, "generate a default config and print it on stdout")
flagConfig := flag.String("config", "", "specify the location of the config file (default: ./deflux.yml or /etc/deflux.yml)")
flagOnce := flag.Bool("1", false, "write sensor state from REST API once and exit")
flag.Parse()
initLogging(flagLoglevel)
if *flagConfigGen {
config.OutputDefaultConfiguration()
os.Exit(0)
}
cfg, err := config.LoadConfiguration(*flagConfig)
if err != nil {
slog.Error("No config file: %s", err)
os.Exit(deflux.ExitFailConfig)
}
if *flagOnce {
os.Exit(deflux.RunOnce(cfg))
}
os.Exit(deflux.RunWebsocket(cfg))
}
// initLogging initializes slog
func initLogging(flagLoglevel *string) {
var logLevel = new(slog.LevelVar)
err := logLevel.UnmarshalText([]byte(*flagLoglevel))
if err != nil {
fmt.Printf("Error parsing log level: %v", err)
logLevel.Set(slog.LevelWarn)
}
slog.SetDefault(slog.New(slog.NewTextHandler(os.Stderr, &slog.HandlerOptions{Level: logLevel})))
}