2018-01-03 14:41:40 +00:00
|
|
|
package all
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"log"
|
|
|
|
|
|
|
|
"github.com/FreifunkBremen/yanic/output"
|
2018-01-13 18:08:46 +00:00
|
|
|
"github.com/FreifunkBremen/yanic/output/filter"
|
2018-01-03 14:41:40 +00:00
|
|
|
"github.com/FreifunkBremen/yanic/runtime"
|
|
|
|
)
|
|
|
|
|
|
|
|
type Output struct {
|
|
|
|
output.Output
|
2018-01-13 18:08:46 +00:00
|
|
|
list map[int]output.Output
|
|
|
|
outputFilter map[int]filter.Set
|
2018-01-03 14:41:40 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func Register(configuration map[string]interface{}) (output.Output, error) {
|
|
|
|
list := make(map[int]output.Output)
|
2018-01-13 18:08:46 +00:00
|
|
|
outputFilter := make(map[int]filter.Set)
|
2018-01-03 14:41:40 +00:00
|
|
|
i := 1
|
|
|
|
allOutputs := configuration
|
|
|
|
for outputType, outputRegister := range output.Adapters {
|
|
|
|
configForOutput := allOutputs[outputType]
|
|
|
|
if configForOutput == nil {
|
|
|
|
log.Printf("the output type '%s' has no configuration\n", outputType)
|
|
|
|
continue
|
|
|
|
}
|
2018-01-15 00:01:32 +00:00
|
|
|
outputConfigs, ok := configForOutput.([]interface{})
|
2018-01-03 14:41:40 +00:00
|
|
|
if !ok {
|
|
|
|
return nil, fmt.Errorf("the output type '%s' has the wrong format", outputType)
|
|
|
|
}
|
2018-01-15 00:01:32 +00:00
|
|
|
for _, outputConfig := range outputConfigs {
|
|
|
|
config, ok := outputConfig.(map[string]interface{})
|
|
|
|
if !ok {
|
|
|
|
return nil, fmt.Errorf("the output type '%s' has the wrong format", outputType)
|
|
|
|
}
|
2018-01-03 14:41:40 +00:00
|
|
|
if c, ok := config["enable"].(bool); ok && !c {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
output, err := outputRegister(config)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
if output == nil {
|
|
|
|
continue
|
|
|
|
}
|
2018-01-13 18:08:46 +00:00
|
|
|
var errs []error
|
|
|
|
var filterSet filter.Set
|
2018-01-03 14:41:40 +00:00
|
|
|
if c := config["filter"]; c != nil {
|
2018-01-13 18:08:46 +00:00
|
|
|
if filterConf, ok := c.(map[string]interface{}); ok {
|
|
|
|
filterSet, errs = filter.New(filterConf)
|
|
|
|
}
|
|
|
|
if len(errs) > 0 {
|
|
|
|
return nil, fmt.Errorf("filter configuration errors: %v", errs)
|
|
|
|
}
|
|
|
|
outputFilter[i] = filterSet
|
2018-01-03 14:41:40 +00:00
|
|
|
}
|
2018-01-13 18:08:46 +00:00
|
|
|
list[i] = output
|
2018-01-03 14:41:40 +00:00
|
|
|
i++
|
|
|
|
}
|
|
|
|
}
|
2018-01-13 18:08:46 +00:00
|
|
|
return &Output{list: list, outputFilter: outputFilter}, nil
|
2018-01-03 14:41:40 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (o *Output) Save(nodes *runtime.Nodes) {
|
|
|
|
for i, item := range o.list {
|
2018-01-13 18:08:46 +00:00
|
|
|
item.Save(o.outputFilter[i].Apply(nodes))
|
2018-01-03 14:41:40 +00:00
|
|
|
}
|
|
|
|
}
|