matrix-bridge-meshtastic/config/config.go

81 lines
1.4 KiB
Go
Raw Normal View History

package config
import (
"encoding/json"
"os"
2024-10-21 04:51:17 +00:00
"strings"
2024-10-20 20:00:50 +00:00
"time"
"github.com/sirupsen/logrus"
"maunium.net/go/mautrix/id"
)
type Config struct {
Meshtastic Meshtastic
Matrix Matrix
Database string
}
type Meshtastic struct {
2024-10-20 20:00:50 +00:00
Address string
RequestTimeout time.Duration
PollingInterval time.Duration
}
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-21 04:51:17 +00:00
var defaultConfigFiles = []string{"/etc/matrix-bridge-meshtastic.json", "matrix-bridge-meshtastic.json"}
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
}
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
}