handlers.go 951 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 map[string]interface{} `yaml:"params"`
17 18 19 20 21

	PreBackupCommand   string `yaml:"pre_backup_command"`
	PostBackupCommand  string `yaml:"post_backup_command"`
	PreRestoreCommand  string `yaml:"pre_restore_command"`
	PostRestoreCommand string `yaml:"post_restore_command"`
ale's avatar
ale committed
22 23 24 25
}

// Parse a HandlerSpec and return a Handler instance.
func (spec *HandlerSpec) Parse(shell *Shell) (Handler, error) {
ale's avatar
ale committed
26 27 28 29
	if spec.Name == "" {
		return nil, errors.New("name is empty")
	}

ale's avatar
ale committed
30 31
	switch spec.Type {
	case "file":
32
		return newFileHandler(*spec, shell)
ale's avatar
ale committed
33
	case "pipe":
34
		return newPipeHandler(*spec, shell)
ale's avatar
ale committed
35 36 37 38
	default:
		return nil, fmt.Errorf("%s: unknown handler type '%s'", spec.Name, spec.Type)
	}
}