add and test style from file
parent
a544dcdde7
commit
061292386c
|
|
@ -39,7 +39,7 @@ function init() {
|
||||||
}
|
}
|
||||||
function format(job) {
|
function format(job) {
|
||||||
return `<tr><td><details>
|
return `<tr><td><details>
|
||||||
<summary>(${job.last.status}) ${job.id}</summary>
|
<summary name="${job.id}">(${job.last.status}) ${job.title}</summary>
|
||||||
<table>
|
<table>
|
||||||
<tr>
|
<tr>
|
||||||
<td><code>${job.cron}</code></td>
|
<td><code>${job.cron}</code></td>
|
||||||
|
|
|
||||||
|
|
@ -48,11 +48,11 @@ func NewFromFile(config string) (*Scheduler, error) {
|
||||||
if len(line) == 0 {
|
if len(line) == 0 {
|
||||||
continue
|
continue
|
||||||
}
|
}
|
||||||
schedule, command := splitScheduleCommand(line)
|
schedule, command, title := splitScheduleCommandTitle(line)
|
||||||
if len(schedule) == 0 || len(command) == 0 {
|
if len(schedule) == 0 || len(command) == 0 {
|
||||||
continue
|
continue
|
||||||
}
|
}
|
||||||
job, err := NewJob(Bash, schedule, command)
|
job, err := newBashJob(schedule, command, title)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
logger.New().Error(err, "cannot fully parse file: new job error", config, ", sched", schedule, ", comm", command)
|
logger.New().Error(err, "cannot fully parse file: new job error", config, ", sched", schedule, ", comm", command)
|
||||||
continue
|
continue
|
||||||
|
|
@ -80,16 +80,20 @@ func cleanLine(b []byte) []byte {
|
||||||
return b
|
return b
|
||||||
}
|
}
|
||||||
|
|
||||||
func splitScheduleCommand(b []byte) (string, string) {
|
func splitScheduleCommandTitle(b []byte) (string, string, string) {
|
||||||
re := regexp.MustCompile(`^((\d+|\*\/\d+|(\d,)*\d+|\*) [ ]*){5,6}`)
|
re := regexp.MustCompile(`^((\d+|\*\/\d+|(\d,)*\d+|\*) [ ]*){5,6}`)
|
||||||
schedule := string(re.Find(b))
|
schedule := string(re.Find(b))
|
||||||
if len(schedule) == 0 {
|
if len(schedule) == 0 {
|
||||||
return "", ""
|
return "", "", ""
|
||||||
}
|
}
|
||||||
command := strings.TrimPrefix(string(b), schedule)
|
commandTitle := strings.TrimPrefix(string(b), schedule)
|
||||||
schedule = strings.TrimSpace(schedule)
|
schedule = strings.TrimSpace(schedule)
|
||||||
command = strings.TrimSpace(command)
|
commandTitle = strings.TrimSpace(commandTitle)
|
||||||
return schedule, command
|
title := ""
|
||||||
|
if i := strings.LastIndex(commandTitle, "#"); i >= 0 {
|
||||||
|
title = commandTitle[i+1:]
|
||||||
|
}
|
||||||
|
return schedule, commandTitle, title
|
||||||
}
|
}
|
||||||
|
|
||||||
func (s *Scheduler) Start() error {
|
func (s *Scheduler) Start() error {
|
||||||
|
|
|
||||||
|
|
@ -153,3 +153,42 @@ func TestSchedulerFromFile(t *testing.T) {
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func TestSplitScheduleCommandTitle(t *testing.T) {
|
||||||
|
cases := map[string]struct {
|
||||||
|
in string
|
||||||
|
schedule string
|
||||||
|
command string
|
||||||
|
title string
|
||||||
|
}{
|
||||||
|
"invalid schedule": {
|
||||||
|
in: "* * * cmd #title",
|
||||||
|
},
|
||||||
|
"no title": {
|
||||||
|
in: "* * * * * cmd ",
|
||||||
|
schedule: "* * * * *",
|
||||||
|
command: "cmd",
|
||||||
|
},
|
||||||
|
"schedule, command, title": {
|
||||||
|
in: "* * * * * cmd #title",
|
||||||
|
schedule: "* * * * *",
|
||||||
|
command: "cmd #title",
|
||||||
|
title: "title",
|
||||||
|
},
|
||||||
|
}
|
||||||
|
|
||||||
|
for name, c := range cases {
|
||||||
|
t.Run(name, func(t *testing.T) {
|
||||||
|
s, a, l := splitScheduleCommandTitle([]byte(c.in))
|
||||||
|
if s != c.schedule {
|
||||||
|
t.Error(s)
|
||||||
|
}
|
||||||
|
if a != c.command {
|
||||||
|
t.Error(a)
|
||||||
|
}
|
||||||
|
if l != c.title {
|
||||||
|
t.Error(l)
|
||||||
|
}
|
||||||
|
})
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
|
||||||
|
|
@ -35,6 +35,7 @@ func (s *Server) list(w http.ResponseWriter, r *http.Request) {
|
||||||
panic(err)
|
panic(err)
|
||||||
}
|
}
|
||||||
out[i]["id"] = j.Name
|
out[i]["id"] = j.Name
|
||||||
|
out[i]["title"] = j.Title
|
||||||
out[i]["cron"] = j.Schedule
|
out[i]["cron"] = j.Schedule
|
||||||
out[i]["language"] = j.Runner.String()
|
out[i]["language"] = j.Runner.String()
|
||||||
out[i]["script"] = j.Raw
|
out[i]["script"] = j.Raw
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue