-
Notifications
You must be signed in to change notification settings - Fork 5
/
commands.go
132 lines (116 loc) · 2.92 KB
/
commands.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
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
package flexvolume
import (
"encoding/json"
"fmt"
"github.com/urfave/cli"
)
func Commands(fv FlexVolume) []cli.Command {
return []cli.Command{
{
Name: "init",
Usage: "Initialize the driver",
Action: func(c *cli.Context) error {
return handle(fv.Init())
},
},
{
Name: "getvolumename",
Usage: "Returns the unique name of the volume",
Action: func(c *cli.Context) error {
var opts map[string]string
if err := json.Unmarshal([]byte(c.Args().Get(1)), &opts); err != nil {
return err
}
return handle(fv.GetVolumeName(opts))
},
},
{
Name: "attach",
Usage: "Attach the volume",
Action: func(c *cli.Context) error {
var opts map[string]string
if err := json.Unmarshal([]byte(c.Args().Get(1)), &opts); err != nil {
return err
}
return handle(fv.Attach(opts))
},
},
{
Name: "waitforattach",
Usage: "Waits until a volume is fully attached to a node and its device emerges",
Action: func(c *cli.Context) error {
var opts map[string]string
if err := json.Unmarshal([]byte(c.Args().Get(2)), &opts); err != nil {
return err
}
return handle(fv.WaitForAttach(c.Args().Get(1), opts))
},
},
{
Name: "detach",
Usage: "Detach the volume",
Action: func(c *cli.Context) error {
return handle(fv.Detach(c.Args().Get(1), c.Args().Get(2)))
},
},
{
Name: "isattached",
Usage: "Checks that a volume is attached to a node",
Action: func(c *cli.Context) error {
var opts map[string]string
if err := json.Unmarshal([]byte(c.Args().Get(1)), &opts); err != nil {
return err
}
return handle(fv.Detach(opts, c.Args().Get(2)))
},
},
{
Name: "mountdevice",
Usage: "Mounts a volume’s device to a directory",
Action: func(c *cli.Context) error {
var opts map[string]string
if err := json.Unmarshal([]byte(c.Args().Get(3)), &opts); err != nil {
return err
}
return handle(fv.MountDevice(c.Args().Get(1), c.Args().Get(2), opts))
},
},
{
Name: "unmountdevice",
Usage: "Unmounts a volume’s device from a directory",
Action: func(c *cli.Context) error {
return handle(fv.UnmountDevice(c.Args().Get(1)))
},
},
{
Name: "mount",
Usage: "Mount the volume",
Action: func(c *cli.Context) error {
var opts map[string]string
if err := json.Unmarshal([]byte(c.Args().Get(1)), &opts); err != nil {
return err
}
return handle(fv.Mount(c.Args().Get(1), c.Args().Get(2), opts))
},
},
{
Name: "umount",
Usage: "Mount the volume",
Action: func(c *cli.Context) error {
return handle(fv.Unmount(c.Args().Get(1)))
},
},
}
}
// The following handles:
// * Output of the Response object.
// * Sets an error so we can bubble up an error code.
func handle(resp Response) error {
// Format the output as JSON.
output, err := json.Marshal(resp)
if err != nil {
return err
}
fmt.Println(string(output))
return nil
}