From 44be488da36aedbe8619d1aee63b595347cdfe02 Mon Sep 17 00:00:00 2001 From: yohamta Date: Wed, 20 Jul 2022 14:27:36 +0900 Subject: [PATCH] feat: overwrite global setting with individual DAG config --- internal/config/config.go | 10 +++++++--- internal/config/config_test.go | 20 ++++++++++++++++++++ internal/config/definition.go | 2 +- internal/config/loader.go | 22 +++++++++++++++++++++- internal/models/status_test.go | 2 +- internal/reporter/reporter.go | 4 ++-- internal/reporter/reporter_test.go | 2 +- tests/config/.dagu/config.yaml | 4 +++- tests/testdata/config_no_overwrite.yaml | 3 +++ tests/testdata/config_overwrite.yaml | 6 ++++++ 10 files changed, 65 insertions(+), 10 deletions(-) create mode 100644 tests/testdata/config_no_overwrite.yaml create mode 100644 tests/testdata/config_overwrite.yaml diff --git a/internal/config/config.go b/internal/config/config.go index 8931e1bce..92249c195 100644 --- a/internal/config/config.go +++ b/internal/config/config.go @@ -27,7 +27,7 @@ type Config struct { LogDir string HandlerOn HandlerOn Steps []*Step - MailOn MailOn + MailOn *MailOn ErrorMail *MailConfig InfoMail *MailConfig Smtp *SmtpConfig @@ -174,8 +174,12 @@ func (b *builder) buildFromDefinition(def *configDefinition, globalConfig *Confi } c.Group = def.Group c.Description = def.Description - c.MailOn.Failure = def.MailOn.Failure - c.MailOn.Success = def.MailOn.Success + if def.MailOn != nil { + c.MailOn = &MailOn{ + Failure: def.MailOn.Failure, + Success: def.MailOn.Success, + } + } c.Delay = time.Second * time.Duration(def.DelaySec) c.Tags = parseTags(def.Tags) diff --git a/internal/config/config_test.go b/internal/config/config_test.go index 64881f50a..3bc909220 100644 --- a/internal/config/config_test.go +++ b/internal/config/config_test.go @@ -304,3 +304,23 @@ func TestSockAddr(t *testing.T) { cfg := &Config{ConfigPath: "testdata/testDag.yml"} require.Regexp(t, `^/tmp/@dagu-testDag-[0-9a-f]+\.sock$`, cfg.SockAddr()) } + +func TestOverwriteGlobalConfig(t *testing.T) { + l := &Loader{HomeDir: utils.MustGetUserHomeDir()} + + cfg, err := l.Load(path.Join(testDir, "config_overwrite.yaml"), "") + require.NoError(t, err) + + require.Equal(t, &MailOn{ + Failure: false, + Success: false, + }, cfg.MailOn) + + cfg, err = l.Load(path.Join(testDir, "config_no_overwrite.yaml"), "") + require.NoError(t, err) + + require.Equal(t, &MailOn{ + Failure: true, + Success: false, + }, cfg.MailOn) +} diff --git a/internal/config/definition.go b/internal/config/definition.go index 0437f329d..7f337a885 100644 --- a/internal/config/definition.go +++ b/internal/config/definition.go @@ -10,7 +10,7 @@ type configDefinition struct { HandlerOn handerOnDef Steps []*stepDef Smtp smtpConfigDef - MailOn mailOnDef + MailOn *mailOnDef ErrorMail mailConfigDef InfoMail mailConfigDef DelaySec int diff --git a/internal/config/loader.go b/internal/config/loader.go index 6455c3bcf..186f7ad87 100644 --- a/internal/config/loader.go +++ b/internal/config/loader.go @@ -7,6 +7,7 @@ import ( "os" "path" "path/filepath" + "reflect" "strings" "github.com/imdario/mergo" @@ -165,8 +166,27 @@ func (cl *Loader) loadConfig(f string, opts *BuildConfigOptions) (*Config, error return dst, nil } +type mergeTranformer struct { +} + +var _ mergo.Transformers = (*mergeTranformer)(nil) + +func (mt *mergeTranformer) Transformer(typ reflect.Type) func(dst, src reflect.Value) error { + if typ == reflect.TypeOf(MailOn{}) { + return func(dst, src reflect.Value) error { + if dst.CanSet() { + dst.Set(src) + } + return nil + } + } + return nil +} + func (cl *Loader) merge(dst, src *Config) error { - return mergo.MergeWithOverwrite(dst, src) + err := mergo.Merge(dst, src, mergo.WithOverride, + mergo.WithTransformers(&mergeTranformer{})) + return err } func (cl *Loader) load(file string) (config map[string]interface{}, err error) { diff --git a/internal/models/status_test.go b/internal/models/status_test.go index 8b7b96817..9487cdc46 100644 --- a/internal/models/status_test.go +++ b/internal/models/status_test.go @@ -39,7 +39,7 @@ func TestStatusSerialization(t *testing.T) { RepeatPolicy: config.RepeatPolicy{}, Preconditions: []*config.Condition{}, }, }, - MailOn: config.MailOn{}, + MailOn: &config.MailOn{}, ErrorMail: &config.MailConfig{}, InfoMail: &config.MailConfig{}, Smtp: &config.SmtpConfig{}, diff --git a/internal/reporter/reporter.go b/internal/reporter/reporter.go index 71d3c5087..5ec3542ae 100644 --- a/internal/reporter/reporter.go +++ b/internal/reporter/reporter.go @@ -60,7 +60,7 @@ func (rp *Reporter) ReportSummary(status *models.Status, err error) { // SendMail is a function that sends a report mail. func (rp *Reporter) SendMail(cfg *config.Config, status *models.Status, err error) error { if err != nil || status.Status == scheduler.SchedulerStatus_Error { - if cfg.MailOn.Failure { + if cfg.MailOn != nil && cfg.MailOn.Failure { return rp.Mailer.SendMail( cfg.ErrorMail.From, []string{cfg.ErrorMail.To}, @@ -69,7 +69,7 @@ func (rp *Reporter) SendMail(cfg *config.Config, status *models.Status, err erro ) } } else if status.Status == scheduler.SchedulerStatus_Success { - if cfg.MailOn.Success { + if cfg.MailOn != nil && cfg.MailOn.Success { rp.Mailer.SendMail( cfg.InfoMail.From, []string{cfg.InfoMail.To}, diff --git a/internal/reporter/reporter_test.go b/internal/reporter/reporter_test.go index 8a70e69a4..a815837d5 100644 --- a/internal/reporter/reporter_test.go +++ b/internal/reporter/reporter_test.go @@ -34,7 +34,7 @@ func TestReporter(t *testing.T) { cfg := &config.Config{ Name: "test DAG", - MailOn: config.MailOn{ + MailOn: &config.MailOn{ Failure: true, }, ErrorMail: &config.MailConfig{ diff --git a/tests/config/.dagu/config.yaml b/tests/config/.dagu/config.yaml index a177f9b25..29c325537 100644 --- a/tests/config/.dagu/config.yaml +++ b/tests/config/.dagu/config.yaml @@ -12,4 +12,6 @@ infoMail: from: "system@mail.com" to: "info@mail.com" prefix: "[INFO]" -histRetentionDays: 7 \ No newline at end of file +histRetentionDays: 7 +mailOn: + failure: true \ No newline at end of file diff --git a/tests/testdata/config_no_overwrite.yaml b/tests/testdata/config_no_overwrite.yaml new file mode 100644 index 000000000..945467f2b --- /dev/null +++ b/tests/testdata/config_no_overwrite.yaml @@ -0,0 +1,3 @@ +steps: + - name: "1" + command: "true" \ No newline at end of file diff --git a/tests/testdata/config_overwrite.yaml b/tests/testdata/config_overwrite.yaml new file mode 100644 index 000000000..9222a7322 --- /dev/null +++ b/tests/testdata/config_overwrite.yaml @@ -0,0 +1,6 @@ +mailOn: + failure: false + +steps: + - name: "1" + command: "true" \ No newline at end of file