-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathjcliCommand.go
More file actions
359 lines (298 loc) · 10.8 KB
/
jcliCommand.go
File metadata and controls
359 lines (298 loc) · 10.8 KB
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
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
package main
import (
"encoding/json"
"fmt"
"strings"
"github.com/jedib0t/go-pretty/table"
)
func login() error {
fmt.Println("Requesting new tokens...")
_, err := getDevToken()
if err != nil {
return fmt.Errorf("error: failed to login: %v", err)
}
return nil
}
func apiGet(p string, authToken string) error {
var apiUrl = "https://api.jamlaunch.com/" + p
data, success := fetch(apiUrl, authToken)
if success == nil {
jsonBytes, err := json.MarshalIndent(data, "", " ")
if err != nil {
//log.Printf("\033[91mError: failed to format response as JSON: %v\033[0m\n", err)
return fmt.Errorf("error: failed to format response as json: %v", err)
}
jsonString := string(jsonBytes)
fmt.Println(jsonString)
} else {
//fmt.Printf("\033[91m%s\033[0m\n", success)
return fmt.Errorf("error: %s", success)
}
return nil
}
func projects(authToken string) error {
var apiUrl = "https://api.jamlaunch.com/projects"
data, success := fetch(apiUrl, authToken)
if success == nil {
if projects, ok := data["projects"].([]interface{}); ok {
if len(projects) == 0 {
fmt.Println("You currently do not have any projects!")
} else {
var (
colProjectIndex = "Id"
colProjectName = "Project Name"
projectHeader = table.Row{colProjectIndex, colProjectName}
)
t := table.NewWriter()
tTemp := table.Table{}
tTemp.Render()
t.AppendHeader(projectHeader)
t.SetTitle("Current Projects")
t.SetStyle(table.StyleColoredDark)
for _, project := range projects {
if projMap, ok := project.(map[string]interface{}); ok {
t.AppendRow(table.Row{projMap["id"], projMap["project_name"]})
}
}
fmt.Println(t.Render())
}
} else {
return fmt.Errorf("error: projects is not an array, please visit https://app.jamlaunch.com/projects and try again")
}
} else {
return fmt.Errorf("%s", success)
}
return nil
}
func projectsName(authToken string, name string) error {
var apiUrlName = "https://api.jamlaunch.com/projects"
nameData, successName := fetch(apiUrlName, authToken)
if successName == nil && nameData != nil {
var projectId string
if projects, ok := nameData["projects"].([]interface{}); ok {
for _, p := range projects {
project := p.(map[string]interface{})
if project["project_name"].(string) == name {
projectId = project["id"].(string)
break
}
}
}
var apiUrlId = "https://api.jamlaunch.com/projects/" + projectId
data, successId := fetch(apiUrlId, authToken)
if successId == nil {
if data != nil && data["project_name"] != nil {
fmt.Printf("\033[93mProject Name:\033[0m %s\n", data["project_name"].(string))
fmt.Printf("\033[93mCreated At:\033[0m %s\n", data["created_at"].(string)[:10])
fmt.Printf("\033[93mProject Id:\033[0m %s\n", data["id"].(string))
fmt.Printf("\033[93mActive:\033[0m %t\n", data["active"].(bool))
fmt.Println("")
if members, ok := data["members"].([]interface{}); ok && len(members) > 0 {
var (
colUsername = "Username"
colLevel = "Level"
membersHeader = table.Row{colUsername, colLevel}
)
t := table.NewWriter()
tTemp := table.Table{}
tTemp.Render()
t.AppendHeader(membersHeader)
t.SetTitle("Current Members")
t.SetStyle(table.StyleColoredDark)
for _, member := range members {
if memMap, ok := member.(map[string]interface{}); ok {
t.AppendRow(table.Row{memMap["username"], memMap["level"]})
}
}
fmt.Println(t.Render())
}
if releases, ok := data["releases"].([]interface{}); ok && len(releases) > 0 {
fmt.Println("")
var (
colId = "id"
colCreatedAt = "Created At"
colDefaultRelease = "Default Release"
colPublic = "Public"
colNetworkMode = "Network Mode"
colServerBuild = "Server Build"
colAllowGuests = "Allow Guests"
releasesHeader = table.Row{colId, colCreatedAt, colDefaultRelease, colPublic, colNetworkMode, colServerBuild, colAllowGuests}
)
t := table.NewWriter()
tTemp := table.Table{}
tTemp.Render()
t.AppendHeader(releasesHeader)
t.SetTitle("Current Releases")
t.SetStyle(table.StyleColoredDark)
for _, release := range releases {
if relMap, ok := release.(map[string]interface{}); ok {
t.AppendRow(table.Row{
relMap["id"],
relMap["created_at"],
relMap["is_default"],
relMap["public"],
relMap["network_mode"],
relMap["server_build"],
relMap["allow_guests"],
})
}
}
fmt.Println(t.Render())
}
} else {
return fmt.Errorf("error: project not found")
}
} else {
return fmt.Errorf("error: unable to retrieve project data: %s", successId)
}
} else {
return fmt.Errorf("error: unable to retrieve projects: %s", successName)
}
return nil
}
func projectSessions(authToken string, name string) error {
var apiUrlName = "https://api.jamlaunch.com/projects"
nameData, successName := fetch(apiUrlName, authToken)
if successName == nil && nameData != nil {
var projectId string
if projects, ok := nameData["projects"].([]interface{}); ok {
for _, p := range projects {
project := p.(map[string]interface{})
if project["project_name"].(string) == name {
projectId = project["id"].(string)
break
}
}
}
var apiUrlSessions = "https://api.jamlaunch.com/projects/" + projectId + "/sessions"
data, successId := fetch(apiUrlSessions, authToken)
if successId == nil {
if data != nil {
if sessions, ok := data["sessions"].([]interface{}); ok && len(sessions) > 0 {
var (
colSessionId = "Id"
colAddress = "Address"
colSessionCreatedAt = "Created At"
colState = "State"
sessionsHeader = table.Row{colSessionId, colAddress, colSessionCreatedAt, colState}
)
t := table.NewWriter()
tTemp := table.Table{}
tTemp.Render()
t.AppendHeader(sessionsHeader)
t.SetTitle("Current Sessions")
t.SetStyle(table.StyleColoredDark)
for _, session := range sessions {
if memMap, ok := session.(map[string]interface{}); ok {
t.AppendRow(table.Row{memMap["id"], memMap["address"], memMap["createdAt"], memMap["state"]})
}
}
fmt.Println(t.Render())
} else {
fmt.Printf("This project currently has no sessions!\n")
}
} else {
fmt.Printf("This project currently has no sessions!\n")
}
} else {
return fmt.Errorf("error: unable to retrieve session data: %s", successId)
}
} else {
return fmt.Errorf("error: unable to retrieve projects: %s", successName)
}
return nil
}
func projectSessionId(authToken string, name string, sessionId string) error {
var apiUrlName = "https://api.jamlaunch.com/projects"
nameData, successName := fetch(apiUrlName, authToken)
if successName == nil && nameData != nil {
var projectId string
if projects, ok := nameData["projects"].([]interface{}); ok {
for _, p := range projects {
project := p.(map[string]interface{})
if project["project_name"].(string) == name {
projectId = project["id"].(string)
break
}
}
}
var apiUrlSessionsWithId = "https://api.jamlaunch.com/projects/" + projectId + "/sessions/" + sessionId
data, successId := fetch(apiUrlSessionsWithId, authToken)
if successId == nil {
if id, ok := data["id"]; ok {
fmt.Printf("\033[93mSession Id:\033[0m %s\n", id.(string))
fmt.Printf("\033[93mSession Address:\033[0m %s\n", data["address"].(string))
fmt.Printf("\033[93mSession Join Code:\033[0m %s\n", data["joinCode"].(string))
fmt.Printf("\033[93mSession Region:\033[0m %s\n", data["region"].(string))
fmt.Printf("\033[93mSession State:\033[0m %s\n", data["state"].(string))
fmt.Println("")
if players, ok := data["players"].([]interface{}); ok && len(players) > 0 {
var (
colPlayerUsername = "Username"
colHost = "Host"
playerHeader = table.Row{colPlayerUsername, colHost}
)
t := table.NewWriter()
tTemp := table.Table{}
tTemp.Render()
t.AppendHeader(playerHeader)
t.SetTitle("Current Players")
t.SetStyle(table.StyleColoredDark)
for _, player := range players {
if memMap, ok := player.(map[string]interface{}); ok {
t.AppendRow(table.Row{memMap["username"], memMap["host"]})
}
}
fmt.Println(t.Render())
} else {
fmt.Printf("This session has no players!\n")
}
} else {
return fmt.Errorf("error: unable to find session or session does not exist")
}
} else {
return fmt.Errorf("error: unable to retrieve session data: %s", successId)
}
} else {
return fmt.Errorf("error: unable to retrieve projects: %s", successName)
}
return nil
}
func help(input string) {
parts := strings.Fields(input)
if len(parts) == 1 && strings.ToLower(parts[0]) == "help" {
fmt.Println("For more information on a specific command, type HELP command-name")
fmt.Println("HELP Provides Help information for Jam Launch CLI commands.")
fmt.Println("LOGIN Prompts the user to log in again.")
fmt.Println("PROJECTS Displays a list of the users current projects.")
} else if len(parts) == 2 && strings.ToLower(parts[0]) == "help" && strings.ToLower(parts[1]) == "projects" {
fmt.Println("PROJECTS command details:")
fmt.Println("Displays a list of the user's current projects.")
fmt.Println("")
fmt.Println("PROJECTS (No Parameters)")
fmt.Println("PROJECTS (Project Name)")
fmt.Println("PROJECTS (Project Name) SESSIONS")
fmt.Println("PROJECTS (Project Name) SESSIONS (Session ID)")
fmt.Println("")
fmt.Println("This command will display the id and name of each project in a table format.")
fmt.Println("Running projects with parameters will display more specific details about a specific project.")
fmt.Println("Running projects with parameters and the \"sessions\" keyword will display session information about the project")
} else if len(parts) == 2 && strings.ToLower(parts[0]) == "help" && strings.ToLower(parts[1]) == "help" {
fmt.Println("HELP command details:")
fmt.Println("Provides Help information for Jam Launch CLI commands.")
fmt.Println("")
fmt.Println("HELP (No Parameters)")
fmt.Println("HELP [Command Name]")
fmt.Println("")
fmt.Println("Running help with parameters will display detailed help information for the command specified by the parameter.")
} else if len(parts) == 2 && strings.ToLower(parts[0]) == "help" && strings.ToLower(parts[1]) == "login" {
fmt.Println("LOGIN command details:")
fmt.Println("Prompts the user to log in again.")
fmt.Println("")
fmt.Println("LOGIN (No Parameters)")
fmt.Println("")
fmt.Println("Running this command will prompt the user to generate a new authentication token and replace the old one regardles if it is valid or not.")
} else {
fmt.Printf("\033[31mCommand formatted incorrectly. Use 'HELP' or 'HELP command-name'.\033[0m\n")
}
}