forked from argoproj/argo-workflows
/
list.go
77 lines (70 loc) · 2 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
package template
import (
"fmt"
"log"
"os"
"text/tabwriter"
"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 workflow templates",
Run: func(cmd *cobra.Command, args []string) {
var wftmplClient v1alpha1.WorkflowTemplateInterface
if listArgs.allNamespaces {
wftmplClient = InitWorkflowTemplateClient(apiv1.NamespaceAll)
} else {
wftmplClient = InitWorkflowTemplateClient()
}
listOpts := metav1.ListOptions{}
labelSelector := labels.NewSelector()
listOpts.LabelSelector = labelSelector.String()
wftmplList, err := wftmplClient.List(listOpts)
if err != nil {
log.Fatal(err)
}
switch listArgs.output {
case "", "wide":
printTable(wftmplList.Items, &listArgs)
case "name":
for _, wftmp := range wftmplList.Items {
fmt.Println(wftmp.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.WorkflowTemplate, listArgs *listFlags) {
w := tabwriter.NewWriter(os.Stdout, 0, 0, 3, ' ', 0)
if listArgs.allNamespaces {
fmt.Fprint(w, "NAMESPACE\t")
}
fmt.Fprint(w, "NAME")
fmt.Fprint(w, "\n")
for _, wf := range wfList {
if listArgs.allNamespaces {
fmt.Fprintf(w, "%s\t", wf.ObjectMeta.Namespace)
}
fmt.Fprintf(w, "%s\t", wf.ObjectMeta.Name)
fmt.Fprintf(w, "\n")
}
_ = w.Flush()
}