Allow omitting instance_name in config

One will be assigned automatically.
This commit is contained in:
fox.cpp 2019-03-20 19:21:09 +03:00 committed by emersion
parent 16b6f8d526
commit af62042aec
2 changed files with 20 additions and 2 deletions

View file

@ -44,6 +44,11 @@ module_name instance_name {
you need to refer to the module from different place in configuration (e.g. you need to refer to the module from different place in configuration (e.g.
configure SMTP to deliver mail to certain specific storage) configure SMTP to deliver mail to certain specific storage)
You can omit `instance_name`. If there is only one module config. block - it
will get name the same as `module_name`. If there are multiple config blocks
for one module - they will get names `module_name`, `module_name1`,
`module_name2` and so on.
#### Defaults #### Defaults
Maddy provides reasonable defaults so you can start using it without spending Maddy provides reasonable defaults so you can start using it without spending

View file

@ -7,6 +7,7 @@ import (
"log" "log"
"os" "os"
"os/signal" "os/signal"
"strconv"
"strings" "strings"
"syscall" "syscall"
@ -17,12 +18,14 @@ import (
func Start(cfg []config.CfgTreeNode) error { func Start(cfg []config.CfgTreeNode) error {
var instances []module.Module var instances []module.Module
for _, block := range cfg { for _, block := range cfg {
var instName string
if len(block.Args) == 0 { if len(block.Args) == 0 {
return fmt.Errorf("wanted at least 1 argument in module instance definition") instName = implicitInstanceName(block.Name)
} else {
instName = block.Args[0]
} }
modName := block.Name modName := block.Name
instName := block.Args[0]
factory := module.GetMod(modName) factory := module.GetMod(modName)
if factory == nil { if factory == nil {
@ -123,3 +126,13 @@ func deliveryTarget(args []string) (module.DeliveryTarget, error) {
} }
return target, nil return target, nil
} }
func implicitInstanceName(modName string) string {
if mod := module.GetInstance(modName); mod == nil {
return modName
}
i := 1
for ; module.GetInstance(modName+strconv.Itoa(i)) != nil; i++ {
}
return modName + strconv.Itoa(i)
}