tgbot_misaka_5882f7/cfg.go

52 lines
962 B
Go

package main
import (
"os"
"strconv"
"strings"
"github.com/go-errors/errors"
"github.com/samber/lo"
)
type Config struct {
AdminUIDs map[int64]struct{}
TGBotToken string
WatchedInterface string
}
var config *Config
func LoadCfg() error {
cfg := Config{}
token := os.Getenv("TG_TOKEN")
if token == "" {
return errors.New("TG_TOKEN env not set")
}
cfg.TGBotToken = token
adminUIDsEnv := os.Getenv("TG_ADMIN_UIDS")
adminUIDs := lo.Filter(strings.Split(adminUIDsEnv, ","), func(s string, _ int) bool {
return strings.Trim(s, "\t ") != ""
})
cfg.AdminUIDs = make(map[int64]struct{}, len(adminUIDs))
for _, uidStr := range adminUIDs {
uid, err := strconv.ParseInt(uidStr, 10, 64)
if err != nil {
return errors.New("invalid admin UID: " + uidStr)
}
cfg.AdminUIDs[uid] = struct{}{}
}
iface := os.Getenv("TG_WATCHED_INTERFACE")
if iface == "" {
iface = "eth0"
}
cfg.WatchedInterface = iface
config = &cfg
return nil
}