handlers.go 963 Bytes
Newer Older
ale's avatar
ale committed
1 2 3
package tabacco

import (
ale's avatar
ale committed
4
	"errors"
ale's avatar
ale committed
5 6 7 8 9 10
	"fmt"
)

// HandlerSpec defines the configuration for a handler.
type HandlerSpec struct {
	// Handler name (unique global identifier).
ale's avatar
ale committed
11
	Name string `yaml:"name"`
ale's avatar
ale committed
12 13

	// Handler type, one of the known types.
ale's avatar
ale committed
14
	Type string `yaml:"type"`
ale's avatar
ale committed
15

ale's avatar
ale committed
16
	Params Params `yaml:"params"`
ale's avatar
ale committed
17 18 19
}

// Parse a HandlerSpec and return a Handler instance.
ale's avatar
ale committed
20
func (spec *HandlerSpec) Parse(src *SourceSpec) (Handler, error) {
ale's avatar
ale committed
21
	if spec.Name == "" {
ale's avatar
ale committed
22 23 24 25 26 27 28 29 30 31 32
		return nil, errors.New("name is not set")
	}

	// Merge parameters from the handler spec and the source, with
	// preference to the latter.
	params := make(map[string]interface{})
	for k, v := range spec.Params {
		params[k] = v
	}
	for k, v := range src.Params {
		params[k] = v
ale's avatar
ale committed
33 34
	}

ale's avatar
ale committed
35 36
	switch spec.Type {
	case "file":
ale's avatar
ale committed
37
		return newFileHandler(spec.Name, params)
ale's avatar
ale committed
38
	case "pipe":
ale's avatar
ale committed
39
		return newPipeHandler(spec.Name, params)
ale's avatar
ale committed
40 41 42 43
	default:
		return nil, fmt.Errorf("%s: unknown handler type '%s'", spec.Name, spec.Type)
	}
}