hibiscus/config.go

155 lines
3.4 KiB
Go
Raw Normal View History

2024-03-17 17:12:41 +03:00
package main
import (
"bufio"
"errors"
2024-03-18 15:22:50 +03:00
"fmt"
2024-03-17 17:12:41 +03:00
"log"
2024-05-04 15:39:15 +03:00
"log/slog"
2024-03-17 17:12:41 +03:00
"os"
2024-03-23 15:36:01 +03:00
"reflect"
2024-03-17 17:12:41 +03:00
"strconv"
"strings"
2024-04-14 11:45:42 +03:00
"time"
2024-03-17 17:12:41 +03:00
)
2024-03-18 15:22:50 +03:00
var ConfigFile = "config/config.txt"
2024-03-17 17:12:41 +03:00
type Config struct {
2024-05-03 16:48:17 +03:00
Username string `config:"username" type:"string"`
Password string `config:"password" type:"string"`
Port int `config:"port" type:"int"`
Timezone *time.Location `config:"timezone" type:"location"`
2024-05-05 13:06:20 +03:00
GraceTime time.Duration `config:"grace_period" type:"duration"`
2024-05-03 16:48:17 +03:00
Language string `config:"language" type:"string"`
2024-05-08 13:12:10 +03:00
Theme string `config:"theme" type:"string"`
2024-05-03 16:48:17 +03:00
LogToFile bool `config:"log_to_file" type:"bool"`
2024-05-04 15:39:15 +03:00
LogFile string `config:"log_file" type:"string"`
2024-05-03 16:48:17 +03:00
Scram bool `config:"enable_scram" type:"bool"`
2024-03-20 16:18:23 +03:00
2024-05-03 16:48:17 +03:00
TelegramToken string `config:"tg_token" type:"string"`
TelegramChat string `config:"tg_chat" type:"string"`
2024-03-17 17:12:41 +03:00
}
2024-03-18 15:22:50 +03:00
func (c *Config) Save() error {
2024-03-23 15:36:01 +03:00
output := ""
v := reflect.ValueOf(*c)
typeOfS := v.Type()
for i := 0; i < v.NumField(); i++ {
key := typeOfS.Field(i).Tag.Get("config")
value := v.Field(i).Interface()
if value != "" {
output += fmt.Sprintf("%s=%v\n", key, value)
}
2024-03-20 16:18:23 +03:00
}
2024-03-17 17:12:41 +03:00
2024-03-18 15:22:50 +03:00
f, err := os.OpenFile(ConfigFile, os.O_CREATE|os.O_WRONLY, 0644)
if err != nil {
return err
}
if _, err := f.Write([]byte(output)); err != nil {
return err
2024-03-17 17:12:41 +03:00
}
2024-03-18 15:22:50 +03:00
return nil
}
2024-03-17 17:12:41 +03:00
2024-03-20 00:02:22 +03:00
func (c *Config) Reload() error {
2024-03-18 15:22:50 +03:00
if _, err := os.Stat(ConfigFile); errors.Is(err, os.ErrNotExist) {
2024-03-20 00:02:22 +03:00
err := c.Save()
2024-03-18 15:22:50 +03:00
if err != nil {
2024-03-20 00:02:22 +03:00
return err
2024-03-18 15:22:50 +03:00
}
2024-03-20 00:02:22 +03:00
return nil
2024-03-18 15:22:50 +03:00
}
file, err := os.Open(ConfigFile)
2024-03-17 17:12:41 +03:00
if err != nil {
2024-03-20 00:02:22 +03:00
return err
2024-03-17 17:12:41 +03:00
}
2024-05-03 16:48:17 +03:00
options := map[string]string{}
2024-03-17 17:12:41 +03:00
scanner := bufio.NewScanner(file)
for scanner.Scan() {
entry := strings.Split(strings.Replace(scanner.Text(), " ", "", -1), "=")
if len(entry) != 2 {
continue
}
2024-05-03 16:48:17 +03:00
options[entry[0]] = entry[1]
2024-03-17 17:12:41 +03:00
}
if err := scanner.Err(); err != nil {
2024-03-20 00:02:22 +03:00
return err
2024-03-17 17:12:41 +03:00
}
2024-05-04 21:36:43 +03:00
err = file.Close()
if err != nil {
return err
}
2024-03-17 17:12:41 +03:00
2024-05-03 16:48:17 +03:00
timezone := "Local" // Timezone is handled separately because reflection
refStruct := reflect.ValueOf(*c)
refElem := reflect.ValueOf(&c).Elem()
typeOfS := refStruct.Type()
for i := 0; i < refStruct.NumField(); i++ {
fieldElem := reflect.Indirect(refElem).Field(i)
key := typeOfS.Field(i).Tag.Get("config")
if v, ok := options[key]; ok && fieldElem.CanSet() {
switch typeOfS.Field(i).Tag.Get("type") {
case "int":
{
numVal, err := strconv.Atoi(v)
if err == nil {
fieldElem.SetInt(int64(numVal))
}
}
case "bool":
{
if v == "true" {
fieldElem.SetBool(true)
} else {
fieldElem.SetBool(false)
}
}
case "location":
timezone = v
2024-05-05 13:06:20 +03:00
case "duration":
{
numVal, err := time.ParseDuration(v)
if err == nil {
fieldElem.SetInt(int64(numVal))
}
}
2024-05-03 16:48:17 +03:00
default:
fieldElem.SetString(v)
}
}
}
loc, err := time.LoadLocation(timezone)
if err != nil {
c.Timezone = time.Local
} else {
c.Timezone = loc
}
2024-05-04 15:39:15 +03:00
slog.Debug("reloaded config", "config", c)
2024-05-03 16:48:17 +03:00
return LoadLanguage(c.Language) // Load selected language
2024-03-20 00:02:22 +03:00
}
2024-03-28 10:42:52 +03:00
// ConfigInit loads config on startup
2024-05-04 15:39:15 +03:00
// Some defaults are declared here
2024-03-20 00:02:22 +03:00
func ConfigInit() Config {
2024-05-04 15:39:15 +03:00
cfg := Config{
2024-05-05 13:06:20 +03:00
Port: 7101,
Username: "admin",
Password: "admin",
Timezone: time.Local,
Language: "en",
2024-05-08 13:12:10 +03:00
Theme: "default",
2024-05-05 13:06:20 +03:00
LogFile: "config/log.txt",
GraceTime: 0,
2024-05-04 15:39:15 +03:00
}
2024-03-20 00:02:22 +03:00
err := cfg.Reload()
if err != nil {
log.Fatal(err)
}
return cfg
2024-03-17 17:12:41 +03:00
}