-
Notifications
You must be signed in to change notification settings - Fork 67
/
list_cmd.go
61 lines (50 loc) 路 1.59 KB
/
list_cmd.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
package cmd
import (
"context"
"fmt"
"strings"
"github.com/kubeshop/tracetest/cli/parameters"
"github.com/kubeshop/tracetest/cli/pkg/resourcemanager"
"github.com/spf13/cobra"
)
var listParams = parameters.ListParams{}
var listCmd = &cobra.Command{
GroupID: cmdGroupResources.ID,
Use: fmt.Sprintf("list %s", strings.Join(parameters.ValidResources, "|")),
Short: "List resources",
Long: "List resources from your Tracetest server",
PreRun: setupCommand(),
Run: WithResourceMiddleware(func(_ *cobra.Command, args []string) (string, error) {
resourceType := args[0]
ctx := context.Background()
resourceClient, err := resources.Get(resourceType)
if err != nil {
return "", err
}
resultFormat, err := resourcemanager.Formats.Get(output)
if err != nil {
return "", err
}
lp := resourcemanager.ListOption{
Take: listParams.Take,
Skip: listParams.Skip,
SortBy: listParams.SortBy,
SortDirection: listParams.SortDirection,
All: listParams.All,
}
result, err := resourceClient.List(ctx, lp, resultFormat)
if err != nil {
return "", err
}
return result, nil
}, listParams),
PostRun: teardownCommand,
}
func init() {
listCmd.Flags().Int32Var(&listParams.Take, "take", 20, "Take number")
listCmd.Flags().Int32Var(&listParams.Skip, "skip", 0, "Skip number")
listCmd.Flags().StringVar(&listParams.SortBy, "sortBy", "", "Sort by")
listCmd.Flags().StringVar(&listParams.SortDirection, "sortDirection", "desc", "Sort direction")
listCmd.Flags().BoolVar(&listParams.All, "all", false, "All")
rootCmd.AddCommand(listCmd)
}