Implemented better config system
All checks were successful
continuous-integration/drone/push Build is passing
All checks were successful
continuous-integration/drone/push Build is passing
This commit is contained in:
6
integration/hue/config.go
Normal file
6
integration/hue/config.go
Normal file
@@ -0,0 +1,6 @@
|
||||
package hue
|
||||
|
||||
type Config struct {
|
||||
Token string `yaml:"token" envconfig:"HUE_TOKEN"`
|
||||
IP string `yaml:"ip" envconfig:"HUE_IP"`
|
||||
}
|
||||
@@ -5,7 +5,6 @@ import (
|
||||
"crypto/tls"
|
||||
"fmt"
|
||||
"net/http"
|
||||
"os"
|
||||
|
||||
"github.com/r3labs/sse/v2"
|
||||
)
|
||||
@@ -38,11 +37,8 @@ func (hue *Hue) SetFlag(id int, value bool) {
|
||||
}
|
||||
}
|
||||
|
||||
func Connect() Hue {
|
||||
login, _ := os.LookupEnv("HUE_BRIDGE")
|
||||
ip, _ := os.LookupEnv("HUE_IP")
|
||||
|
||||
hue := Hue{ip: ip, login: login, Events: make(chan *sse.Event)}
|
||||
func Connect(config Config) Hue {
|
||||
hue := Hue{ip: config.IP, login: config.Token, Events: make(chan *sse.Event)}
|
||||
|
||||
// Subscribe to eventstream
|
||||
client := sse.NewClient(fmt.Sprintf("https://%s/eventstream/clip/v2", hue.ip))
|
||||
|
||||
10
integration/mqtt/config.go
Normal file
10
integration/mqtt/config.go
Normal file
@@ -0,0 +1,10 @@
|
||||
package mqtt
|
||||
|
||||
type Config struct {
|
||||
Host string `yaml:"host" envconfig:"MQTT_HOST"`
|
||||
Port string `yaml:"port" envconfig:"MQTT_PORT"`
|
||||
Username string `yaml:"username" envconfig:"MQTT_USERNAME"`
|
||||
Password string `yaml:"password" envconfig:"MQTT_PASSWORD"`
|
||||
ClientID string `yaml:"client_id" envconfig:"MQTT_CLIENT_ID"`
|
||||
}
|
||||
|
||||
@@ -17,33 +17,12 @@ var defaultHandler mqtt.MessageHandler = func(client mqtt.Client, msg mqtt.Messa
|
||||
fmt.Printf("MSG: %s\n", msg.Payload())
|
||||
}
|
||||
|
||||
func Connect() MQTT {
|
||||
host, ok := os.LookupEnv("MQTT_HOST")
|
||||
if !ok {
|
||||
host = "localhost"
|
||||
}
|
||||
port, ok := os.LookupEnv("MQTT_PORT")
|
||||
if !ok {
|
||||
port = "1883"
|
||||
}
|
||||
user, ok := os.LookupEnv("MQTT_USER")
|
||||
if !ok {
|
||||
user = "test"
|
||||
}
|
||||
pass, ok := os.LookupEnv("MQTT_PASS")
|
||||
if !ok {
|
||||
pass = "test"
|
||||
}
|
||||
clientID, ok := os.LookupEnv("MQTT_CLIENT_ID")
|
||||
if !ok {
|
||||
clientID = "automation"
|
||||
}
|
||||
|
||||
opts := mqtt.NewClientOptions().AddBroker(fmt.Sprintf("%s:%s", host, port))
|
||||
opts.SetClientID(clientID)
|
||||
func Connect(config Config) MQTT {
|
||||
opts := mqtt.NewClientOptions().AddBroker(fmt.Sprintf("%s:%s", config.Host, config.Port))
|
||||
opts.SetClientID(config.ClientID)
|
||||
opts.SetDefaultPublishHandler(defaultHandler)
|
||||
opts.SetUsername(user)
|
||||
opts.SetPassword(pass)
|
||||
opts.SetUsername(config.Username)
|
||||
opts.SetPassword(config.Password)
|
||||
|
||||
client := mqtt.NewClient(opts)
|
||||
if token := client.Connect(); token.Wait() && token.Error() != nil {
|
||||
|
||||
5
integration/ntfy/config.go
Normal file
5
integration/ntfy/config.go
Normal file
@@ -0,0 +1,5 @@
|
||||
package ntfy
|
||||
|
||||
type Config struct {
|
||||
Presence string `yaml:"presence" envconfig:"NTFY_PRESENCE"`
|
||||
}
|
||||
@@ -3,12 +3,11 @@ package ntfy
|
||||
import (
|
||||
"fmt"
|
||||
"net/http"
|
||||
"os"
|
||||
"strings"
|
||||
)
|
||||
|
||||
type ntfy struct {
|
||||
topic string
|
||||
presence string
|
||||
}
|
||||
|
||||
func (ntfy *ntfy) Presence(home bool) {
|
||||
@@ -23,7 +22,7 @@ func (ntfy *ntfy) Presence(home bool) {
|
||||
actions = "broadcast, Set as home, extras.cmd=presence, extras.state=1, clear=true"
|
||||
}
|
||||
|
||||
req, err := http.NewRequest("POST", fmt.Sprintf("https://ntfy.sh/%s", ntfy.topic), strings.NewReader(description))
|
||||
req, err := http.NewRequest("POST", fmt.Sprintf("https://ntfy.sh/%s", ntfy.presence), strings.NewReader(description))
|
||||
if err != nil {
|
||||
panic(err)
|
||||
}
|
||||
@@ -36,9 +35,8 @@ func (ntfy *ntfy) Presence(home bool) {
|
||||
http.DefaultClient.Do(req)
|
||||
}
|
||||
|
||||
func Connect() ntfy {
|
||||
topic, _ := os.LookupEnv("NTFY_TOPIC")
|
||||
ntfy := ntfy{topic}
|
||||
func Connect(config Config) ntfy {
|
||||
ntfy := ntfy{presence: config.Presence}
|
||||
|
||||
// @TODO Make sure the topic is valid?
|
||||
|
||||
|
||||
Reference in New Issue
Block a user