Files
Gregor Lohaus 8254a28baa init
2026-04-08 04:29:35 +02:00

82 lines
1.3 KiB
Go

package config
import (
"github.com/adrg/xdg"
"github.com/pelletier/go-toml"
"os"
"path/filepath"
)
var confPath string
var Conf *Config
var DefaultConf *Config
func init() {
confPath = filepath.Join(xdg.ConfigHome, "<@var(context.project.name)>", "config.toml")
Conf = &Config{}
DefaultConf = &Config{
Server: Server{
Host: "127.0.0.1",
Port: 8080,
FrontendUrls: []string{"http://localhost:5173"},
},
Database: Database{
User: "pp",
Password: "<@var(context.project.name)>",
Host: "127.0.0.1",
Port: 5432,
Name: "<@var(context.project.name)>",
},
}
}
func Load() error {
confContent, err := os.ReadFile(confPath)
if err != nil {
return err
}
return toml.Unmarshal(confContent, Conf)
}
func Write(config Config) error {
confContent, err := toml.Marshal(config)
if err != nil {
return err
}
dir := filepath.Dir(confPath)
if err := os.MkdirAll(dir, 0755); err != nil {
return err
}
if err := os.WriteFile(confPath, confContent, 0644); err != nil {
return err
}
if err := Load(); err != nil {
return err
}
return nil
}
type Server struct {
Host string
Port int
FrontendUrls []string
}
type Database struct {
User string
Password string
Host string
Port int
Name string
}
type Config struct {
Server Server
Database Database
}