-
Notifications
You must be signed in to change notification settings - Fork 72
/
controller_list_snapshots.go
89 lines (75 loc) · 1.78 KB
/
controller_list_snapshots.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
81
82
83
84
85
86
87
88
89
package cmd
import (
"context"
"os"
"github.com/spf13/cobra"
"github.com/container-storage-interface/spec/lib/go/csi"
"github.com/rexray/gocsi/utils"
)
var listSnapshots struct {
maxEntries int32
startingToken string
paging bool
}
var listSnapshotsCmd = &cobra.Command{
Use: "list-snapshots",
Aliases: []string{"sl", "snap-list", "snapshots"},
Short: `invokes the rpc "ListSnapshots"`,
RunE: func(*cobra.Command, []string) error {
ctx, cancel := context.WithTimeout(root.ctx, root.timeout)
defer cancel()
req := csi.ListSnapshotsRequest{
MaxEntries: listSnapshots.maxEntries,
StartingToken: listSnapshots.startingToken,
}
// If auto-paging is not enabled then send a normal request.
if !listSnapshots.paging {
rep, err := controller.client.ListSnapshots(ctx, &req)
if err != nil {
return err
}
return root.tpl.Execute(os.Stdout, rep)
}
// Paging is enabled.
cvol, cerr := utils.PageSnapshots(ctx, controller.client, req)
for {
select {
case v, ok := <-cvol:
if !ok {
return nil
}
if err := root.tpl.Execute(os.Stdout, v); err != nil {
return err
}
case e, ok := <-cerr:
if !ok {
return nil
}
return e
}
}
},
}
func init() {
controllerCmd.AddCommand(listSnapshotsCmd)
listSnapshotsCmd.Flags().Int32Var(
&listSnapshots.maxEntries,
"max-entries",
0,
"The maximum number of entries to return")
listSnapshotsCmd.Flags().StringVar(
&listSnapshots.startingToken,
"starting-token",
"",
"The starting token used to retrieve paged data")
listSnapshotsCmd.Flags().BoolVar(
&listSnapshots.paging,
"paging",
false,
"Enables auto-paging")
listSnapshotsCmd.Flags().StringVar(
&root.format,
"format",
"",
"The Go template format used to emit the results")
}