-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
90 lines (74 loc) · 1.81 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
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
package main
import (
"fmt"
"github.com/leafney/rose"
"github.com/leafney/rpi-monitor/config"
"github.com/leafney/rpi-monitor/pkg/mqt"
flag "github.com/spf13/pflag"
"github.com/spf13/viper"
"runtime"
)
var (
//configFile *string
v bool
h bool
Version = "0.1.0"
GitBranch = ""
GitCommit = ""
BuildTime = "2023-01-08 23:37:48"
)
func main() {
//configFile = flag.StringP("config", "f", "config.yaml", "the config file")
flag.BoolVarP(&h, "help", "h", false, "help")
flag.BoolVarP(&v, "version", "v", false, "version")
flag.Parse()
if h {
flag.PrintDefaults()
} else if v {
// 输出版本信息
fmt.Println("Version: " + Version)
fmt.Println("Git branch: " + GitBranch)
fmt.Println("Git commit: " + GitCommit)
fmt.Println("Built time: " + BuildTime)
fmt.Println("Go version: " + runtime.Version())
fmt.Println("OS/Arch: " + runtime.GOOS + "/" + runtime.GOARCH)
} else {
start()
}
}
func start() {
// set default config
viper.SetDefault("mqtt", map[string]interface{}{
"host": "127.0.0.1",
"port": "1883",
"topic": "",
"username": "",
"password": "",
"qos": 0})
// load config file
viper.SetConfigName("monitor")
viper.SetConfigType("yaml")
viper.AddConfigPath("/etc/monitor/")
viper.AddConfigPath("$HOME/.monitor")
viper.AddConfigPath(".")
// use env config
viper.AutomaticEnv()
viper.SetEnvPrefix("monitor")
// config change
//viper.WatchConfig()
//viper.OnConfigChange(func(e fsnotify.Event) {
// fmt.Println("Config file changed:", e.Name)
//})
if err := viper.ReadInConfig(); err != nil {
panic(err)
}
var cfg config.Config
if err := viper.Unmarshal(&cfg); err != nil {
panic(err)
}
fmt.Println(rose.JsonMarshalStr(&cfg))
// mqtt
mqt.GetMQTTClient(cfg.MQTT)
fmt.Println("starting...")
select {}
}