mirror of
https://github.com/jhillyerd/inbucket.git
synced 2025-12-18 18:17:03 +00:00
70 lines
2.0 KiB
Go
70 lines
2.0 KiB
Go
package inbucket
|
|
|
|
import (
|
|
"container/list"
|
|
"fmt"
|
|
"github.com/robfig/goconfig/config"
|
|
"os"
|
|
)
|
|
|
|
var Config *config.Config
|
|
|
|
// LoadConfig loads the specified configuration file into inbucket.Config
|
|
// and performs validations on it.
|
|
func LoadConfig(filename string) error {
|
|
var err error
|
|
Config, err = config.ReadDefault(filename)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
|
|
messages := list.New()
|
|
|
|
// Validate sections
|
|
requireSection(messages, "smtp")
|
|
requireSection(messages, "web")
|
|
requireSection(messages, "datastore")
|
|
if messages.Len() > 0 {
|
|
fmt.Fprintln(os.Stderr, "Error(s) validating configuration:")
|
|
for e := messages.Front(); e != nil; e = e.Next() {
|
|
fmt.Fprintln(os.Stderr, " -", e.Value.(string))
|
|
}
|
|
return fmt.Errorf("Failed to validate configuration")
|
|
}
|
|
|
|
// Validate options
|
|
requireOption(messages, "smtp", "ip4.address")
|
|
requireOption(messages, "smtp", "ip4.port")
|
|
requireOption(messages, "smtp", "domain")
|
|
requireOption(messages, "web", "ip4.address")
|
|
requireOption(messages, "web", "ip4.port")
|
|
requireOption(messages, "web", "templates.dir")
|
|
requireOption(messages, "web", "public.dir")
|
|
requireOption(messages, "datastore", "path")
|
|
if messages.Len() > 0 {
|
|
fmt.Fprintln(os.Stderr, "Error(s) validating configuration:")
|
|
for e := messages.Front(); e != nil; e = e.Next() {
|
|
fmt.Fprintln(os.Stderr, " -", e.Value.(string))
|
|
}
|
|
return fmt.Errorf("Failed to validate configuration")
|
|
}
|
|
|
|
return nil
|
|
}
|
|
|
|
// requireSection checks that a [section] is defined in the configuration file,
|
|
// appending a message if not.
|
|
func requireSection(messages *list.List, section string) {
|
|
if !Config.HasSection(section) {
|
|
messages.PushBack(fmt.Sprintf("Config section [%v] is required", section))
|
|
}
|
|
}
|
|
|
|
// requireOption checks that 'option' is defined in [section] of the config file,
|
|
// appending a message if not.
|
|
func requireOption(messages *list.List, section string, option string) {
|
|
if !Config.HasOption(section, option) {
|
|
messages.PushBack(fmt.Sprintf("Config option '%v' is required in section [%v]", option, section))
|
|
}
|
|
}
|