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
5ccf2d8c95
14
job.go
14
job.go
@ -52,7 +52,7 @@ func (r ResticConfig) Validate() error {
|
||||
type Job struct {
|
||||
Name string `hcl:"name,label"`
|
||||
Schedule string `hcl:"schedule"`
|
||||
Config ResticConfig `hcl:"config,block"`
|
||||
Config *ResticConfig `hcl:"config,block"`
|
||||
Tasks []JobTask `hcl:"task,block"`
|
||||
Backup BackupFilesTask `hcl:"backup,block"`
|
||||
Forget *ForgetOpts `hcl:"forget,block"`
|
||||
@ -85,6 +85,10 @@ func (j Job) Validate() error {
|
||||
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 {
|
||||
return fmt.Errorf("job %s has invalid config: %w", j.Name, err)
|
||||
}
|
||||
@ -276,7 +280,7 @@ func (j Job) NewRestic() *Restic {
|
||||
}
|
||||
|
||||
type Config struct {
|
||||
// GlobalConfig *ResticConfig `hcl:"global_config,block"`
|
||||
DefaultConfig *ResticConfig `hcl:"default_config,block"`
|
||||
Jobs []Job `hcl:"job,block"`
|
||||
}
|
||||
|
||||
@ -286,6 +290,12 @@ func (c Config) Validate() error {
|
||||
}
|
||||
|
||||
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 {
|
||||
return err
|
||||
}
|
||||
|
43
job_test.go
43
job_test.go
@ -7,8 +7,8 @@ import (
|
||||
main "git.iamthefij.com/iamthefij/restic-scheduler"
|
||||
)
|
||||
|
||||
func ValidResticConfig() main.ResticConfig {
|
||||
return main.ResticConfig{
|
||||
func ValidResticConfig() *main.ResticConfig {
|
||||
return &main.ResticConfig{
|
||||
Passphrase: "shh",
|
||||
Repo: "./data",
|
||||
Env: nil,
|
||||
@ -129,7 +129,7 @@ func TestJobValidation(t *testing.T) {
|
||||
job: main.Job{
|
||||
Name: "Test job",
|
||||
Schedule: "@daily",
|
||||
Config: main.ResticConfig{}, //nolint:exhaustruct
|
||||
Config: &main.ResticConfig{}, //nolint:exhaustruct
|
||||
Tasks: []main.JobTask{},
|
||||
Backup: main.BackupFilesTask{Paths: []string{"/test"}}, //nolint:exhaustruct
|
||||
Forget: nil,
|
||||
@ -207,7 +207,9 @@ func TestConfigValidation(t *testing.T) {
|
||||
}{
|
||||
{
|
||||
name: "Valid job",
|
||||
config: main.Config{Jobs: []main.Job{{
|
||||
config: main.Config{
|
||||
DefaultConfig: nil,
|
||||
Jobs: []main.Job{{
|
||||
Name: "Valid job",
|
||||
Schedule: "@daily",
|
||||
Config: ValidResticConfig(),
|
||||
@ -215,7 +217,24 @@ func TestConfigValidation(t *testing.T) {
|
||||
Backup: main.BackupFilesTask{Paths: []string{"/test"}}, //nolint:exhaustruct
|
||||
MySQL: []main.JobTaskMySQL{},
|
||||
Sqlite: []main.JobTaskSqlite{},
|
||||
}}},
|
||||
}},
|
||||
},
|
||||
expectedErr: nil,
|
||||
},
|
||||
{
|
||||
name: "Valid job with default config",
|
||||
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,
|
||||
},
|
||||
{
|
||||
@ -237,6 +256,20 @@ func TestConfigValidation(t *testing.T) {
|
||||
}}},
|
||||
expectedErr: main.ErrMissingField,
|
||||
},
|
||||
{
|
||||
name: "Missing config",
|
||||
config: main.Config{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,
|
||||
},
|
||||
}
|
||||
|
||||
for _, c := range cases {
|
||||
|
Loading…
Reference in New Issue
Block a user