2024-10-19 01:21:50 +00:00
|
|
|
package config
|
|
|
|
|
|
|
|
import (
|
|
|
|
"encoding/json"
|
|
|
|
"os"
|
2024-10-21 04:51:17 +00:00
|
|
|
"strings"
|
2024-10-20 20:00:50 +00:00
|
|
|
"time"
|
2024-10-19 01:21:50 +00:00
|
|
|
|
|
|
|
"github.com/sirupsen/logrus"
|
2024-10-21 01:38:35 +00:00
|
|
|
"maunium.net/go/mautrix/id"
|
2024-10-19 01:21:50 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
type Config struct {
|
|
|
|
Meshtastic Meshtastic
|
2024-10-21 01:38:35 +00:00
|
|
|
Matrix Matrix
|
|
|
|
Database string
|
2024-10-19 01:21:50 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
type Meshtastic struct {
|
2024-10-20 20:00:50 +00:00
|
|
|
Address string
|
|
|
|
RequestTimeout time.Duration
|
|
|
|
PollingInterval time.Duration
|
2024-10-19 01:21:50 +00:00
|
|
|
}
|
|
|
|
|
2024-10-21 01:38:35 +00:00
|
|
|
type Matrix struct {
|
|
|
|
User id.UserID
|
|
|
|
Password string
|
|
|
|
Room id.RoomID
|
|
|
|
}
|
|
|
|
|
2024-10-20 20:00:50 +00:00
|
|
|
var C = Config{
|
2024-10-21 04:44:55 +00:00
|
|
|
Database: "matrix-bridge-meshtastic.db",
|
2024-10-20 20:00:50 +00:00
|
|
|
Meshtastic: Meshtastic{
|
|
|
|
RequestTimeout: time.Second * 5,
|
|
|
|
PollingInterval: time.Millisecond * 500,
|
|
|
|
},
|
|
|
|
}
|
2024-10-19 01:21:50 +00:00
|
|
|
|
2024-10-21 04:51:17 +00:00
|
|
|
var defaultConfigFiles = []string{"/etc/matrix-bridge-meshtastic.json", "matrix-bridge-meshtastic.json"}
|
|
|
|
|
2024-10-19 01:21:50 +00:00
|
|
|
func Load() error {
|
2024-10-21 04:51:17 +00:00
|
|
|
configFiles := defaultConfigFiles
|
|
|
|
|
|
|
|
envConfigFiles := os.Getenv("MATRIX_BRIDGE_MESHTASTIC_CONFIG")
|
|
|
|
if envConfigFiles != "" {
|
|
|
|
configFiles = strings.Split(envConfigFiles, ",")
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, filename := range configFiles {
|
2024-10-21 04:44:55 +00:00
|
|
|
err := load(filename)
|
|
|
|
if err != nil && !os.IsNotExist(err) {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-10-21 04:54:22 +00:00
|
|
|
// someday i'll bring a proper config library, until then you get this
|
|
|
|
matrixPassword := os.Getenv("MATRIX_PASSWORD")
|
|
|
|
if matrixPassword != "" {
|
|
|
|
C.Matrix.Password = matrixPassword
|
|
|
|
}
|
|
|
|
|
2024-10-21 04:44:55 +00:00
|
|
|
return nil
|
2024-10-19 01:21:50 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func load(filename string) error {
|
|
|
|
logrus.WithField("file", filename).Info("reading config file")
|
|
|
|
|
|
|
|
f, err := os.Open(filename)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
defer f.Close()
|
|
|
|
|
|
|
|
err = json.NewDecoder(f).Decode(&C)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|