config: Use toml for configuration

This commit is contained in:
Adnan Maolood 2021-03-20 02:02:36 -04:00
parent acfe42c25f
commit ce4b4406c7
6 changed files with 34 additions and 43 deletions

View file

@ -3,52 +3,41 @@ package main
import (
"mime"
"os"
"strings"
"text/template"
"git.sr.ht/~adnano/go-ini"
"github.com/pelletier/go-toml"
)
// Config contains site configuration.
type Config struct {
Title string // site title
URLs []string // site URLs
Feeds map[string]string // site feeds
Templates *Templates // site templates
Title string `toml:"title"` // site title
URLs []string `toml:"urls"` // site URLs
Feeds map[string]string `toml:"feeds"` // site feeds
Mediatypes map[string][]string `toml:"mediatypes"` // mediatypes
Templates *Templates `toml:"-"` // site templates
}
// NewConfig returns a new configuration.
func NewConfig() *Config {
return &Config{
Feeds: map[string]string{},
}
}
// Load loads the configuration from the provided path.
func (c *Config) Load(path string) error {
// LoadConfig loads the configuration from the provided path.
func LoadConfig(path string) (*Config, error) {
c := new(Config)
f, err := os.Open(path)
if err != nil {
return err
return nil, err
}
defer f.Close()
if err := toml.NewDecoder(f).Decode(c); err != nil {
return nil, err
}
// Load config
return ini.Parse(f, func(section, key, value string) {
switch section {
case "":
switch key {
case "title":
c.Title = value
case "url":
c.URLs = strings.Fields(value)
}
case "feeds":
c.Feeds[key] = value
case "mediatypes":
for _, field := range strings.Fields(value) {
mime.AddExtensionType(key, field)
// Register media types
for typ, exts := range c.Mediatypes {
for _, ext := range exts {
mime.AddExtensionType(typ, ext)
}
}
})
return c, nil
}
// LoadTemplates loads templates from the provided path.

View file

@ -1,5 +0,0 @@
title=Example Site
url=gemini://example.com https://example.com
[feeds]
/blog/=Example Feed

5
example/config.toml Normal file
View file

@ -0,0 +1,5 @@
title = "Example Site"
urls = ["gemini://example.com", "https://example.com"]
[feeds]
"/blog/" = "Example Feed"

2
go.mod
View file

@ -4,5 +4,5 @@ go 1.16
require (
git.sr.ht/~adnano/go-gemini v0.1.8
git.sr.ht/~adnano/go-ini v0.0.0-20201014024959-cc89f6531f0e
github.com/pelletier/go-toml v1.8.1
)

6
go.sum
View file

@ -1,4 +1,6 @@
git.sr.ht/~adnano/go-gemini v0.1.8 h1:93DxDNXB0bjnfDhZewf+QsEopfuOMh/I4v7ujoJ6WIs=
git.sr.ht/~adnano/go-gemini v0.1.8/go.mod h1:If1VxEWcZDrRt5FeAFnGTcM2Ud1E3BXs3VJ5rnZWKq0=
git.sr.ht/~adnano/go-ini v0.0.0-20201014024959-cc89f6531f0e h1:NRVtAaE3jvBKkwX/x7/yGeQKe8Agj13VDoKKOEE04aA=
git.sr.ht/~adnano/go-ini v0.0.0-20201014024959-cc89f6531f0e/go.mod h1:eysIkA6b8oivxACZfyG75+wlq6BW6t5qPVnx8IcFYlE=
github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c=
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/pelletier/go-toml v1.8.1 h1:1Nf83orprkJyknT6h7zbuEGUEjcyVlCxSUGTENmNCRM=
github.com/pelletier/go-toml v1.8.1/go.mod h1:T2/BmBdy8dvIRq1a/8aqjN41wvWlN4lrapLU/GW4pbc=

View file

@ -17,8 +17,8 @@ func run() error {
flag.Parse()
// Load config
cfg := NewConfig()
if err := cfg.Load("config.ini"); err != nil {
cfg, err := LoadConfig("config.toml")
if err != nil {
return err
}
if err := cfg.LoadTemplates("layouts"); err != nil {