implement Executor pattern

This commit is contained in:
2026-02-09 13:10:00 -05:00
parent 3c11a79252
commit f615b03794
6 changed files with 93 additions and 48 deletions

View File

@@ -11,6 +11,7 @@ import (
"strings"
"time"
"git.gentoo.party/sam/thanks/internal/executor"
"git.gentoo.party/sam/thanks/internal/runner"
)
@@ -37,6 +38,20 @@ func Cmd(ctx context.Context, arg string, a ...any) ([]byte, error) {
return out, err
}
func CreateSnapshot(ctx context.Context, e executor.Executor, dataset, name string) error {
_, err := e.CombinedOutput(
ctx,
"zfs",
"snapshot",
fmt.Sprintf("%s@%s", dataset, name),
)
if err != nil {
return fmt.Errorf("zfs-snapshot error: %w", err)
}
return nil
}
func ParseSnapshots(reader io.Reader) ([]Snapshot, error) {
scanner := bufio.NewScanner(reader)
@@ -78,33 +93,8 @@ func ParseSnapshots(reader io.Reader) ([]Snapshot, error) {
return snaps, nil
}
type Host struct {
SSH string
ZFSPath string
}
func (h *Host) CommandContext(ctx context.Context, name string, arg ...string) *exec.Cmd {
var args []string
if h.SSH != "" {
name = "ssh"
args = append([]string{"ssh", h.SSH}, arg...)
} else {
args = arg
}
cmd := exec.CommandContext(
ctx,
name,
args...,
)
return cmd
}
// h.Comman
func Snapshots(ctx context.Context, target string) ([]Snapshot, error) {
cmd := exec.CommandContext(
func Snapshots(ctx context.Context, e executor.Executor, target string) ([]Snapshot, error) {
cmd := e.CommandContext(
ctx,
"zfs",
"list",
@@ -182,7 +172,6 @@ func IncrementalSend(ctx context.Context, prevSnapName, newSnapName, sshTarget,
)
}
func Destroy(ctx context.Context, target string) ([]byte, error) {
cmd := exec.CommandContext(ctx, "zfs", "destroy", target)
return cmd.CombinedOutput()
func Destroy(ctx context.Context, e executor.Executor, target string) ([]byte, error) {
return e.CombinedOutput(ctx, "zfs", target)
}