package config
import (
"fmt"
"io/fs"
"net/url"
"path/filepath"
"time"
"alin.ovh/x/log"
"github.com/BurntSushi/toml"
"github.com/Southclaws/fault"
"github.com/Southclaws/fault/fmsg"
)
type Taxonomy struct {
Name string
Feed bool
}
type MenuItem struct {
Name string
URL URL `toml:"url"`
}
type URL struct {
*url.URL
}
type Timezone struct {
*time.Location
}
type GoPackagesConfig struct {
Domain URL
Forge URL
Packages []string
}
type Config struct {
Title string
Email string
PublicEmail string `toml:"public_email"`
Description string
BaseURL URL `toml:"base_url"`
OriginalDomain string `toml:"original_domain"`
DomainStartDate string `toml:"domain_start_date"`
GoatCounter URL `toml:"goatcounter"`
OIDCEmail string `toml:"oidc_email"`
OIDCHost URL `toml:"oidc_host"`
Domains []string
WildcardDomain string `toml:"wildcard_domain"`
Language string
Timezone Timezone
Taxonomies []Taxonomy
Menu []MenuItem
RelMe []MenuItem `toml:"rel_me"`
StaticFiles []string `toml:"static_files"`
Go GoPackagesConfig
}
func NewURL(rawURL string) URL {
u, err := url.Parse(rawURL)
if err != nil {
panic(err)
}
return URL{u}
}
func (u *URL) UnmarshalText(text []byte) (err error) {
u.URL, err = url.Parse(string(text))
return fault.Wrap(err, fmsg.With(fmt.Sprintf("could not parse URL %s", string(text))))
}
func (t *Timezone) UnmarshalText(text []byte) (err error) {
t.Location, err = time.LoadLocation(string(text))
return fault.Wrap(err, fmsg.With(fmt.Sprintf("could not parse timezone %s", string(text))))
}
func GetConfig(dir string, log *log.Logger) (*Config, error) {
config := &Config{}
filename := filepath.Join(dir, "config.toml")
log.Debug("reading config", "filename", filename)
_, err := toml.DecodeFile(filename, config)
if err != nil {
switch t := err.(type) {
case *fs.PathError:
return nil, fault.Wrap(t, fmsg.With("could not read configuration"))
case *toml.ParseError:
return nil, fault.Wrap(t, fmsg.With(t.ErrorWithUsage()))
}
return nil, fault.Wrap(err, fmsg.With("config error"))
}
if config.PublicEmail == "" {
config.PublicEmail = config.Email
}
if config.OIDCEmail == "" {
config.OIDCEmail = config.Email
}
return config, nil
}
shared/config/config.go (view raw)