handler_file.go 1.29 KB
Newer Older
ale's avatar
ale committed
1 2 3 4 5 6
package tabacco

import (
	"context"
	"errors"
	"path/filepath"
ale's avatar
ale committed
7 8

	"git.autistici.org/ale/tabacco/jobs"
ale's avatar
ale committed
9 10 11 12
)

type fileHandler struct {
	path string
13
	spec HandlerSpec
ale's avatar
ale committed
14 15
}

16 17
func newFileHandler(spec HandlerSpec, _ *Shell) (Handler, error) {
	path, ok := spec.Params["path"].(string)
ale's avatar
ale committed
18 19 20
	if !ok || path == "" {
		return nil, errors.New("missing path")
	}
21 22 23 24 25
	return &fileHandler{path: path, spec: spec}, nil
}

func (h *fileHandler) Spec() HandlerSpec {
	return h.spec
ale's avatar
ale committed
26 27
}

ale's avatar
ale committed
28 29
func (h *fileHandler) DatasetsForBackup(ds Dataset) []Dataset {
	// Set SourcePath on all atoms.
ale's avatar
ale committed
30 31
	var atoms []Atom
	for _, atom := range ds.Atoms {
ale's avatar
ale committed
32 33 34 35 36
		relPath := atom.RelativePath
		if relPath == "" {
			relPath = atom.Name
		}
		atom.SourcePath = filepath.Join(h.path, relPath)
ale's avatar
ale committed
37 38 39
		atoms = append(atoms, atom)
	}
	ds.Atoms = atoms
ale's avatar
ale committed
40 41 42 43 44 45 46 47 48 49 50
	return []Dataset{ds}
}

func (h *fileHandler) BackupJob(repo Repository, backup Backup, ds Dataset) jobs.Job {
	return jobs.JobFunc(func(ctx context.Context) error {
		return repo.Backup(ctx, backup, ds, h.path /* UNUSED */)
	})
}

func (h *fileHandler) DatasetsForRestore(ds Dataset) []Dataset {
	return []Dataset{ds}
ale's avatar
ale committed
51 52
}

ale's avatar
ale committed
53 54 55 56
func (h *fileHandler) RestoreJob(repo Repository, backup Backup, ds Dataset, target string) jobs.Job {
	return jobs.JobFunc(func(ctx context.Context) error {
		return repo.Restore(ctx, backup, ds, target)
	})
ale's avatar
ale committed
57
}