-
Notifications
You must be signed in to change notification settings - Fork 20
/
generate.go
54 lines (46 loc) · 1.36 KB
/
generate.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
package main
import (
"bufio"
"fmt"
"io/ioutil"
"os"
hue "github.com/collinux/gohue"
"gopkg.in/yaml.v2"
)
func generateConfig(outputFile *string) {
bridges, err := hue.FindBridges()
if err != nil {
panic(fmt.Sprintf("Error while searching for Hue bridges on the local network: %v\n", err))
}
if len(bridges) == 0 {
panic("No Hue bridges found on the local network.\n")
} else if len(bridges) == 1 {
fmt.Printf("Found 1 Hue bridge on the local network: %s (%v).\n", bridges[0].Info.Device.FriendlyName, bridges[0].IPAddress)
} else {
panic("Found multiple Hue bridges on the local network.\n")
}
bridge := bridges[0]
var apiKey string
for {
apiKey, err = bridge.CreateUser("hue_exporter")
if err != nil {
print("Creating API key failed. Have you pushed the link button on your hub?\nPress Enter to continue.")
bufio.NewReader(os.Stdin).ReadBytes('\n')
} else {
break
}
}
println("Successfully created API user.")
config, err := yaml.Marshal(Config{
IPAddr: bridge.IPAddress,
APIKey: apiKey,
})
if err != nil {
panic(fmt.Sprintf("Error while generating configuration file content: %v.\n", err))
}
err = ioutil.WriteFile(*outputFile, config, 0660)
if err != nil {
panic(fmt.Sprintf("Error while writing configuration file content to %s: %v.\n", *outputFile, err))
}
fmt.Printf("Configuration written to %s.", *outputFile)
}