diff --git a/config.go b/config.go index a873cec..31eee43 100644 --- a/config.go +++ b/config.go @@ -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. diff --git a/example/config.ini b/example/config.ini deleted file mode 100644 index b443d39..0000000 --- a/example/config.ini +++ /dev/null @@ -1,5 +0,0 @@ -title=Example Site -url=gemini://example.com https://example.com - -[feeds] -/blog/=Example Feed diff --git a/example/config.toml b/example/config.toml new file mode 100644 index 0000000..243a36d --- /dev/null +++ b/example/config.toml @@ -0,0 +1,5 @@ +title = "Example Site" +urls = ["gemini://example.com", "https://example.com"] + +[feeds] +"/blog/" = "Example Feed" diff --git a/go.mod b/go.mod index edae82d..c6b3dae 100644 --- a/go.mod +++ b/go.mod @@ -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 ) diff --git a/go.sum b/go.sum index 6b53722..1565b93 100644 --- a/go.sum +++ b/go.sum @@ -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= diff --git a/main.go b/main.go index 617710e..06d85ce 100644 --- a/main.go +++ b/main.go @@ -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 {