-
Notifications
You must be signed in to change notification settings - Fork 57
/
get.go
99 lines (84 loc) · 2.56 KB
/
get.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
package volumecommands
import (
"github.com/rackspace/rack/commandoptions"
"github.com/rackspace/rack/handler"
"github.com/rackspace/rack/internal/github.com/codegangsta/cli"
osVolumes "github.com/rackspace/rack/internal/github.com/rackspace/gophercloud/openstack/blockstorage/v1/volumes"
"github.com/rackspace/rack/util"
)
var get = cli.Command{
Name: "get",
Usage: util.Usage(commandPrefix, "get", "[--id <volumeID> | --name <volumeName> | --stdin id]"),
Description: "Gets a volume",
Action: actionGet,
Flags: commandoptions.CommandFlags(flagsGet, keysGet),
BashComplete: func(c *cli.Context) {
commandoptions.CompleteFlags(commandoptions.CommandFlags(flagsGet, keysGet))
},
}
func flagsGet() []cli.Flag {
return []cli.Flag{
cli.StringFlag{
Name: "id",
Usage: "[optional; required if `stdin` or `name` isn't provided] The ID of the volume.",
},
cli.StringFlag{
Name: "name",
Usage: "[optional; required if `stdin` or `id` isn't provided] The name of the volume.",
},
cli.StringFlag{
Name: "stdin",
Usage: "[optional; required if `id` or `name` isn't provided] The field being piped into STDIN. Valid values are: id",
},
}
}
var keysGet = []string{"ID", "Name", "Description", "Size", "VolumeType", "SnapshotID", "Attachments", "CreatedAt"}
type paramsGet struct {
volumeID string
}
type commandGet handler.Command
func actionGet(c *cli.Context) {
command := &commandGet{
Ctx: &handler.Context{
CLIContext: c,
},
}
handler.Handle(command)
}
func (command *commandGet) Context() *handler.Context {
return command.Ctx
}
func (command *commandGet) Keys() []string {
return keysGet
}
func (command *commandGet) ServiceClientType() string {
return serviceClientType
}
func (command *commandGet) HandleFlags(resource *handler.Resource) error {
resource.Params = ¶msGet{}
return nil
}
func (command *commandGet) HandlePipe(resource *handler.Resource, item string) error {
resource.Params.(*paramsGet).volumeID = item
return nil
}
func (command *commandGet) HandleSingle(resource *handler.Resource) error {
volumeID, err := command.Ctx.IDOrName(osVolumes.IDFromName)
if err != nil {
return err
}
resource.Params.(*paramsGet).volumeID = volumeID
return nil
}
func (command *commandGet) Execute(resource *handler.Resource) {
volumeID := resource.Params.(*paramsGet).volumeID
volume, err := osVolumes.Get(command.Ctx.ServiceClient, volumeID).Extract()
if err != nil {
resource.Err = err
return
}
resource.Result = volumeSingle(volume)
}
func (command *commandGet) StdinField() string {
return "id"
}