Skip to content

Commit 29ca077

Browse files
authored
feat: list command - improved output (#205)
Signed-off-by: Zbynek Roubalik <zroubali@redhat.com>
1 parent 745640d commit 29ca077

File tree

6 files changed

+70
-29
lines changed

6 files changed

+70
-29
lines changed

client.go

+11-3
Original file line numberDiff line numberDiff line change
@@ -67,7 +67,15 @@ type Remover interface {
6767
// Lister of deployed services.
6868
type Lister interface {
6969
// List the Functions currently deployed.
70-
List() ([]string, error)
70+
List() ([]ListItem, error)
71+
}
72+
73+
type ListItem struct {
74+
Name string `json:"name" yaml:"name"`
75+
Runtime string `json:"runtime" yaml:"runtime"`
76+
URL string `json:"url" yaml:"url"`
77+
KService string `json:"kservice" yaml:"kservice"`
78+
Ready string `json:"ready" yaml:"ready"`
7179
}
7280

7381
// ProgressListener is notified of task progress.
@@ -476,7 +484,7 @@ func (c *Client) Run(root string) error {
476484
}
477485

478486
// List currently deployed Functions.
479-
func (c *Client) List() ([]string, error) {
487+
func (c *Client) List() ([]ListItem, error) {
480488
// delegate to concrete implementation of lister entirely.
481489
return c.lister.List()
482490
}
@@ -549,7 +557,7 @@ func (n *noopRemover) Remove(string) error { return nil }
549557

550558
type noopLister struct{ output io.Writer }
551559

552-
func (n *noopLister) List() ([]string, error) { return []string{}, nil }
560+
func (n *noopLister) List() ([]ListItem, error) { return []ListItem{}, nil }
553561

554562
type noopDNSProvider struct{ output io.Writer }
555563

cmd/completion_util.go

+5-2
Original file line numberDiff line numberDiff line change
@@ -18,12 +18,15 @@ func CompleteFunctionList(cmd *cobra.Command, args []string, toComplete string)
1818
directive = cobra.ShellCompDirectiveError
1919
return
2020
}
21-
s, err := lister.List()
21+
list, err := lister.List()
2222
if err != nil {
2323
directive = cobra.ShellCompDirectiveError
2424
return
2525
}
26-
strings = s
26+
27+
for _, item := range list{
28+
strings = append(strings, item.Name)
29+
}
2730
directive = cobra.ShellCompDirectiveDefault
2831
return
2932
}

cmd/list.go

+22-17
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ import (
66
"fmt"
77
"io"
88
"os"
9+
"text/tabwriter"
910

1011
"github.com/ory/viper"
1112
"github.com/spf13/cobra"
@@ -54,12 +55,13 @@ func runList(cmd *cobra.Command, args []string) (err error) {
5455
faas.WithVerbose(config.Verbose),
5556
faas.WithLister(lister))
5657

57-
nn, err := client.List()
58+
items, err := client.List()
5859
if err != nil {
5960
return
6061
}
6162

62-
write(os.Stdout, names(nn), config.Format)
63+
write(os.Stdout, listItems(items), config.Format)
64+
6365
return
6466
}
6567

@@ -83,30 +85,33 @@ func newListConfig() listConfig {
8385
// Output Formatting (serializers)
8486
// -------------------------------
8587

86-
type names []string
88+
type listItems []faas.ListItem
8789

88-
func (nn names) Human(w io.Writer) error {
89-
return nn.Plain(w)
90+
func (items listItems) Human(w io.Writer) error {
91+
return items.Plain(w)
9092
}
9193

92-
func (nn names) Plain(w io.Writer) error {
93-
for _, name := range nn {
94-
fmt.Fprintln(w, name)
94+
func (items listItems) Plain(w io.Writer) error {
95+
96+
// minwidth, tabwidth, padding, padchar, flags
97+
tabWriter := tabwriter.NewWriter(w, 0, 8, 2, ' ', 0)
98+
defer tabWriter.Flush()
99+
100+
fmt.Fprintf(tabWriter, "%s\t%s\t%s\t%s\t%s\n", "NAME", "RUNTIME", "URL", "KSERVICE", "READY")
101+
for _, item := range items {
102+
fmt.Fprintf(tabWriter, "%s\t%s\t%s\t%s\t%s\n", item.Name, item.Runtime, item.URL, item.KService, item.Ready)
95103
}
96104
return nil
97105
}
98106

99-
func (nn names) JSON(w io.Writer) error {
100-
return json.NewEncoder(w).Encode(nn)
107+
func (items listItems) JSON(w io.Writer) error {
108+
return json.NewEncoder(w).Encode(items)
101109
}
102110

103-
func (nn names) XML(w io.Writer) error {
104-
return xml.NewEncoder(w).Encode(nn)
111+
func (items listItems) XML(w io.Writer) error {
112+
return xml.NewEncoder(w).Encode(items)
105113
}
106114

107-
func (nn names) YAML(w io.Writer) error {
108-
// the yaml.v2 package refuses to directly serialize a []string unless
109-
// exposed as a public struct member; so an inline anonymous is used.
110-
ff := struct{ Names []string }{nn}
111-
return yaml.NewEncoder(w).Encode(ff.Names)
115+
func (items listItems) YAML(w io.Writer) error {
116+
return yaml.NewEncoder(w).Encode(items)
112117
}

go.mod

+1
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ require (
1515
k8s.io/client-go v11.0.1-0.20190805182717-6502b5e7b1b5+incompatible
1616
knative.dev/client v0.17.2
1717
knative.dev/eventing v0.17.5
18+
knative.dev/pkg v0.0.0-20200831162708-14fb2347fb77
1819
knative.dev/serving v0.17.3
1920
)
2021

knative/lister.go

+26-4
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,11 @@
11
package knative
22

33
import (
4+
corev1 "k8s.io/api/core/v1"
45
clientservingv1 "knative.dev/client/pkg/serving/v1"
6+
"knative.dev/pkg/apis"
57

8+
"github.com/boson-project/faas"
69
"github.com/boson-project/faas/k8s"
710
)
811

@@ -28,7 +31,7 @@ func NewLister(namespaceOverride string) (l *Lister, err error) {
2831
return
2932
}
3033

31-
func (l *Lister) List() (names []string, err error) {
34+
func (l *Lister) List() (items []faas.ListItem, err error) {
3235

3336
client, err := NewServingClient(l.namespace)
3437
if err != nil {
@@ -41,13 +44,32 @@ func (l *Lister) List() (names []string, err error) {
4144
}
4245

4346
for _, service := range lst.Items {
47+
4448
// Convert the "subdomain-encoded" (i.e. kube-service-friendly) name
4549
// back out to a fully qualified service name.
46-
n, err := k8s.FromK8sAllowedName(service.Name)
50+
name, err := k8s.FromK8sAllowedName(service.Name)
4751
if err != nil {
48-
return names, err
52+
return items, err
53+
}
54+
55+
// get status
56+
ready := corev1.ConditionUnknown
57+
for _, con := range service.Status.Conditions {
58+
if con.Type == apis.ConditionReady {
59+
ready = con.Status
60+
break
61+
}
4962
}
50-
names = append(names, n)
63+
64+
listItem := faas.ListItem{
65+
Name: name,
66+
Runtime: service.Labels["boson.dev/runtime"],
67+
KService: service.Name,
68+
URL: service.Status.URL.String(),
69+
Ready: string(ready),
70+
}
71+
72+
items = append(items, listItem)
5173
}
5274
return
5375
}

mock/lister.go

+5-3
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,19 @@
11
package mock
22

3+
import "github.com/boson-project/faas"
4+
35
type Lister struct {
46
ListInvoked bool
5-
ListFn func() ([]string, error)
7+
ListFn func() ([]faas.ListItem, error)
68
}
79

810
func NewLister() *Lister {
911
return &Lister{
10-
ListFn: func() ([]string, error) { return []string{}, nil },
12+
ListFn: func() ([]faas.ListItem, error) { return []faas.ListItem{}, nil },
1113
}
1214
}
1315

14-
func (l *Lister) List() ([]string, error) {
16+
func (l *Lister) List() ([]faas.ListItem, error) {
1517
l.ListInvoked = true
1618
return l.ListFn()
1719
}

0 commit comments

Comments
 (0)