-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathactivities.go
More file actions
80 lines (72 loc) · 1.85 KB
/
Copy pathactivities.go
File metadata and controls
80 lines (72 loc) · 1.85 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
package cronjobs
import (
"fmt"
"github.com/pterm/pterm"
"github.com/urfave/cli/v2"
"github.com/NodeOps-app/createos-cli/internal/api"
"github.com/NodeOps-app/createos-cli/internal/output"
)
func newCronjobsActivitiesCommand() *cli.Command {
return &cli.Command{
Name: "activities",
Usage: "Show recent execution history for a cron job",
Flags: []cli.Flag{
&cli.StringFlag{Name: "project", Usage: "Project ID"},
&cli.StringFlag{Name: "cronjob", Usage: "Cron job ID"},
},
Action: func(c *cli.Context) error {
client, ok := c.App.Metadata[api.ClientKey].(*api.APIClient)
if !ok {
return fmt.Errorf("you're not signed in — run 'createos login' to get started")
}
projectID, cronjobID, err := resolveCronjob(c, client)
if err != nil {
return err
}
activities, err := client.ListCronjobActivities(projectID, cronjobID)
if err != nil {
return err
}
output.Render(c, activities, func() {
if len(activities) == 0 {
fmt.Println("No execution history found for this cron job yet.")
return
}
tableData := pterm.TableData{
{"ID", "Success", "Status Code", "Scheduled At", "Log"},
}
for _, a := range activities {
success := "-"
if a.Success != nil {
if *a.Success {
success = "yes"
} else {
success = "no"
}
}
statusCode := "-"
if a.StatusCode != nil {
statusCode = fmt.Sprintf("%d", *a.StatusCode)
}
log := "-"
if a.Log != nil && *a.Log != "" {
log = *a.Log
if len(log) > 80 {
log = log[:77] + "..."
}
}
tableData = append(tableData, []string{
a.ID,
success,
statusCode,
a.ScheduledAt.Format("2006-01-02 15:04:05"),
log,
})
}
_ = pterm.DefaultTable.WithHasHeader().WithData(tableData).Render()
fmt.Println()
})
return nil
},
}
}