generated from golang-templates/seed
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
5 changed files
with
164 additions
and
47 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
package abstraction | ||
|
||
type ( | ||
ValidatableStr string | ||
Validatable interface { | ||
Validate() error | ||
} | ||
) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,122 @@ | ||
package config | ||
|
||
import ( | ||
"fmt" | ||
"time" | ||
|
||
"github.com/robfig/cron/v3" | ||
) | ||
|
||
var cronParser = cron.NewParser(cron.SecondOptional) | ||
|
||
func (cfg *Config) Validate() error { | ||
if err := cfg.LogFormat.Validate(); err != nil { | ||
return err | ||
} | ||
if err := cfg.LogLevel.Validate(); err != nil { | ||
return err | ||
} | ||
for _, job := range cfg.Jobs { | ||
if err := job.Validate(); err != nil { | ||
return err | ||
} | ||
} | ||
return nil | ||
} | ||
|
||
func (c *JobConfig) Validate() error { | ||
if c.Enabled == false { | ||
return nil | ||
} | ||
if c.Timeout < 0 { | ||
return fmt.Errorf( | ||
"timeout for jobs cannot be negative received `%s` for `%s`", | ||
c.Timeout, | ||
c.Name, | ||
) | ||
} | ||
|
||
if c.RetryDelay < 0 { | ||
return fmt.Errorf( | ||
"retry delay for jobs cannot be negative received `%s` for `%s`", | ||
c.RetryDelay, | ||
c.Name, | ||
) | ||
} | ||
for _, s := range c.Schedulers { | ||
if err := s.Validate(); err != nil { | ||
return err | ||
} | ||
} | ||
for _, t := range c.Tasks { | ||
if err := t.Validate(); err != nil { | ||
return err | ||
} | ||
} | ||
for _, t := range c.Hooks.Done { | ||
if err := t.Validate(); err != nil { | ||
return err | ||
} | ||
} | ||
for _, t := range c.Hooks.Failed { | ||
if err := t.Validate(); err != nil { | ||
return err | ||
} | ||
} | ||
return nil | ||
} | ||
|
||
func (c *Task) Validate() error { | ||
actions := []bool{ | ||
c.Get != "", | ||
c.Command != "", | ||
c.Post != "", | ||
} | ||
activeActions := 0 | ||
for _, t := range actions { | ||
if t { | ||
activeActions++ | ||
} | ||
} | ||
if activeActions != 1 { | ||
return fmt.Errorf( | ||
"a single task should have one of (get,post,command) fields, received:(command: `%s`, get: `%s`, post: `%s`)", | ||
c.Command, | ||
c.Get, | ||
c.Post, | ||
) | ||
} | ||
return nil | ||
} | ||
|
||
func (s *JobScheduler) Validate() error { | ||
if s.At != nil { | ||
if s.At.Before(time.Now()) { | ||
fmt.Println("you've set the time in the scheduler that is before now, received:", s.At, "Given time will be ignored") | ||
} | ||
} else if s.Interval < 0 { | ||
return fmt.Errorf("received a negative time in interval: `%v`", s.Interval) | ||
} else if _, err := cronParser.Parse(s.Cron); err != nil { | ||
return err | ||
} | ||
schedules := []bool{ | ||
s.At != nil, | ||
s.Interval != 0, | ||
s.Cron != "", | ||
} | ||
activeSchedules := 0 | ||
for _, t := range schedules { | ||
if t { | ||
activeSchedules++ | ||
} | ||
} | ||
if activeSchedules != 1 { | ||
return fmt.Errorf( | ||
"a single scheduler must have one of (at,interval,cron) field, received:(cron: `%s`, interval: `%s`, at: `%s`)", | ||
s.Cron, | ||
s.Interval, | ||
s.At, | ||
) | ||
} | ||
return nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters