-
Notifications
You must be signed in to change notification settings - Fork 2
/
main.go
155 lines (124 loc) · 3.36 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
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
package main
import (
"io"
"os"
badger "github.com/dgraph-io/badger/v2"
"github.com/dgraph-io/badger/v2/options"
log "github.com/sirupsen/logrus"
"github.com/spf13/pflag"
"github.com/spf13/viper"
"github.com/dj95/huekit/pkg/homekit"
"github.com/dj95/huekit/pkg/hue"
"github.com/dj95/huekit/pkg/store"
)
func init() {
// set the config name
viper.SetConfigName("config")
// add config paths
viper.AddConfigPath(".")
viper.AddConfigPath("/")
// add command line flags
initializeCommandFlags()
// override the config file when the commandline flag is set
if viper.IsSet("config") {
viper.SetConfigFile(viper.GetString("config"))
}
// set the env prefix to HUEKIT_ for configuration via
// environment variables
viper.SetEnvPrefix("HUEKIT")
// read the config file
if err := viper.ReadInConfig(); err != nil {
log.Warnf("Cannot read a config file. Trying to fetch config from env.")
}
// read the configuration from the environment and override
// the given values in the config file with it
viper.AutomaticEnv()
// set the default log level and mode
log.SetLevel(log.InfoLevel)
log.SetFormatter(&log.TextFormatter{})
// activate the debug mode
if viper.GetString("log_level") == "debug" {
log.SetLevel(log.DebugLevel)
}
// set the json formatter if configured
if viper.GetString("log_format") == "json" {
log.SetFormatter(&log.JSONFormatter{})
}
// open the io writer for the log file
file, err := os.OpenFile(
"huekit.log",
os.O_CREATE|os.O_WRONLY|os.O_APPEND,
0600,
)
// create the log output
logOutput := io.MultiWriter(os.Stdout, file)
// if no error occurred...
if err != nil {
logOutput = os.Stdout
log.Info("failed to log to file, using default stderr")
}
// set the stdout + file logger
log.SetOutput(logOutput)
}
func main() {
if viper.GetString("bridge_address") == "" || viper.GetString("homekit_pin") == "" {
log.Fatal("Invalid configuration! Either 'bridge_address' or 'homekit_pin' are missing!")
}
// open the database
db, err := badger.Open(
badger.
DefaultOptions("./huekit_data").
WithLogger(log.StandardLogger()).
WithValueLogLoadingMode(options.FileIO),
)
// error handling
if err != nil {
log.Fatal(err)
}
// close the database on exit
defer db.Close()
// create a new storage with the database as backend
store := store.NewBadger(db)
// create a new bridge connection and authenticate,
// if no authentication is saved in the storage
bridge, err := hue.NewBridge(
viper.GetString("bridge_address"),
store,
)
// error handling
if err != nil {
log.Fatal(err.Error())
}
// fetch all lights
lights, err := bridge.Lights()
// error handling
if err != nil {
log.Fatal(err.Error())
}
// iterate through all the lights
for _, light := range lights {
log.WithFields(log.Fields{
"id": light.ID,
"name": light.Name,
"type": light.Type,
"model": light.ModelID,
"software_version": light.SoftwareVersion,
}).Debug("found device")
}
homekit.StartBridge(
viper.GetString("homekit_pin"),
viper.GetString("homekit_port"),
lights,
bridge,
)
}
func initializeCommandFlags() {
// create a new flag for docker health checks
pflag.String("config", "", "choose the config file")
// parse the pflags
pflag.Parse()
// bind the pflags
if err := viper.BindPFlags(pflag.CommandLine); err != nil {
log.Fatal(err)
}
}