Skip to content

Commit 6e1d484

Browse files
committed
Add sort to the list command
Allows listing by name and invocations. Signed-off-by: Alex Ellis (OpenFaaS Ltd) <[email protected]>
1 parent 6b5e7a1 commit 6e1d484

File tree

1 file changed

+22
-0
lines changed

1 file changed

+22
-0
lines changed

commands/list.go

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,15 +7,18 @@ import (
77
"context"
88
"fmt"
99
"os"
10+
"sort"
1011

1112
"github.com/openfaas/faas-cli/proxy"
1213
"github.com/openfaas/faas-cli/stack"
14+
"github.com/openfaas/faas-provider/types"
1315
"github.com/spf13/cobra"
1416
)
1517

1618
var (
1719
verboseList bool
1820
token string
21+
sortOrder string
1922
)
2023

2124
func init() {
@@ -28,6 +31,7 @@ func init() {
2831
listCmd.Flags().BoolVar(&tlsInsecure, "tls-no-verify", false, "Disable TLS validation")
2932
listCmd.Flags().BoolVar(&envsubst, "envsubst", true, "Substitute environment variables in stack.yml file")
3033
listCmd.Flags().StringVarP(&token, "token", "k", "", "Pass a JWT token to use instead of basic auth")
34+
listCmd.Flags().StringVar(&sortOrder, "sort", "name", "Sort the functions by \"name\" or \"invocations\"")
3135

3236
faasCmd.AddCommand(listCmd)
3337
}
@@ -74,6 +78,12 @@ func runList(cmd *cobra.Command, args []string) error {
7478
return err
7579
}
7680

81+
if sortOrder == "name" {
82+
sort.Sort(byName(functions))
83+
} else if sortOrder == "invocations" {
84+
sort.Sort(byInvocations(functions))
85+
}
86+
7787
if quiet {
7888
for _, function := range functions {
7989
fmt.Printf("%s\n", function.Name)
@@ -103,3 +113,15 @@ func runList(cmd *cobra.Command, args []string) error {
103113
}
104114
return nil
105115
}
116+
117+
type byName []types.FunctionStatus
118+
119+
func (a byName) Len() int { return len(a) }
120+
func (a byName) Swap(i, j int) { a[i], a[j] = a[j], a[i] }
121+
func (a byName) Less(i, j int) bool { return a[i].Name < a[j].Name }
122+
123+
type byInvocations []types.FunctionStatus
124+
125+
func (a byInvocations) Len() int { return len(a) }
126+
func (a byInvocations) Swap(i, j int) { a[i], a[j] = a[j], a[i] }
127+
func (a byInvocations) Less(i, j int) bool { return a[i].InvocationCount > a[j].InvocationCount }

0 commit comments

Comments
 (0)