-
Notifications
You must be signed in to change notification settings - Fork 12
/
main.go
260 lines (218 loc) · 6.74 KB
/
main.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
package main
import (
"encoding/json"
"fmt"
"github.com/lawrencegripper/azbrowse/style"
"github.com/lawrencegripper/azbrowse/version"
"log"
"os"
"runtime"
"strings"
"time"
"github.com/atotto/clipboard"
"github.com/jroimartin/gocui"
"github.com/lawrencegripper/azbrowse/armclient"
"github.com/lawrencegripper/azbrowse/search"
open "github.com/skratchdot/open-golang/open"
)
func main() {
if len(os.Args) >= 2 {
arg := os.Args[1]
if strings.Contains(arg, "version") {
fmt.Println(version.BuildDataVersion)
fmt.Println(version.BuildDataGitCommit)
fmt.Println(version.BuildDataGoVersion)
fmt.Println(fmt.Sprintf("%s/%s", runtime.GOOS, runtime.GOARCH))
fmt.Println(version.BuildDataBuildDate)
os.Exit(0)
}
if strings.Contains(arg, "search") {
fmt.Print("Getting resources \n")
subRequest, _ := getSubscriptions()
search.CrawlResources(subRequest)
fmt.Print("Build suggester \n")
suggester, _ := search.NewSuggester()
fmt.Print("Get suggestions \n")
suggestions := suggester.Autocomplete(os.Args[2])
fmt.Printf("%v \n", suggestions)
os.Exit(0)
}
}
confirmAndSelfUpdate()
g, err := gocui.NewGui(gocui.OutputNormal)
if err != nil {
log.Panicln(err)
}
defer g.Close()
g.Highlight = true
g.SelFgColor = gocui.ColorBlue
// help := NewHelpWidget("help", 1, 1, helpText)
maxX, maxY := g.Size()
// Padding
maxX = maxX - 2
maxY = maxY - 2
if maxX < 72 {
panic("I can't run in a terminal less than 72 wide ... it's tooooo small!!!")
}
status := NewStatusbarWidget(1, maxY-2, maxX, g)
header := NewHeaderWidget(1, 1, 70, 9)
content := NewItemWidget(70+2, 1, maxX-70-1, maxY-4, "")
list := NewListWidget(1, 11, 70, maxY-14, []string{"Loading..."}, 0, content, status)
g.SetManager(status, content, list, header)
g.SetCurrentView("listWidget")
if err := g.SetKeybinding("listWidget", gocui.KeyArrowDown, gocui.ModNone, func(g *gocui.Gui, v *gocui.View) error {
list.ChangeSelection(list.CurrentSelection() + 1)
return nil
}); err != nil {
log.Panicln(err)
}
if err := g.SetKeybinding("listWidget", gocui.KeyArrowUp, gocui.ModNone, func(g *gocui.Gui, v *gocui.View) error {
list.ChangeSelection(list.CurrentSelection() - 1)
return nil
}); err != nil {
log.Panicln(err)
}
if err := g.SetKeybinding("listWidget", gocui.KeyEnter, gocui.ModNone, func(g *gocui.Gui, v *gocui.View) error {
list.ExpandCurrentSelection()
return nil
}); err != nil {
log.Panicln(err)
}
if err := g.SetKeybinding("listWidget", gocui.KeyBackspace2, gocui.ModNone, func(g *gocui.Gui, v *gocui.View) error {
list.GoBack()
return nil
}); err != nil {
log.Panicln(err)
}
if err := g.SetKeybinding("listWidget", gocui.KeyBackspace, gocui.ModNone, func(g *gocui.Gui, v *gocui.View) error {
list.GoBack()
return nil
}); err != nil {
log.Panicln(err)
}
if err := g.SetKeybinding("listWidget", gocui.KeyCtrlA, gocui.ModNone, func(g *gocui.Gui, v *gocui.View) error {
return LoadActionsView(list)
}); err != nil {
log.Panicln(err)
}
if err := g.SetKeybinding("listWidget", gocui.KeyCtrlO, gocui.ModNone, func(g *gocui.Gui, v *gocui.View) error {
item := list.CurrentItem()
protalURL := os.Getenv("AZURE_PORTAL_URL")
if protalURL == "" {
protalURL = "https://portal.azure.com"
}
open.Run(protalURL + "/#@" + armclient.GetTenantID() + "/resource/" + item.parentid + "/overview")
return nil
}); err != nil {
log.Panicln(err)
}
editModelEnabled := false
if err := g.SetKeybinding("", gocui.KeyCtrlE, gocui.ModNone, func(g *gocui.Gui, v *gocui.View) error {
editModelEnabled = !editModelEnabled
if editModelEnabled {
g.Cursor = true
g.SetCurrentView("itemWidget")
} else {
g.Cursor = false
g.SetCurrentView("listWidget")
}
return nil
}); err != nil {
log.Panicln(err)
}
isFullScreen := false
if err := g.SetKeybinding("", gocui.KeyCtrlF, gocui.ModNone, func(g *gocui.Gui, v *gocui.View) error {
isFullScreen = !isFullScreen
if isFullScreen {
g.Cursor = true
maxX, maxY := g.Size()
v, _ := g.SetView("fullscreenContent", 0, 0, maxX, maxY)
v.Editable = true
v.Frame = false
v.Wrap = true
fmt.Fprintf(v, content.GetContent())
g.SetCurrentView("fullscreenContent")
} else {
g.Cursor = false
g.DeleteView("fullscreenContent")
g.SetCurrentView("listWidget")
}
return nil
}); err != nil {
log.Panicln(err)
}
if err := g.SetKeybinding("", gocui.KeyCtrlS, gocui.ModNone, func(g *gocui.Gui, v *gocui.View) error {
clipboard.WriteAll(content.GetContent())
status.Status("Current resource's JSON copied to clipboard", false)
return nil
}); err != nil {
log.Panicln(err)
}
// HACK: To prevent accidental deletes this method requires del to be pressed twice on a resource
// before it will proceed
var deleteConfirmItemID string
var deleteConfirmCount int
if err := g.SetKeybinding("", gocui.KeyDelete, gocui.ModNone, func(g *gocui.Gui, v *gocui.View) error {
item := list.CurrentItem()
if deleteConfirmItemID != item.id {
deleteConfirmItemID = item.id
deleteConfirmCount = 0
}
status.Status("Delete item? Really? PRESS DEL TO CONFIRM: "+item.deleteURL, true)
deleteConfirmCount++
if deleteConfirmCount > 1 {
status.Status("Delete item? Really? PRESS DEL TO CONFIRM: "+item.deleteURL, true)
res, err := armclient.DoRequest("DELETE", item.deleteURL)
if err != nil {
panic(err)
}
content.SetContent(style.Title("Delete response for item:"+item.deleteURL+"\n ------------------------------- \n") + res)
status.Status("Delete request sent successfully: "+item.deleteURL, false)
deleteConfirmItemID = ""
}
return nil
}); err != nil {
log.Panicln(err)
}
if err := g.SetKeybinding("", gocui.KeyCtrlC, gocui.ModNone, quit); err != nil {
log.Panicln(err)
}
go func() {
time.Sleep(time.Second * 1)
status.Status("Fetching Subscriptions", true)
subRequest, data := getSubscriptions()
g.Update(func(gui *gocui.Gui) error {
g.SetCurrentView("listWidget")
status.Status("Getting provider data", true)
armclient.PopulateResourceAPILookup()
status.Status("Done getting provider data", false)
list.SetSubscriptions(subRequest)
if err != nil {
content.SetContent(err.Error())
return nil
}
content.SetContent(data)
return nil
})
status.Status("Fetching Subscriptions: Completed", false)
}()
if err := g.MainLoop(); err != nil && err != gocui.ErrQuit {
log.Panicln(err)
}
}
func getSubscriptions() (armclient.SubResponse, string) {
// Get Subscriptions
data, err := armclient.DoRequest("GET", "/subscriptions?api-version=2018-01-01")
if err != nil {
panic(err)
}
var subRequest armclient.SubResponse
err = json.Unmarshal([]byte(data), &subRequest)
if err != nil {
panic(err)
}
return subRequest, data
}
func quit(g *gocui.Gui, v *gocui.View) error {
return gocui.ErrQuit
}