/
widget.go
262 lines (206 loc) · 6.05 KB
/
widget.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
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
package digitalocean
import (
"context"
"errors"
"fmt"
"github.com/digitalocean/godo"
"github.com/rivo/tview"
"github.com/wtfutil/wtf/utils"
"github.com/wtfutil/wtf/view"
"golang.org/x/oauth2"
)
/* -------------------- Oauth2 Token -------------------- */
type tokenSource struct {
AccessToken string
}
// Token creates and returns an Oauth2 token
func (t *tokenSource) Token() (*oauth2.Token, error) {
token := &oauth2.Token{
AccessToken: t.AccessToken,
}
return token, nil
}
/* -------------------- Widget -------------------- */
// Widget is the container for droplet data
type Widget struct {
view.ScrollableWidget
app *tview.Application
client *godo.Client
droplets []*Droplet
pages *tview.Pages
settings *Settings
err error
}
// NewWidget creates a new instance of a widget
func NewWidget(tviewApp *tview.Application, redrawChan chan bool, pages *tview.Pages, settings *Settings) *Widget {
widget := Widget{
ScrollableWidget: view.NewScrollableWidget(tviewApp, redrawChan, pages, settings.Common),
app: tviewApp,
pages: pages,
settings: settings,
}
widget.initializeKeyboardControls()
widget.View.SetScrollable(true)
widget.SetRenderFunction(widget.display)
widget.createClient()
return &widget
}
/* -------------------- Exported Functions -------------------- */
// Fetch retrieves droplet data
func (widget *Widget) Fetch() error {
if widget.client == nil {
return errors.New("client could not be initialized")
}
var err error
widget.droplets, err = widget.dropletsFetch()
return err
}
// Next selects the next item in the list
func (widget *Widget) Next() {
widget.ScrollableWidget.Next()
}
// Prev selects the previous item in the list
func (widget *Widget) Prev() {
widget.ScrollableWidget.Prev()
}
// Refresh updates the data for this widget and displays it onscreen
func (widget *Widget) Refresh() {
err := widget.Fetch()
if err != nil {
widget.err = err
widget.SetItemCount(0)
} else {
widget.err = nil
widget.SetItemCount(len(widget.droplets))
}
widget.display()
}
// Unselect clears the selection of list items
func (widget *Widget) Unselect() {
widget.ScrollableWidget.Unselect()
widget.RenderFunction()
}
/* -------------------- Unexported Functions -------------------- */
// createClient create a persisten DigitalOcean client for use in the calls below
func (widget *Widget) createClient() {
tokenSource := &tokenSource{
AccessToken: widget.settings.apiKey,
}
oauthClient := oauth2.NewClient(context.Background(), tokenSource)
widget.client = godo.NewClient(oauthClient)
}
// currentDroplet returns the currently-selected droplet, if there is one
// Returns nil if no droplet is selected
func (widget *Widget) currentDroplet() *Droplet {
if len(widget.droplets) == 0 {
return nil
}
if len(widget.droplets) <= widget.Selected {
return nil
}
return widget.droplets[widget.Selected]
}
// dropletsFetch uses the DigitalOcean API to fetch information about all the available droplets
func (widget *Widget) dropletsFetch() ([]*Droplet, error) {
dropletList := []*Droplet{}
opts := &godo.ListOptions{}
for {
doDroplets, resp, err := widget.client.Droplets.List(context.Background(), opts)
if err != nil {
return dropletList, err
}
for _, doDroplet := range doDroplets {
droplet := NewDroplet(doDroplet)
dropletList = append(dropletList, droplet)
}
if resp.Links == nil || resp.Links.IsLastPage() {
break
}
page, err := resp.Links.CurrentPage()
if err != nil {
return dropletList, err
}
// Set the page we want for the next request
opts.Page = page + 1
}
return dropletList, nil
}
/* -------------------- Droplet Actions -------------------- */
// dropletDestroy destroys the selected droplet
func (widget *Widget) dropletDestroy() {
currDroplet := widget.currentDroplet()
if currDroplet == nil {
return
}
_, err := widget.client.Droplets.Delete(context.Background(), currDroplet.ID)
if err != nil {
return
}
widget.dropletRemoveSelected()
widget.Refresh()
}
// dropletEnabledPrivateNetworking enabled private networking on the selected droplet
func (widget *Widget) dropletEnabledPrivateNetworking() {
currDroplet := widget.currentDroplet()
if currDroplet == nil {
return
}
_, _, err := widget.client.DropletActions.EnablePrivateNetworking(context.Background(), currDroplet.ID)
if err != nil {
return
}
widget.Refresh()
}
// dropletRemoveSelected removes the currently-selected droplet from the internal list of droplets
func (widget *Widget) dropletRemoveSelected() {
currDroplet := widget.currentDroplet()
if currDroplet != nil {
widget.droplets[len(widget.droplets)-1], widget.droplets[widget.Selected] = widget.droplets[widget.Selected], widget.droplets[len(widget.droplets)-1]
widget.droplets = widget.droplets[:len(widget.droplets)-1]
}
}
// dropletRestart restarts the selected droplet
func (widget *Widget) dropletRestart() {
currDroplet := widget.currentDroplet()
if currDroplet == nil {
return
}
_, _, err := widget.client.DropletActions.Reboot(context.Background(), currDroplet.ID)
if err != nil {
return
}
widget.Refresh()
}
// dropletShutDown powers down the selected droplet
func (widget *Widget) dropletShutDown() {
currDroplet := widget.currentDroplet()
if currDroplet == nil {
return
}
_, _, err := widget.client.DropletActions.Shutdown(context.Background(), currDroplet.ID)
if err != nil {
return
}
widget.Refresh()
}
/* -------------------- Common Actions -------------------- */
// showInfo shows a modal window with information about the selected droplet
func (widget *Widget) showInfo() {
droplet := widget.currentDroplet()
if droplet == nil {
return
}
closeFunc := func() {
widget.pages.RemovePage("info")
widget.app.SetFocus(widget.View)
}
propTable := newDropletPropertiesTable(droplet).render()
propTable += utils.CenterText("Esc to close", 80)
modal := view.NewBillboardModal(propTable, closeFunc)
modal.SetTitle(fmt.Sprintf(" %s ", droplet.Name))
widget.pages.AddPage("info", modal, false, true)
widget.app.SetFocus(modal)
widget.app.QueueUpdateDraw(func() {
widget.app.Draw()
})
}