-
Notifications
You must be signed in to change notification settings - Fork 218
/
volumes.go
181 lines (129 loc) · 4.08 KB
/
volumes.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
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
package cmd
import (
"fmt"
"os"
"strconv"
"time"
"github.com/dustin/go-humanize"
"github.com/spf13/cobra"
"github.com/superfly/flyctl/api"
"github.com/superfly/flyctl/cmdctx"
"github.com/superfly/flyctl/helpers"
"github.com/superfly/flyctl/docstrings"
)
func newVolumesCommand() *Command {
volumesStrings := docstrings.Get("volumes")
volumesCmd := BuildCommandKS(nil, nil, volumesStrings, os.Stdout, requireAppName, requireSession)
volumesCmd.Aliases = []string{"vol"}
listStrings := docstrings.Get("volumes.list")
BuildCommandKS(volumesCmd, runListVolumes, listStrings, os.Stdout, requireAppName, requireSession)
createStrings := docstrings.Get("volumes.create")
createCmd := BuildCommandKS(volumesCmd, runCreateVolume, createStrings, os.Stdout, requireAppName, requireSession)
createCmd.Args = cobra.ExactArgs(1)
createCmd.AddStringFlag(StringFlagOpts{
Name: "region",
Description: "Set region for new volume",
})
createCmd.AddIntFlag(IntFlagOpts{
Name: "size",
Description: "Size of volume in gigabytes, default 10GB",
Default: 10,
})
createCmd.AddBoolFlag(BoolFlagOpts{
Name: "encrypted",
Description: "Encrypt the volume (default: true)",
Default: true,
})
deleteStrings := docstrings.Get("volumes.delete")
deleteCmd := BuildCommandKS(volumesCmd, runDestroyVolume, deleteStrings, os.Stdout, requireSession)
deleteCmd.Args = cobra.ExactArgs(1)
showStrings := docstrings.Get("volumes.show")
showCmd := BuildCommandKS(volumesCmd, runShowVolume, showStrings, os.Stdout, requireAppName, requireSession)
showCmd.Args = cobra.ExactArgs(1)
return volumesCmd
}
func runListVolumes(ctx *cmdctx.CmdContext) error {
volumes, err := ctx.Client.API().GetVolumes(ctx.AppName)
if err != nil {
return err
}
if len(volumes) == 0 {
fmt.Printf("No Volumes Defined for %s\n", ctx.AppName)
return nil
}
if ctx.OutputJSON() {
ctx.WriteJSON(volumes)
return nil
}
table := helpers.MakeSimpleTable(ctx.Out, []string{"ID", "Name", "Size", "Region", "Created At"})
for _, v := range volumes {
table.Append([]string{v.ID, v.Name, strconv.Itoa(v.SizeGb) + "GB", v.Region, humanize.Time(v.CreatedAt)})
}
table.Render()
return nil
}
func runCreateVolume(ctx *cmdctx.CmdContext) error {
volName := ctx.Args[0]
region, err := ctx.Config.GetString("region")
if err != nil {
return err
}
app, err := ctx.Client.API().GetApp(ctx.AppName)
if err != nil {
return err
}
appid := app.ID
if region == "" {
return fmt.Errorf("--region <region> flag required")
}
sizeGb := ctx.Config.GetInt("size")
volume, err := ctx.Client.API().CreateVolume(appid, volName, region, sizeGb, ctx.Config.GetBool("encrypted"))
if err != nil {
return err
}
fmt.Printf("%10s: %s\n", "ID", volume.ID)
fmt.Printf("%10s: %s\n", "Name", volume.Name)
fmt.Printf("%10s: %s\n", "Region", volume.Region)
fmt.Printf("%10s: %d\n", "Size GB", volume.SizeGb)
fmt.Printf("%10s: %t\n", "Encrypted", volume.Encrypted)
fmt.Printf("%10s: %s\n", "Created at", volume.CreatedAt.Format(time.RFC822))
return nil
}
func runDestroyVolume(ctx *cmdctx.CmdContext) error {
volID := ctx.Args[0]
data, err := ctx.Client.API().DeleteVolume(volID)
if err != nil {
return err
}
fmt.Printf("Destroyed volume %s from %s\n", volID, data.Name)
return nil
}
func runShowVolume(ctx *cmdctx.CmdContext) error {
volID := ctx.Args[0]
// TODO: Remove Client side fix for Volume API
var volume *api.Volume
volumes, err := ctx.Client.API().GetVolumes(ctx.AppName)
for _, v := range volumes {
if v.ID == volID {
volume = &v
break
}
}
if volume == nil {
return fmt.Errorf("volume %s not found in app %s", volID, ctx.AppName)
}
//volume, err := ctx.Client.API().GetVolume(ctx.AppName,volID)
if err != nil {
return err
}
if ctx.OutputJSON() {
ctx.WriteJSON(volume)
return nil
}
fmt.Printf("%10s: %s\n", "ID", volume.ID)
fmt.Printf("%10s: %s\n", "Name", volume.Name)
fmt.Printf("%10s: %s\n", "Region", volume.Region)
fmt.Printf("%10s: %d\n", "Size GB", volume.SizeGb)
fmt.Printf("%10s: %s\n", "Created at", volume.CreatedAt.Format(time.RFC822))
return nil
}