-
Notifications
You must be signed in to change notification settings - Fork 1
/
main.go
247 lines (222 loc) · 7.07 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
package main
import (
"fmt"
"github.com/speedata/optionparser"
"github.com/charmbracelet/huh"
"github.com/charmbracelet/huh/spinner"
"github.com/charmbracelet/lipgloss"
"github.com/p5quared/decanter/Autolab"
)
// By default, we're running in production mode. Yee haw!
var PROD = "TRUE"
const ( // I was told it was okay to put these here... :p
host = "https://autolab.cse.buffalo.edu"
decanterClientID = "D4MZfAzZ27U121M2vwnHMEN6Cz-RMrQIKMlVjpEuKh8"
decanterClientSecret = "fGVpQqJ0SdLGp7hfyN9wCn6VvuzU9djJfRklRPRQGGk"
)
// Big TODO: Caching user datda (like courses and due dates)
func main() {
if PROD != "TRUE" {
exclaim := lipgloss.NewStyle().Foreground(colorPrimary).Bold(true).Render
fmt.Println(exclaim("RUNNING IN DEBUG MODE"))
}
op := optionparser.NewOptionParser()
var file string
op.On("-f NAME", "--file NAME", "Specify file. -f main.go", &file)
var assessment string
op.On("-a NAME", "--assessment NAME", "Specify an assessment. -a pa1", &assessment)
var course string
op.On("-c NAME", "--course NAME", "Specify a course. -c cse468-s24", &course)
var wait bool
op.On("-w", "--wait", "Waits for additional info (if applicable). ex: 'submit -w' will wait for and display results. (NOT CURRENTLY IMPLEMENTED)", &wait)
var all bool
op.On("--all", "Display all (extra) data.", &all)
var interactive bool
op.On("-i", "--interactive", "Run in interactive mode.", &interactive)
op.Command("setup", "Setup (authorize) a new device (you should only need to do this once).")
op.Command("submit", "Submit to an assessment.Available flags: --course, --assessment, --file, --wait")
op.Command("list", "List data. Args: courses|assessments|submissions|me")
err := op.Parse()
if err != nil {
fmt.Println(errorMsg(err.Error()))
return
}
ex := op.Extra
if len(ex) == 0 {
fmt.Println(errorMsg("No command specified."))
return
}
decanter := NewDecanter()
if ex[0] == "setup" {
if decanter.tokenExists() {
if !areYouSure("It looks like you already setup Decanter.", "Continue setup", "Abort") {
return
}
}
decanter.interactiveSetup()
return
}
// check that we have a token
if !decanter.tokenExists() {
fmt.Println(errorMsg("No token found. Please run 'decanter setup' to authorize this device."))
return
}
cmd := ex[0]
switch cmd {
case "setup":
if decanter.tokenExists() {
if !areYouSure("It looks like you already setup Decanter.", "Continue setup", "Abort") {
return
}
}
decanter.interactiveSetup()
// TODO: Should be a multipart form;
// 1. Select course
// 2. Select assessment
// 3. Select file
// Where each steps are pre-populated with
// the users possible options, so they don't
// have to remember the course/assessment names.
// TODO: Save last used course/assessment to a cache
// to avoid user having to re-enter it.
// Prompt like "Resubmit [file] to [assessment]?"
case "submit":
if course == "" || assessment == "" || file == "" {
// TODO: Form theme.
huh.NewForm(
huh.NewGroup(
huh.NewInput().
Title("Course").
Description("Which course to submit for?\n'decanter list courses' to see options").
Value(&course),
huh.NewInput().
Title("Assessment").
Description("Which assessment to submit for?\n'decanter list assessments' to see options").
Value(&assessment),
huh.NewFilePicker().
Title("Select a file:").
Description("This file will be uploaded and submit to Autolab.").
Value(&file),
),
).
WithTheme(decanterFormStyle()).
WithShowHelp(true).Run()
}
if file == "" || assessment == "" || course == "" {
return // User cancelled
}
tStr := fmt.Sprintf("Submitting %s to %s...", file, assessment)
var err error
spinner.New().
Title(tStr).
Style(spinStyle).
Action(func() {
_, err = decanter.SubmitFile(course, assessment, file)
}).Run()
if err != nil {
// Not sure why, but we need this, otherwise the text is getting pushed over.
fmt.Println()
fmt.Println(errorMsg("Decanter could not submit.") + "\n" + err.Error())
return
} else {
var emphasis = lipgloss.NewStyle().Bold(true).Foreground(colorPrimary).Render
fmt.Printf("%s %s to %s!\n", finished("Successfully submit"), emphasis(file), emphasis(assessment))
}
if wait {
var latest Autolab.SubmissionsResponse
spinner.New().
Style(spinStyle).
Title("Waiting for grading...").
Action(func() {
latest, err = decanter.PollLatestSubmission(course, assessment)
}).Run()
if err != nil {
errStr := fmt.Sprintf("Something went wrong while waiting for grading.\n%s", err.Error())
fmt.Println(errorMsg(errStr))
return
}
fmt.Println(finished("Submission graded"))
displaySubmission(latest)
}
case "list":
if len(ex) < 2 {
fmt.Println("Invalid Usage: Please specify a list group.\nex: decanter list courses|assessments|me")
return
}
switch ex[1] {
case "courses":
var courses []Autolab.CoursesResponse
spinner.New().
Title("Fetching course data...").
Style(spinStyle).
Action(func() {
courses, _ = decanter.GetUserCourses()
}).Run()
// Default: Only show current semester
if !all {
courses = filterCourses(courses, func(c Autolab.CoursesResponse) bool {
return c.Semester == "s24"
})
}
fmt.Println(finished("Fetched course data"))
displayCourseList(courses)
case "assessments", "ass":
var courses []Autolab.CoursesResponse
spinner.New().
Style(spinStyle).
Title("Fetching assessments...").
Action(func() {
courses, _ = decanter.GetUserCourses()
}).Run()
if !all {
courses = filter(courses, func(c Autolab.CoursesResponse) bool {
return c.Semester == "s24"
})
}
// Technically this is a lie.
fmt.Println(finished("Fetched assessments"))
displayAssessmentList(decanter, courses)
case "submissions", "subs":
if course == "" || assessment == "" {
fmt.Println(errorMsg("To view submissions, please pass a course and assessment."))
return
}
var submissions []Autolab.SubmissionsResponse
spinner.New().
Style(spinStyle).
Title("Fetching submissions...").
Action(func() {
submissions, err = decanter.GetSubmissions(course, assessment)
}).Run()
if err != nil {
errStr := fmt.Sprintf("Something went wrong while fetching submissions. \nCheck your arguments:\nCourse: %s\nAssessment: %s", course, assessment)
fmt.Println(errorMsg(errStr))
return
}
doneStr := fmt.Sprintf("Fetched submisions for %s", course)
fmt.Println(finished(doneStr))
// Grab latest
var submission Autolab.SubmissionsResponse
for _, sub := range submissions {
if sub.Version > submission.Version {
submission = sub
}
}
displaySubmission(submission)
case "me":
var user Autolab.UserResponse
spinner.New().
Style(spinStyle).
Title("Fetching user data...").
Action(func() {
user, _ = decanter.GetUserInfo()
}).Run()
fmt.Println(finished("Fetched user data"))
displayUserInfo(user)
default:
fmt.Println("Invalid list group.\nOptions: courses|assessments")
}
default:
fmt.Println("Command not recognized.")
}
}