config: Create default configuration before parsing

This commit is contained in:
adnano 2021-03-20 23:57:16 -04:00
parent 713c1336fb
commit c966e9f815

View file

@ -35,33 +35,33 @@ func (t Task) Format(p *Page) (string, []byte) {
return t.postProcess.Format(p) return t.postProcess.Format(p)
} }
// LoadConfig loads the configuration from the provided path. // DefaultConfig returns the default configuration.
func LoadConfig(path string) (*Config, error) { func DefaultConfig() *Config {
c := new(Config) c := new(Config)
c.Feeds = make(map[string]string) c.Feeds = make(map[string]string)
c.Tasks = make(map[string]*Task) c.Tasks = make(map[string]*Task)
c.Tasks["gemini"] = &Task{
Input: ".gmi",
Output: ".gmi",
Template: ".gmi",
Destination: "public",
}
return c
}
// LoadConfig loads the configuration from the provided path.
func LoadConfig(path string) (*Config, error) {
f, err := os.Open(path) f, err := os.Open(path)
if err != nil { if err != nil {
return nil, err return nil, err
} }
defer f.Close() defer f.Close()
c := DefaultConfig()
if _, err := toml.DecodeReader(f, c); err != nil { if _, err := toml.DecodeReader(f, c); err != nil {
return nil, err return nil, err
} }
// Add default task
// The toml library overwrites the map, so add default values after parsing
if _, ok := c.Tasks["gemini"]; !ok {
c.Tasks["gemini"] = &Task{
Input: ".gmi",
Output: ".gmi",
Template: ".gmi",
Destination: "public",
}
}
for _, task := range c.Tasks { for _, task := range c.Tasks {
switch task.PostProcess { switch task.PostProcess {
case "": case "":