parse job definitions from YAML file

This commit is contained in:
Sam Hoffman
2026-01-30 19:16:00 -05:00
parent b14132886d
commit 5ea252ecb7
7 changed files with 94 additions and 15 deletions

View File

@@ -16,12 +16,12 @@ import (
)
type BackupJob struct {
Source string // the source dataset (e.g., zroot)
TargetHost string // SSH-compatible host
Target string // the target dataset
Keep int // number of snapshots to keep
Prefix string // name each snapshot with this prefix
Recursive bool // create recursive snapshots
Source string `yaml:"source"` // the source dataset (e.g., zroot)
TargetHost string `yaml:"targetHost"` // SSH-compatible host
Target string `yaml:"target"` // the target dataset
Keep int `yaml:"keep"` // number of snapshots to keep
Recursive bool `yaml:"recursive"` // create recursive snapshots
Prefix string `yaml:"prefix"` // name each snapshot with this prefix
}
// func (j *BackupJob) getBaseSnap() {

27
internal/jobs/parse.go Normal file
View File

@@ -0,0 +1,27 @@
package jobs
import (
"io"
"os"
"gopkg.in/yaml.v3"
)
type JobsRoot struct {
Jobs []BackupJob `yaml:"jobs"`
}
func Parse(reader io.Reader) ([]BackupJob, error) {
dec := yaml.NewDecoder(reader)
var jobsRoot JobsRoot
err := dec.Decode(&jobsRoot)
return jobsRoot.Jobs, err
}
func ParseFile(path string) ([]BackupJob, error) {
f, err := os.Open(path)
if err != nil {
return nil, err
}
return Parse(f)
}

View File

@@ -0,0 +1,26 @@
package jobs_test
import (
"strings"
"testing"
"git.gentoo.party/sam/thanks/internal/jobs"
"github.com/stretchr/testify/assert"
)
const testParseStr = `
jobs:
- source: "zroot/home/sam/thanks"
target: "zrust/backup/weller/thanks"
targetHost: "backup@woodford.gentoo.party"
keep: 30
prefix: "thanks-"
recursive: false
`
func TestParse(t *testing.T) {
reader := strings.NewReader(testParseStr)
backupJobs, err := jobs.Parse(reader)
assert.Nil(t, err)
assert.Equal(t, backupJobs[0].Source, "zroot/home/sam/thanks")
}