-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
108 lines (93 loc) · 2.77 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
package main
import (
"flag"
"fmt"
"os"
"strings"
"github.com/alexcoder04/friendly/v2"
"github.com/alexcoder04/iserv2go/iserv"
"github.com/alexcoder04/iserv2go/iserv/types"
"github.com/joho/godotenv"
)
var (
VERSION string = "unknown"
COMMIT_SHA string = "unknown"
Info *bool = flag.Bool("info", false, "show program info")
EnableEmail *bool = flag.Bool("enable-email", false, "enable email module")
EnableFiles *bool = flag.Bool("enable-files", false, "enable files module")
EnableWeb *bool = flag.Bool("enable-web", false, "enable web module")
SaveSessions *bool = flag.Bool("save-sessions", false, "save login credentials on disk for subsequent logins")
Interactive *bool = flag.Bool("interactive", false, "interactive session")
Args []string
Client iserv.Client
)
var CommandsMap map[string]func([]string) = map[string]func([]string){
"email.list_mailboxes": CommandEmailListMailboxes,
"email.read_mailbox": CommandEmailReadMailbox,
"email.send_mail": CommandEmailSendMail,
"web.get_badges": CommandWebGetBadges,
"web.get_notifications": CommandWebGetNotifications,
"web.get_upcoming_events": CommandWebGetUpcomingEvents,
"web.get_current_exercises": CommandWebGetCurrentExercises,
"web.get_past_exercises": CommandWebGetPastExercises,
"files.ls": CommandFilesLs,
"files.cat": CommandFilesCat,
"files.download": CommandFilesDownload,
"files.upload": CommandFilesUpload,
}
func init() {
godotenv.Load()
flag.Parse()
Args = flag.Args()
}
func RunCommand(cmd string, args []string) error {
if _, ok := CommandsMap[cmd]; ok {
CommandsMap[cmd](args)
return nil
}
return fmt.Errorf("command not found: '%s'", cmd)
}
func main() {
if *Info {
fmt.Printf("iserv2go %s (commit %s)\n", VERSION, COMMIT_SHA)
return
}
Client = iserv.Client{}
err := Client.Login(&types.ClientConfig{
IServHost: os.Getenv("ISERV_HOST"),
Password: os.Getenv("ISERV_PASSWORD"),
Username: os.Getenv("ISERV_USERNAME"),
EnableModules: map[string]bool{
"email": *EnableEmail,
"files": *EnableFiles,
"web": *EnableWeb,
},
SaveSessions: *SaveSessions,
})
if err != nil {
friendly.Die("Cannot login: %s", err.Error())
}
defer Client.Logout()
if *Interactive {
for {
inp, err := friendly.Input("iserv> ")
if err != nil {
friendly.Die("Failed to read command: %s", err.Error())
}
cmdline := strings.Split(strings.TrimSpace(inp), " ")
if cmdline[0] == "exit" {
return
}
err = RunCommand(cmdline[0], cmdline[1:])
if err != nil {
friendly.Warn("Command '%s' not found", cmdline[0])
}
}
}
if Args[0] != "" {
err := RunCommand(Args[0], Args[1:])
if err != nil {
friendly.Die("Command '%s' not found", Args[0])
}
}
}