forked from nickschuch/flexvolume
-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathcommands.go
71 lines (65 loc) · 1.43 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
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: "attach",
Usage: "Attach the volume",
Action: func(c *cli.Context) error {
var opts map[string]string
if err := json.Unmarshal([]byte(c.Args().Get(0)), &opts); err != nil {
return err
}
return handle(fv.Attach(opts))
},
},
{
Name: "detach",
Usage: "Detach the volume",
Action: func(c *cli.Context) error {
return handle(fv.Detach(c.Args().Get(0)))
},
},
{
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(2)), &opts); err != nil {
return err
}
return handle(fv.Mount(c.Args().Get(0), c.Args().Get(1), opts))
},
},
{
Name: "unmount",
Usage: "Mount the volume",
Action: func(c *cli.Context) error {
return handle(fv.Unmount(c.Args().Get(0)))
},
},
}
}
// 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
}