123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081 |
- package main
- import (
- "errors"
- "fmt"
- "os"
- "time"
- "github.com/pelletier/go-toml/v2"
- )
- type Config struct {
- // System Identification
- Name string // The name to refer to this BBS
- Sysop string // The name to identify the System Operator (SYSOP)
- // Paths
- DataPath string // Path to users.db, and various *.toml config files
- MessagePath string // Path to store *.db (for all message bases: local, NETWORKs)
- FilePath string // Path to store files (for both NETWORK-received, and local-received)
- LogPath string // Path to store *.log files (for lunarhub.log, and more)
- }
- func DefaultConfig() *Config {
- cfg := &Config{
- Name: "Lunar Hub",
- Sysop: "Sysop",
- DataPath: "data",
- MessagePath: "msgs",
- FilePath: "files",
- LogPath: "logs",
- }
- cfg.Save()
- return cfg
- }
- func LoadConfig() *Config {
- in, err := os.ReadFile("lunar_hub.toml")
- if err != nil {
- if errors.Is(err, os.ErrNotExist) {
- return DefaultConfig()
- } else {
- panic(fmt.Errorf("load config >> %v", err))
- }
- }
- var data map[string]any = map[string]any{}
- err = toml.Unmarshal(in, &data)
- if err != nil {
- return DefaultConfig()
- }
- paths := data["Paths"].(map[string]any)
- return &Config{
- Name: data["Name"].(string),
- Sysop: data["Sysop"].(string),
- DataPath: paths["Data"].(string),
- MessagePath: paths["Messages"].(string),
- FilePath: paths["Files"].(string),
- LogPath: paths["Logs"].(string),
- }
- }
- func (cfg *Config) Save() error {
- f, err := os.Create("lunar_hub.toml")
- if err != nil {
- return err
- }
- now := time.Now().Truncate(time.Second).String()
- f.WriteString("# Lunar Hub - Main Config\n")
- f.WriteString("# Generated: " + now + "\n\n")
- f.WriteString("Name = \"" + cfg.Name + "\"\n")
- f.WriteString("Sysop = \"" + cfg.Sysop + "\"\n\n")
- f.WriteString("[Paths]\n")
- f.WriteString("\"Data\" = \"" + cfg.DataPath + "\"\n")
- f.WriteString("\"Messages\" = \"" + cfg.MessagePath + "\"\n")
- f.WriteString("\"Files\" = \"" + cfg.FilePath + "\"\n")
- f.WriteString("\"Logs\" = \"" + cfg.LogPath + "\"\n")
- return f.Close()
- }
|