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

1
.gitignore vendored
View File

@@ -1 +1,2 @@
thanks.yaml
bin/ bin/

View File

@@ -4,6 +4,7 @@ import (
"context" "context"
"flag" "flag"
"fmt" "fmt"
"log"
"git.gentoo.party/sam/thanks/internal/jobs" "git.gentoo.party/sam/thanks/internal/jobs"
) )
@@ -13,10 +14,14 @@ var (
BuildTime = "unknown" BuildTime = "unknown"
) )
var flagVersion = false var (
flagVersion = false
jobsFile = "thanks.yaml"
)
func init() { func init() {
flag.BoolVar(&flagVersion, "v", false, "show program version") flag.BoolVar(&flagVersion, "v", false, "show program version")
flag.StringVar(&jobsFile, "jobs", "thanks.yaml", "backup job definitions file")
} }
func main() { func main() {
@@ -27,15 +32,15 @@ func main() {
return return
} }
myJob := jobs.BackupJob{ backupJobs, err := jobs.ParseFile(jobsFile)
Source: "zroot/home/sam/thanks", if err != nil {
Target: "zrust/backup/weller/thanks", log.Fatalf("error reading backup job definitions: %s\n", err.Error())
TargetHost: "backup@woodford.gentoo.party",
Keep: 30,
Prefix: "thanks-",
Recursive: false,
} }
ctx := context.Background() ctx := context.Background()
myJob.Do(ctx) for _, myJob := range backupJobs {
myJob.Do(ctx)
}
log.Printf("backups completed successfully")
} }

10
go.mod
View File

@@ -1,3 +1,13 @@
module git.gentoo.party/sam/thanks module git.gentoo.party/sam/thanks
go 1.25.5 go 1.25.5
require (
github.com/stretchr/testify v1.11.1
gopkg.in/yaml.v3 v3.0.1
)
require (
github.com/davecgh/go-spew v1.1.1 // indirect
github.com/pmezard/go-difflib v1.0.0 // indirect
)

10
go.sum Normal file
View File

@@ -0,0 +1,10 @@
github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c=
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
github.com/stretchr/testify v1.11.1 h1:7s2iGBzp5EwR7/aIZr8ao5+dra3wiQyKjjFuvgVKu7U=
github.com/stretchr/testify v1.11.1/go.mod h1:wZwfW3scLgRK+23gO65QZefKpKQRnfz6sD981Nm4B6U=
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405 h1:yhCVgyC4o1eVCa2tZl7eS0r+SDo693bJlVdllGtEeKM=
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA=
gopkg.in/yaml.v3 v3.0.1/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=

View File

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