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 committed 2023-08-05 10:58:57 -04:00
1 parent afc318526c
commit 820883e60c
6 files changed
+66 -16

No files matched your search

+12 -2
View File
@@ -3,8 +3,8 @@
> New as of 0.23.0 > New as of 0.23.0
``` ```
cron:list <app> # List scheduled cron tasks for an app cron:list <app> [--format json|stdout] # List scheduled cron tasks for an app
cron:report [<app>] [<flag>] # Display report about an app cron:report [<app>] [<flag>] # Display report about an app
``` ```
## Usage ## Usage
@@ -62,6 +62,16 @@ cGhwPT09cGhwIHRlc3QucGhwPT09QGRhaWx5 @daily node index.js
cGhwPT09dHJ1ZT09PSogKiAqICogKg== * * * * * true cGhwPT09dHJ1ZT09PSogKiAqICogKg== * * * * * true
``` ```
The output can also be displayed in json format:
```shell
dokku cron:list node-js-app --format json
```
```
[{"id":"cGhwPT09cGhwIHRlc3QucGhwPT09QGRhaWx5","app":"node-js-app","command":"node index.js","schedule":"@daily"}]
```
#### Displaying reports #### Displaying reports
You can get a report about the cron configuration for apps using the `cron:report` command: You can get a report about the cron configuration for apps using the `cron:report` command:
+6 -6
View File
@@ -14,12 +14,12 @@ import (
) )
type TemplateCommand struct { type TemplateCommand struct {
ID string ID string `json:"id"`
App string App string `json:"app"`
Command string Command string `json:"command"`
Schedule string Schedule string `json:"schedule"`
AltCommand string AltCommand string `json:"-"`
LogFile string LogFile string `json:"-"`
} }
func (t TemplateCommand) CronCommand() string { func (t TemplateCommand) CronCommand() string {
+1 -1
View File
@@ -18,7 +18,7 @@ Manage scheduled cron tasks
Additional commands:` Additional commands:`
helpContent = ` helpContent = `
cron:list <app>, List scheduled cron tasks for an app cron:list <app> [--format json|stdout], List scheduled cron tasks for an app
cron:report [<app>] [<flag>], Display report about an app cron:report [<app>] [<flag>], Display report about an app
` `
) )
+2 -1
View File
@@ -20,9 +20,10 @@ func main() {
switch subcommand { switch subcommand {
case "list": case "list":
args := flag.NewFlagSet("cron:list", flag.ExitOnError) args := flag.NewFlagSet("cron:list", flag.ExitOnError)
format := args.String("format", "stdout", "format: [ stdout | json ]")
args.Parse(os.Args[2:]) args.Parse(os.Args[2:])
appName := args.Arg(0) appName := args.Arg(0)
err = cron.CommandList(appName) err = cron.CommandList(appName, *format)
case "report": case "report":
args := flag.NewFlagSet("cron:report", flag.ExitOnError) args := flag.NewFlagSet("cron:report", flag.ExitOnError)
format := args.String("format", "stdout", "format: [ stdout | json ]") format := args.String("format", "stdout", "format: [ stdout | json ]")
+25 -6
View File
@@ -1,6 +1,7 @@
package cron package cron
import ( import (
"encoding/json"
"fmt" "fmt"
"github.com/dokku/dokku/plugins/common" "github.com/dokku/dokku/plugins/common"
@@ -8,22 +9,40 @@ import (
) )
// CommandList lists all scheduled cron tasks for a given app // 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 { if err := common.VerifyAppName(appName); err != nil {
return err 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) entries, err := FetchCronEntries(appName)
if err != nil { if err != nil {
return err return err
} }
output := []string{"ID | Schedule | Command"} if format == "stdout" {
for _, entry := range entries { output := []string{"ID | Schedule | Command"}
output = append(output, fmt.Sprintf("%s | %s | %s", entry.ID, entry.Schedule, entry.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 return nil
} }
+20
View File
@@ -177,6 +177,26 @@ teardown() {
assert_failure assert_failure
} }
@test "(cron) cron:list --format json" {
run deploy_app python dokku@dokku.me:$TEST_APP template_cron_file_valid
echo "output: $output"
echo "status: $status"
assert_success
run /bin/bash -c "dokku cron:list $TEST_APP --format json"
echo "output: $output"
echo "status: $status"
assert_success
assert_output_exists
cron_id="$(dokku cron:list $TEST_APP --format json | jq -r '.[0].id')"
run /bin/bash -c "echo $cron_id"
echo "output: $output"
echo "status: $status"
assert_success
assert_output_exists
}
template_cron_file_invalid() { template_cron_file_invalid() {
local APP="$1" local APP="$1"
local APP_REPO_DIR="$2" local APP_REPO_DIR="$2"