forked from argoproj/argo-workflows
-
Notifications
You must be signed in to change notification settings - Fork 0
/
list.go
91 lines (85 loc) · 2.96 KB
/
list.go
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
81
82
83
84
85
86
87
88
89
90
91
package cron
import (
"fmt"
"log"
"os"
"text/tabwriter"
"time"
"github.com/argoproj/pkg/errors"
"github.com/argoproj/pkg/humanize"
"github.com/spf13/cobra"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/labels"
"github.com/argoproj/argo-workflows/v3/cmd/argo/commands/client"
cronworkflowpkg "github.com/argoproj/argo-workflows/v3/pkg/apiclient/cronworkflow"
wfv1 "github.com/argoproj/argo-workflows/v3/pkg/apis/workflow/v1alpha1"
)
type listFlags struct {
allNamespaces bool // --all-namespaces
output string // --output
}
func NewListCommand() *cobra.Command {
var listArgs listFlags
command := &cobra.Command{
Use: "list",
Short: "list cron workflows",
Run: func(cmd *cobra.Command, args []string) {
ctx, apiClient := client.NewAPIClient(cmd.Context())
serviceClient, err := apiClient.NewCronWorkflowServiceClient()
errors.CheckError(err)
namespace := client.Namespace()
if listArgs.allNamespaces {
namespace = ""
}
listOpts := metav1.ListOptions{}
labelSelector := labels.NewSelector()
listOpts.LabelSelector = labelSelector.String()
cronWfList, err := serviceClient.ListCronWorkflows(ctx, &cronworkflowpkg.ListCronWorkflowsRequest{
Namespace: namespace,
ListOptions: &listOpts,
})
errors.CheckError(err)
switch listArgs.output {
case "", "wide":
printTable(cronWfList.Items, &listArgs)
case "name":
for _, cronWf := range cronWfList.Items {
fmt.Println(cronWf.ObjectMeta.Name)
}
default:
log.Fatalf("Unknown output mode: %s", listArgs.output)
}
},
}
command.Flags().BoolVarP(&listArgs.allNamespaces, "all-namespaces", "A", false, "Show workflows from all namespaces")
command.Flags().StringVarP(&listArgs.output, "output", "o", "", "Output format. One of: wide|name")
return command
}
func printTable(wfList []wfv1.CronWorkflow, listArgs *listFlags) {
w := tabwriter.NewWriter(os.Stdout, 0, 0, 3, ' ', 0)
if listArgs.allNamespaces {
_, _ = fmt.Fprint(w, "NAMESPACE\t")
}
_, _ = fmt.Fprint(w, "NAME\tAGE\tLAST RUN\tNEXT RUN\tSCHEDULE\tTIMEZONE\tSUSPENDED")
_, _ = fmt.Fprint(w, "\n")
for _, cwf := range wfList {
if listArgs.allNamespaces {
_, _ = fmt.Fprintf(w, "%s\t", cwf.ObjectMeta.Namespace)
}
var cleanLastScheduledTime string
if cwf.Status.LastScheduledTime != nil {
cleanLastScheduledTime = humanize.RelativeDurationShort(cwf.Status.LastScheduledTime.Time, time.Now())
} else {
cleanLastScheduledTime = "N/A"
}
var cleanNextScheduledTime string
if next, err := GetNextRuntime(&cwf); err == nil {
cleanNextScheduledTime = humanize.RelativeDurationShort(next, time.Now())
} else {
cleanNextScheduledTime = "N/A"
}
_, _ = fmt.Fprintf(w, "%s\t%s\t%s\t%s\t%s\t%s\t%t", cwf.ObjectMeta.Name, humanize.RelativeDurationShort(cwf.ObjectMeta.CreationTimestamp.Time, time.Now()), cleanLastScheduledTime, cleanNextScheduledTime, cwf.Spec.Schedule, cwf.Spec.Timezone, cwf.Spec.Suspend)
_, _ = fmt.Fprintf(w, "\n")
}
_ = w.Flush()
}