Merge pull request #6463 from dokku/cron-appjson
refactor: use exported function from appjson module instead of manually parsing
This commit is contained in:
@@ -1,13 +1,9 @@
|
|||||||
package cron
|
package cron
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"encoding/json"
|
|
||||||
"fmt"
|
"fmt"
|
||||||
"os"
|
|
||||||
"strings"
|
|
||||||
|
|
||||||
appjson "github.com/dokku/dokku/plugins/app-json"
|
appjson "github.com/dokku/dokku/plugins/app-json"
|
||||||
"github.com/dokku/dokku/plugins/common"
|
|
||||||
|
|
||||||
"github.com/multiformats/go-base36"
|
"github.com/multiformats/go-base36"
|
||||||
cronparser "github.com/robfig/cron/v3"
|
cronparser "github.com/robfig/cron/v3"
|
||||||
@@ -25,15 +21,28 @@ var (
|
|||||||
}
|
}
|
||||||
)
|
)
|
||||||
|
|
||||||
|
// TemplateCommand is a struct that represents a cron command
|
||||||
type TemplateCommand struct {
|
type TemplateCommand struct {
|
||||||
ID string `json:"id"`
|
// ID is a unique identifier for the cron command
|
||||||
App string `json:"app"`
|
ID string `json:"id"`
|
||||||
Command string `json:"command"`
|
|
||||||
Schedule string `json:"schedule"`
|
// App is the app the cron command belongs to
|
||||||
|
App string `json:"app"`
|
||||||
|
|
||||||
|
// Command is the command to run
|
||||||
|
Command string `json:"command"`
|
||||||
|
|
||||||
|
// Schedule is the cron schedule
|
||||||
|
Schedule string `json:"schedule"`
|
||||||
|
|
||||||
|
// AltCommand is an alternate command to run
|
||||||
AltCommand string `json:"-"`
|
AltCommand string `json:"-"`
|
||||||
LogFile string `json:"-"`
|
|
||||||
|
// LogFile is the log file to write to
|
||||||
|
LogFile string `json:"-"`
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// CronCommand returns the command to run for a given cron command
|
||||||
func (t TemplateCommand) CronCommand() string {
|
func (t TemplateCommand) CronCommand() string {
|
||||||
if t.AltCommand != "" {
|
if t.AltCommand != "" {
|
||||||
if t.LogFile != "" {
|
if t.LogFile != "" {
|
||||||
@@ -45,27 +54,18 @@ func (t TemplateCommand) CronCommand() string {
|
|||||||
return fmt.Sprintf("dokku run --cron-id %s %s -- %s", t.ID, t.App, t.Command)
|
return fmt.Sprintf("dokku run --cron-id %s %s -- %s", t.ID, t.App, t.Command)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// FetchCronEntries returns a list of cron commands for a given app
|
||||||
func FetchCronEntries(appName string) ([]TemplateCommand, error) {
|
func FetchCronEntries(appName string) ([]TemplateCommand, error) {
|
||||||
commands := []TemplateCommand{}
|
commands := []TemplateCommand{}
|
||||||
appjsonPath := appjson.GetAppjsonPath(appName)
|
appJSON, err := appjson.GetAppJSON(appName)
|
||||||
if !common.FileExists(appjsonPath) {
|
|
||||||
return commands, nil
|
|
||||||
}
|
|
||||||
|
|
||||||
b, err := os.ReadFile(appjsonPath)
|
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return commands, fmt.Errorf("Cannot read app.json file for %s: %v", appName, err)
|
return commands, err
|
||||||
}
|
}
|
||||||
|
|
||||||
if strings.TrimSpace(string(b)) == "" {
|
if appJSON.Cron == nil {
|
||||||
return commands, nil
|
return commands, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
var appJSON appjson.AppJSON
|
|
||||||
if err = json.Unmarshal(b, &appJSON); err != nil {
|
|
||||||
return commands, fmt.Errorf("Cannot parse app.json for %s: %v", appName, err)
|
|
||||||
}
|
|
||||||
|
|
||||||
for _, c := range appJSON.Cron {
|
for _, c := range appJSON.Cron {
|
||||||
parser := cronparser.NewParser(cronparser.Minute | cronparser.Hour | cronparser.Dom | cronparser.Month | cronparser.Dow | cronparser.Descriptor)
|
parser := cronparser.NewParser(cronparser.Minute | cronparser.Hour | cronparser.Dom | cronparser.Month | cronparser.Dow | cronparser.Descriptor)
|
||||||
_, err := parser.Parse(c.Schedule)
|
_, err := parser.Parse(c.Schedule)
|
||||||
|
|||||||
Reference in New Issue
Block a user