feat: add json format output to cron:list command

This makes it easier to parse output in a machine-readable format - such as when interacting with cron tasks via jq for writing tests.
This commit is contained in:
Jose Diaz-Gonzalez
2023-07-02 04:36:08 -04:00
parent afc318526c
commit 820883e60c
6 changed files with 66 additions and 16 deletions

View File

@@ -1,6 +1,7 @@
package cron
import (
"encoding/json"
"fmt"
"github.com/dokku/dokku/plugins/common"
@@ -8,22 +9,40 @@ import (
)
// CommandList lists all scheduled cron tasks for a given app
func CommandList(appName string) error {
func CommandList(appName string, format string) error {
if err := common.VerifyAppName(appName); err != nil {
return err
}
if format == "" {
format = "stdout"
}
if format != "stdout" && format != "json" {
return fmt.Errorf("Invalid format specified, supported formats: json, stdout")
}
entries, err := FetchCronEntries(appName)
if err != nil {
return err
}
output := []string{"ID | Schedule | Command"}
for _, entry := range entries {
output = append(output, fmt.Sprintf("%s | %s | %s", entry.ID, entry.Schedule, entry.Command))
if format == "stdout" {
output := []string{"ID | Schedule | Command"}
for _, entry := range entries {
output = append(output, fmt.Sprintf("%s | %s | %s", entry.ID, entry.Schedule, entry.Command))
}
result := columnize.SimpleFormat(output)
fmt.Println(result)
return nil
}
result := columnize.SimpleFormat(output)
fmt.Println(result)
out, err := json.Marshal(entries)
if err != nil {
return err
}
common.Log(string(out))
return nil
}