Adds DefaultConfig to the config format so that a single config can be defined for all jobs
This commit is contained in:
parent
57e933c20d
commit
9c5d597ab4
40
job.go
40
job.go
@ -52,7 +52,7 @@ func (r ResticConfig) Validate() error {
|
|||||||
type Job struct {
|
type Job struct {
|
||||||
Name string `hcl:"name,label"`
|
Name string `hcl:"name,label"`
|
||||||
Schedule string `hcl:"schedule"`
|
Schedule string `hcl:"schedule"`
|
||||||
Config ResticConfig `hcl:"config,block"`
|
Config *ResticConfig `hcl:"config,block"`
|
||||||
Tasks []JobTask `hcl:"task,block"`
|
Tasks []JobTask `hcl:"task,block"`
|
||||||
Backup BackupFilesTask `hcl:"backup,block"`
|
Backup BackupFilesTask `hcl:"backup,block"`
|
||||||
Forget *ForgetOpts `hcl:"forget,block"`
|
Forget *ForgetOpts `hcl:"forget,block"`
|
||||||
@ -73,6 +73,18 @@ func (j Job) validateTasks() error {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
for _, mysql := range j.MySQL {
|
||||||
|
if err := mysql.Validate(); err != nil {
|
||||||
|
return fmt.Errorf("job %s has an invalid task: %w", j.Name, err)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
for _, sqlite := range j.Sqlite {
|
||||||
|
if err := sqlite.Validate(); err != nil {
|
||||||
|
return fmt.Errorf("job %s has an invalid task: %w", j.Name, err)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -85,6 +97,10 @@ func (j Job) Validate() error {
|
|||||||
return fmt.Errorf("job %s has an invalid schedule: %v: %w", j.Name, err, ErrInvalidConfigValue)
|
return fmt.Errorf("job %s has an invalid schedule: %v: %w", j.Name, err, ErrInvalidConfigValue)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if j.Config == nil {
|
||||||
|
return fmt.Errorf("job %s is missing restic config: %w", j.Name, ErrMissingField)
|
||||||
|
}
|
||||||
|
|
||||||
if err := j.Config.Validate(); err != nil {
|
if err := j.Config.Validate(); err != nil {
|
||||||
return fmt.Errorf("job %s has invalid config: %w", j.Name, err)
|
return fmt.Errorf("job %s has invalid config: %w", j.Name, err)
|
||||||
}
|
}
|
||||||
@ -93,18 +109,6 @@ func (j Job) Validate() error {
|
|||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
for _, mysql := range j.MySQL {
|
|
||||||
if err := mysql.Validate(); err != nil {
|
|
||||||
return fmt.Errorf("job %s has an invalid task: %w", j.Name, err)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
for _, sqlite := range j.Sqlite {
|
|
||||||
if err := sqlite.Validate(); err != nil {
|
|
||||||
return fmt.Errorf("job %s has an invalid task: %w", j.Name, err)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
if err := j.Backup.Validate(); err != nil {
|
if err := j.Backup.Validate(); err != nil {
|
||||||
return fmt.Errorf("job %s has an invalid backup config: %w", j.Name, err)
|
return fmt.Errorf("job %s has an invalid backup config: %w", j.Name, err)
|
||||||
}
|
}
|
||||||
@ -276,8 +280,8 @@ func (j Job) NewRestic() *Restic {
|
|||||||
}
|
}
|
||||||
|
|
||||||
type Config struct {
|
type Config struct {
|
||||||
// GlobalConfig *ResticConfig `hcl:"global_config,block"`
|
DefaultConfig *ResticConfig `hcl:"default_config,block"`
|
||||||
Jobs []Job `hcl:"job,block"`
|
Jobs []Job `hcl:"job,block"`
|
||||||
}
|
}
|
||||||
|
|
||||||
func (c Config) Validate() error {
|
func (c Config) Validate() error {
|
||||||
@ -286,6 +290,12 @@ func (c Config) Validate() error {
|
|||||||
}
|
}
|
||||||
|
|
||||||
for _, job := range c.Jobs {
|
for _, job := range c.Jobs {
|
||||||
|
// Use default restic config if no job config is provided
|
||||||
|
// TODO: Maybe merge values here
|
||||||
|
if job.Config == nil {
|
||||||
|
job.Config = c.DefaultConfig
|
||||||
|
}
|
||||||
|
|
||||||
if err := job.Validate(); err != nil {
|
if err := job.Validate(); err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
89
job_test.go
89
job_test.go
@ -7,8 +7,8 @@ import (
|
|||||||
main "git.iamthefij.com/iamthefij/restic-scheduler"
|
main "git.iamthefij.com/iamthefij/restic-scheduler"
|
||||||
)
|
)
|
||||||
|
|
||||||
func ValidResticConfig() main.ResticConfig {
|
func ValidResticConfig() *main.ResticConfig {
|
||||||
return main.ResticConfig{
|
return &main.ResticConfig{
|
||||||
Passphrase: "shh",
|
Passphrase: "shh",
|
||||||
Repo: "./data",
|
Repo: "./data",
|
||||||
Env: nil,
|
Env: nil,
|
||||||
@ -129,7 +129,7 @@ func TestJobValidation(t *testing.T) {
|
|||||||
job: main.Job{
|
job: main.Job{
|
||||||
Name: "Test job",
|
Name: "Test job",
|
||||||
Schedule: "@daily",
|
Schedule: "@daily",
|
||||||
Config: main.ResticConfig{}, //nolint:exhaustruct
|
Config: &main.ResticConfig{}, //nolint:exhaustruct
|
||||||
Tasks: []main.JobTask{},
|
Tasks: []main.JobTask{},
|
||||||
Backup: main.BackupFilesTask{Paths: []string{"/test"}}, //nolint:exhaustruct
|
Backup: main.BackupFilesTask{Paths: []string{"/test"}}, //nolint:exhaustruct
|
||||||
Forget: nil,
|
Forget: nil,
|
||||||
@ -207,34 +207,75 @@ func TestConfigValidation(t *testing.T) {
|
|||||||
}{
|
}{
|
||||||
{
|
{
|
||||||
name: "Valid job",
|
name: "Valid job",
|
||||||
config: main.Config{Jobs: []main.Job{{
|
config: main.Config{
|
||||||
Name: "Valid job",
|
DefaultConfig: nil,
|
||||||
Schedule: "@daily",
|
Jobs: []main.Job{{
|
||||||
Config: ValidResticConfig(),
|
Name: "Valid job",
|
||||||
Tasks: []main.JobTask{},
|
Schedule: "@daily",
|
||||||
Backup: main.BackupFilesTask{Paths: []string{"/test"}}, //nolint:exhaustruct
|
Config: ValidResticConfig(),
|
||||||
MySQL: []main.JobTaskMySQL{},
|
Tasks: []main.JobTask{},
|
||||||
Sqlite: []main.JobTaskSqlite{},
|
Backup: main.BackupFilesTask{Paths: []string{"/test"}}, //nolint:exhaustruct
|
||||||
}}},
|
MySQL: []main.JobTaskMySQL{},
|
||||||
|
Sqlite: []main.JobTaskSqlite{},
|
||||||
|
}},
|
||||||
|
},
|
||||||
expectedErr: nil,
|
expectedErr: nil,
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
name: "No jobs",
|
name: "Valid job with default config",
|
||||||
config: main.Config{Jobs: []main.Job{}},
|
config: main.Config{
|
||||||
|
DefaultConfig: ValidResticConfig(),
|
||||||
|
Jobs: []main.Job{{
|
||||||
|
Name: "Valid job",
|
||||||
|
Schedule: "@daily",
|
||||||
|
Config: nil,
|
||||||
|
Tasks: []main.JobTask{},
|
||||||
|
Backup: main.BackupFilesTask{Paths: []string{"/test"}}, //nolint:exhaustruct
|
||||||
|
MySQL: []main.JobTaskMySQL{},
|
||||||
|
Sqlite: []main.JobTaskSqlite{},
|
||||||
|
}},
|
||||||
|
},
|
||||||
|
expectedErr: nil,
|
||||||
|
},
|
||||||
|
{
|
||||||
|
name: "No jobs",
|
||||||
|
config: main.Config{
|
||||||
|
DefaultConfig: nil,
|
||||||
|
Jobs: []main.Job{},
|
||||||
|
},
|
||||||
expectedErr: main.ErrNoJobsFound,
|
expectedErr: main.ErrNoJobsFound,
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
name: "Invalid name",
|
name: "Invalid name",
|
||||||
config: main.Config{Jobs: []main.Job{{
|
config: main.Config{
|
||||||
Name: "",
|
DefaultConfig: nil,
|
||||||
Schedule: "@daily",
|
Jobs: []main.Job{{
|
||||||
Config: ValidResticConfig(),
|
Name: "",
|
||||||
Tasks: []main.JobTask{},
|
Schedule: "@daily",
|
||||||
Backup: main.BackupFilesTask{Paths: []string{"/test"}}, //nolint:exhaustruct
|
Config: ValidResticConfig(),
|
||||||
Forget: nil,
|
Tasks: []main.JobTask{},
|
||||||
MySQL: []main.JobTaskMySQL{},
|
Backup: main.BackupFilesTask{Paths: []string{"/test"}}, //nolint:exhaustruct
|
||||||
Sqlite: []main.JobTaskSqlite{},
|
Forget: nil,
|
||||||
}}},
|
MySQL: []main.JobTaskMySQL{},
|
||||||
|
Sqlite: []main.JobTaskSqlite{},
|
||||||
|
}},
|
||||||
|
},
|
||||||
|
expectedErr: main.ErrMissingField,
|
||||||
|
},
|
||||||
|
{
|
||||||
|
name: "Missing config",
|
||||||
|
config: main.Config{
|
||||||
|
DefaultConfig: nil,
|
||||||
|
Jobs: []main.Job{{
|
||||||
|
Name: "",
|
||||||
|
Schedule: "@daily",
|
||||||
|
Config: nil,
|
||||||
|
Tasks: []main.JobTask{},
|
||||||
|
Backup: main.BackupFilesTask{Paths: []string{"/test"}}, //nolint:exhaustruct
|
||||||
|
Forget: nil,
|
||||||
|
MySQL: []main.JobTaskMySQL{},
|
||||||
|
Sqlite: []main.JobTaskSqlite{},
|
||||||
|
}}},
|
||||||
expectedErr: main.ErrMissingField,
|
expectedErr: main.ErrMissingField,
|
||||||
},
|
},
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user