forked from digitalocean/doctl
/
volume_actions.go
87 lines (70 loc) · 1.85 KB
/
volume_actions.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
package commands
import (
"strconv"
"github.com/digitalocean/doctl"
"github.com/digitalocean/doctl/do"
"github.com/spf13/cobra"
)
type volumeActionFn func(das do.VolumeActionsService) (*do.Action, error)
func performVolumeAction(c *CmdConfig, fn volumeActionFn) error {
das := c.VolumeActions()
a, err := fn(das)
if err != nil {
return err
}
wait, err := c.Doit.GetBool(c.NS, doctl.ArgCommandWait)
if err != nil {
return err
}
if wait {
a, err = actionWait(c, a.ID, 5)
if err != nil {
return err
}
}
item := &action{actions: do.Actions{*a}}
return c.Display(item)
}
// VolumeAction creates the volume command
func VolumeAction() *Command {
cmd := &Command{
Command: &cobra.Command{
Use: "volume-action",
Short: "volume action commands",
Long: "volume-action is used to access volume action commands",
},
}
CmdBuilder(cmd, RunVolumeAttach, "attach <volume-id> <droplet-id>", "attach a volume", Writer,
aliasOpt("a"))
CmdBuilder(cmd, RunVolumeDetach, "detach <volume-id>", "detach a volume", Writer,
aliasOpt("d"))
return cmd
}
// RunVolumeAttach attaches a volume to a droplet.
func RunVolumeAttach(c *CmdConfig) error {
fn := func(das do.VolumeActionsService) (*do.Action, error) {
if len(c.Args) != 2 {
return nil, doctl.NewMissingArgsErr(c.NS)
}
volumeID := c.Args[0]
dropletID, err := strconv.Atoi(c.Args[1])
if err != nil {
return nil, err
}
a, err := das.Attach(volumeID, dropletID)
return a, err
}
return performVolumeAction(c, fn)
}
// RunVolumeDetach detaches a volume from the droplet it's attached to.
func RunVolumeDetach(c *CmdConfig) error {
fn := func(das do.VolumeActionsService) (*do.Action, error) {
if len(c.Args) != 1 {
return nil, doctl.NewMissingArgsErr(c.NS)
}
volumeID := c.Args[0]
a, err := das.Detach(volumeID)
return a, err
}
return performVolumeAction(c, fn)
}