-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
103 lines (95 loc) · 2.49 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
package main
import (
"aivle-cli/models"
"aivle-cli/operations"
"encoding/json"
"fmt"
"github.com/AlecAivazis/survey/v2"
"github.com/joho/godotenv"
"log"
"net/http"
"net/url"
"os"
)
const (
OperationExit = "exit"
OperationPrintToken = "print token"
OperationDownloadSubmissions = "download submissions"
OperationDownloadResults = "download results"
OperationUploadWhitelist = "upload whitelist"
)
// the questions to ask
var loginQuestions = []*survey.Question{
{
Name: "username",
Prompt: &survey.Input{Message: "What is your aiVLE username?"},
Validate: survey.Required,
},
{
Name: "password",
Prompt: &survey.Password{Message: "Password:"},
Validate: survey.Required,
},
}
func main() {
err := godotenv.Load()
if err != nil {
log.Fatal("Error loading .env file")
}
apiRoot := os.Getenv("API_ROOT")
// the loginAnswers will be written to this struct
loginAnswers := struct {
Username string `survey:"username"`
Password string `survey:"password"`
}{}
// perform the questions
err = survey.Ask(loginQuestions, &loginAnswers)
if err != nil {
fmt.Println(err.Error())
return
}
// try getting the token
resp, err := http.PostForm(apiRoot+"/dj-rest-auth/login/", url.Values{
"username": {loginAnswers.Username},
"password": {loginAnswers.Password},
})
if err != nil {
fmt.Println(err.Error())
return
}
if resp.StatusCode != 200 {
fmt.Printf("Unable to login with status code %d\n", resp.StatusCode)
return
}
tokenResponse := models.TokenResponse{}
defer resp.Body.Close()
err = json.NewDecoder(resp.Body).Decode(&tokenResponse)
if err != nil {
fmt.Println(err.Error())
return
}
for {
// handle operations
operation := ""
err = survey.AskOne(&survey.Select{
Message: "Choose an operation:",
Options: []string{OperationExit, OperationDownloadSubmissions, OperationDownloadResults, OperationUploadWhitelist, OperationPrintToken},
Default: "print token",
}, &operation)
if err != nil {
fmt.Println(err.Error())
return
}
if operation == OperationExit {
break
} else if operation == OperationPrintToken {
fmt.Println(tokenResponse.Token)
} else if operation == OperationDownloadSubmissions {
operations.DownloadSubmissions(apiRoot, tokenResponse.Token)
} else if operation == OperationDownloadResults {
operations.DownloadResults(apiRoot, tokenResponse.Token)
} else if operation == OperationUploadWhitelist {
operations.UploadWhitelist(apiRoot, tokenResponse.Token)
}
}
}