kiln/config.go
Adnan Maolood f6b2772c73 Revert "Switch to other toml library"
This reverts commit bf4ad9816e.
2021-03-20 20:35:48 -04:00

55 lines
1.1 KiB
Go

package main
import (
"os"
"text/template"
"github.com/pelletier/go-toml"
)
// Config contains site configuration.
type Config struct {
Title string `toml:"title"` // site title
URLs []string `toml:"urls"` // site URLs
Feeds map[string]string `toml:"feeds"` // site feeds
Templates *Templates `toml:"-"` // site templates
}
// 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 nil, err
}
defer f.Close()
if err := toml.NewDecoder(f).Decode(c); err != nil {
return nil, err
}
return c, nil
}
// LoadTemplates loads templates from the provided path.
func (c *Config) LoadTemplates(path string) error {
// Site contains site metadata passed to templates
type Site struct {
Title string
URLs []string
}
// Load templates
c.Templates = NewTemplates()
c.Templates.Funcs(template.FuncMap{
"site": func() Site {
return Site{
Title: c.Title,
URLs: c.URLs,
}
},
})
c.Templates.LoadDefault()
return c.Templates.Load(path)
}