forked from argoproj/argo-workflows
/
list.go
86 lines (78 loc) · 2.43 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
package cron
import (
"fmt"
"log"
"os"
"text/tabwriter"
"time"
"github.com/argoproj/pkg/humanize"
"github.com/spf13/cobra"
apiv1 "k8s.io/api/core/v1"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/labels"
wfv1 "github.com/argoproj/argo/pkg/apis/workflow/v1alpha1"
"github.com/argoproj/argo/pkg/client/clientset/versioned/typed/workflow/v1alpha1"
)
type listFlags struct {
allNamespaces bool // --all-namespaces
output string // --output
}
func NewListCommand() *cobra.Command {
var (
listArgs listFlags
)
var command = &cobra.Command{
Use: "list",
Short: "list cron workflows",
Run: func(cmd *cobra.Command, args []string) {
var cronWfClient v1alpha1.CronWorkflowInterface
if listArgs.allNamespaces {
cronWfClient = InitCronWorkflowClient(apiv1.NamespaceAll)
} else {
cronWfClient = InitCronWorkflowClient()
}
listOpts := metav1.ListOptions{}
labelSelector := labels.NewSelector()
listOpts.LabelSelector = labelSelector.String()
cronWfList, err := cronWfClient.List(listOpts)
if err != nil {
log.Fatal(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().BoolVar(&listArgs.allNamespaces, "all-namespaces", 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\tSCHEDULE\tSUSPENDED")
fmt.Fprint(w, "\n")
for _, wf := range wfList {
if listArgs.allNamespaces {
fmt.Fprintf(w, "%s\t", wf.ObjectMeta.Namespace)
}
var cleanLastScheduledTime string
if wf.Status.LastScheduledTime != nil {
cleanLastScheduledTime = humanize.RelativeDurationShort(wf.Status.LastScheduledTime.Time, time.Now())
} else {
cleanLastScheduledTime = "N/A"
}
fmt.Fprintf(w, "%s\t%s\t%s\t%s\t%t", wf.ObjectMeta.Name, humanize.RelativeDurationShort(wf.ObjectMeta.CreationTimestamp.Time, time.Now()), cleanLastScheduledTime, wf.Spec.Schedule, wf.Spec.Suspend)
fmt.Fprintf(w, "\n")
}
_ = w.Flush()
}