This repository has been archived by the owner on Mar 29, 2018. It is now read-only.
/
image_actions.go
88 lines (69 loc) · 1.98 KB
/
image_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
88
package commands
import (
"io"
"strconv"
"github.com/bryanl/doit"
"github.com/Sirupsen/logrus"
"github.com/digitalocean/godo"
"github.com/spf13/cobra"
)
// ImageAction creates the image action commmand.
func ImageAction() *cobra.Command {
cmd := &cobra.Command{
Use: "image-action",
Short: "image-action commands",
Long: "image-action commands",
}
cmdImageActionsGet := cmdBuilder(RunImageActionsGet,
"get <image-id>", "get image action", writer)
cmd.AddCommand(cmdImageActionsGet)
addIntFlag(cmdImageActionsGet, doit.ArgActionID, 0, "action id", requiredOpt())
cmdImageActionsTransfer := cmdBuilder(RunImageActionsTransfer,
"transfer <image-id>", "transfer imagr", writer)
cmd.AddCommand(cmdImageActionsTransfer)
addStringFlag(cmdImageActionsTransfer, doit.ArgRegionSlug, "", "region", requiredOpt())
return cmd
}
// RunImageActionsGet retrieves an action for an image.
func RunImageActionsGet(ns string, config doit.Config, out io.Writer, args []string) error {
client := config.GetGodoClient()
if len(args) != 1 {
return doit.NewMissingArgsErr(ns)
}
imageID, err := strconv.Atoi(args[0])
if err != nil {
return err
}
actionID, err := config.GetInt(ns, doit.ArgActionID)
if err != nil {
return err
}
action, _, err := client.ImageActions.Get(imageID, actionID)
if err != nil {
return err
}
return doit.DisplayOutput(action, out)
}
// RunImageActionsTransfer an image.
func RunImageActionsTransfer(ns string, config doit.Config, out io.Writer, args []string) error {
client := config.GetGodoClient()
if len(args) != 1 {
return doit.NewMissingArgsErr(ns)
}
id, err := strconv.Atoi(args[0])
if err != nil {
return err
}
region, err := config.GetString(ns, doit.ArgRegionSlug)
if err != nil {
return err
}
req := &godo.ActionRequest{
"region": region,
}
action, _, err := client.ImageActions.Transfer(id, req)
if err != nil {
logrus.WithField("err", err).Fatal("could not transfer image")
}
return doit.DisplayOutput(action, out)
}