config: Use toml for configuration

This commit is contained in:
adnano 2021-03-20 02:02:36 -04:00
parent fd187da71d
commit c7adb5fef1
6 changed files with 34 additions and 43 deletions

View file

@ -3,52 +3,41 @@ package main
import ( import (
"mime" "mime"
"os" "os"
"strings"
"text/template" "text/template"
"git.sr.ht/~adnano/go-ini" "github.com/pelletier/go-toml"
) )
// Config contains site configuration. // Config contains site configuration.
type Config struct { type Config struct {
Title string // site title Title string `toml:"title"` // site title
URLs []string // site URLs URLs []string `toml:"urls"` // site URLs
Feeds map[string]string // site feeds Feeds map[string]string `toml:"feeds"` // site feeds
Templates *Templates // site templates Mediatypes map[string][]string `toml:"mediatypes"` // mediatypes
Templates *Templates `toml:"-"` // site templates
} }
// NewConfig returns a new configuration. // LoadConfig loads the configuration from the provided path.
func NewConfig() *Config { func LoadConfig(path string) (*Config, error) {
return &Config{ c := new(Config)
Feeds: map[string]string{},
}
}
// Load loads the configuration from the provided path.
func (c *Config) Load(path string) error {
f, err := os.Open(path) f, err := os.Open(path)
if err != nil { 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 // Register media types
return ini.Parse(f, func(section, key, value string) { for typ, exts := range c.Mediatypes {
switch section { for _, ext := range exts {
case "": mime.AddExtensionType(typ, ext)
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)
} }
} }
})
return c, nil
} }
// LoadTemplates loads templates from the provided path. // 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 ( require (
git.sr.ht/~adnano/go-gemini v0.1.8 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 h1:93DxDNXB0bjnfDhZewf+QsEopfuOMh/I4v7ujoJ6WIs=
git.sr.ht/~adnano/go-gemini v0.1.8/go.mod h1:If1VxEWcZDrRt5FeAFnGTcM2Ud1E3BXs3VJ5rnZWKq0= 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= github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c=
git.sr.ht/~adnano/go-ini v0.0.0-20201014024959-cc89f6531f0e/go.mod h1:eysIkA6b8oivxACZfyG75+wlq6BW6t5qPVnx8IcFYlE= 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() flag.Parse()
// Load config // Load config
cfg := NewConfig() cfg, err := LoadConfig("config.toml")
if err := cfg.Load("config.ini"); err != nil { if err != nil {
return err return err
} }
if err := cfg.LoadTemplates("layouts"); err != nil { if err := cfg.LoadTemplates("layouts"); err != nil {