-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcli.go
118 lines (108 loc) · 2.8 KB
/
cli.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
package main
import (
"fmt"
"os"
"strings"
"github.com/sirupsen/logrus"
"github.com/urfave/cli/v2"
)
type Options struct {
ID string
Password string
Origin string
Workers uint64
CarefulMode bool
SpamInterval uint64
CourseIDs []string
}
func RunCLI() *Options {
shouldExit := true
options := &Options{}
app := &cli.App{
Name: "goer",
Usage: "A simple tool to help students enroll in their courses on the Edusoft website",
Version: "2.0.2",
Flags: []cli.Flag{
&cli.StringFlag{
Name: "id",
Aliases: []string{"i"},
Usage: "Login to account with the provided `ID`",
Required: true,
Destination: &options.ID,
},
&cli.StringFlag{
Name: "password",
Aliases: []string{"p"},
Usage: "Account's password of the provied ID",
Required: true,
Destination: &options.Password,
},
&cli.StringFlag{
Name: "origin",
Aliases: []string{"o"},
Usage: "Origin",
Value: "https://edusoftweb.hcmiu.edu.vn",
Destination: &options.Origin,
},
&cli.BoolFlag{
Name: "careful",
Aliases: []string{"c"},
Usage: "Save after each single successful registration",
Value: true,
Destination: &options.CarefulMode,
},
&cli.Uint64Flag{
Name: "spam",
Aliases: []string{"s"},
Usage: "Repeat the registrations every `TIME` seconds",
Destination: &options.SpamInterval,
Action: func(ctx *cli.Context, u uint64) error {
if u < 1 {
logrus.Fatalf("Flag `spam` value %v must be equal to or greater than 1", u)
}
return nil
},
},
&cli.Uint64Flag{
Name: "workers",
Aliases: []string{"w"},
Usage: "Set the number of workers which register for courses",
Value: 1,
Destination: &options.Workers,
Action: func(ctx *cli.Context, u uint64) error {
if u < 1 {
logrus.Fatalf("Flag `workers` value %v must be equal to or greater than 1", u)
}
return nil
},
},
&cli.StringSliceFlag{
Name: "course-id",
Aliases: []string{"I"},
Required: true,
Usage: "ID of registered course",
},
},
Action: func(ctx *cli.Context) error {
// Fix: https://github.com/TP-O/goer/issues/1
courseIDs := ctx.StringSlice("course-id")
for i := 0; i < len(courseIDs); i++ {
if len(strings.Split(courseIDs[i], "|")) < 10 && i < len(courseIDs[i])-1 {
options.CourseIDs = append(options.CourseIDs, fmt.Sprintf("%s, %s", courseIDs[i], courseIDs[i+1]))
i++
} else {
options.CourseIDs = append(options.CourseIDs, courseIDs[i])
}
}
shouldExit = false
return nil
},
}
if err := app.Run(os.Args); err != nil {
logrus.Fatal(err)
}
if shouldExit {
os.Exit(0)
}
return options
}