add and test style from file

master
bel 2020-03-15 16:55:46 +00:00
parent c1fe373427
commit 9d51626e4b
4 changed files with 52 additions and 8 deletions

View File

@ -39,7 +39,7 @@ function init() {
}
function format(job) {
return `<tr><td><details>
<summary>(${job.last.status}) ${job.id}</summary>
<summary name="${job.id}">(${job.last.status}) ${job.title}</summary>
<table>
<tr>
<td><code>${job.cron}</code></td>

View File

@ -48,11 +48,11 @@ func NewFromFile(config string) (*Scheduler, error) {
if len(line) == 0 {
continue
}
schedule, command := splitScheduleCommand(line)
schedule, command, title := splitScheduleCommandTitle(line)
if len(schedule) == 0 || len(command) == 0 {
continue
}
job, err := NewJob(Bash, schedule, command)
job, err := newBashJob(schedule, command, title)
if err != nil {
logger.New().Error(err, "cannot fully parse file: new job error", config, ", sched", schedule, ", comm", command)
continue
@ -80,16 +80,20 @@ func cleanLine(b []byte) []byte {
return b
}
func splitScheduleCommand(b []byte) (string, string) {
func splitScheduleCommandTitle(b []byte) (string, string, string) {
re := regexp.MustCompile(`^((\d+|\*\/\d+|(\d,)*\d+|\*) [ ]*){5,6}`)
schedule := string(re.Find(b))
if len(schedule) == 0 {
return "", ""
return "", "", ""
}
command := strings.TrimPrefix(string(b), schedule)
commandTitle := strings.TrimPrefix(string(b), schedule)
schedule = strings.TrimSpace(schedule)
command = strings.TrimSpace(command)
return schedule, command
commandTitle = strings.TrimSpace(commandTitle)
title := ""
if i := strings.LastIndex(commandTitle, "#"); i >= 0 {
title = commandTitle[i+1:]
}
return schedule, commandTitle, title
}
func (s *Scheduler) Start() error {

View File

@ -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)
}
})
}
}

View File

@ -35,6 +35,7 @@ func (s *Server) list(w http.ResponseWriter, r *http.Request) {
panic(err)
}
out[i]["id"] = j.Name
out[i]["title"] = j.Title
out[i]["cron"] = j.Schedule
out[i]["language"] = j.Runner.String()
out[i]["script"] = j.Raw