/
store_list.go
80 lines (62 loc) · 2 KB
/
store_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
// Copyright (c) OpenFaaS Author(s) 2018. All rights reserved.
// Licensed under the MIT license. See LICENSE file in the project root for full license information.
package commands
import (
"bytes"
"fmt"
"strings"
"text/tabwriter"
storeV2 "github.com/openfaas/faas-cli/schema/store/v2"
"github.com/spf13/cobra"
)
func init() {
// Setup flags used by store command
storeListCmd.Flags().BoolVarP(&verbose, "verbose", "v", true, "Enable verbose output to see the full description of each function in the store")
storeCmd.AddCommand(storeListCmd)
}
var storeListCmd = &cobra.Command{
Use: `list [--url STORE_URL]`,
Aliases: []string{"ls"},
Short: "List available OpenFaaS functions in a store",
Example: ` faas-cli store list
faas-cli store list --verbose
faas-cli store list --url https://host:port/store.json`,
RunE: runStoreList,
}
func runStoreList(cmd *cobra.Command, args []string) error {
targetPlatform := getTargetPlatform(platformValue)
storeList, err := storeList(storeAddress)
if err != nil {
return err
}
filteredFunctions := filterStoreList(storeList, targetPlatform)
if len(filteredFunctions) == 0 {
availablePlatforms := getStorePlatforms(storeList)
fmt.Printf("No functions found in the store for platform '%s', try one of the following: %s\n", targetPlatform, strings.Join(availablePlatforms, ", "))
return nil
}
fmt.Print(storeRenderItems(filteredFunctions))
return nil
}
func storeRenderItems(items []storeV2.StoreFunction) string {
var b bytes.Buffer
w := tabwriter.NewWriter(&b, 0, 0, 1, ' ', 0)
fmt.Fprintln(w)
fmt.Fprintln(w, "FUNCTION\tAUTHOR\tDESCRIPTION")
for _, item := range items {
author := item.Author
if author == "" {
author = "unknown"
}
fmt.Fprintf(w, "%s\t%s\t%s\n", item.Name, author, storeRenderDescription(item.Title))
}
fmt.Fprintln(w)
w.Flush()
return b.String()
}
func storeRenderDescription(descr string) string {
if !verbose && len(descr) > maxDescriptionLen {
return descr[0:maxDescriptionLen-3] + "..."
}
return descr
}