forked from dianhsu-official/cf-tool
/
args.go
190 lines (180 loc) · 5.19 KB
/
args.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
package cmd
import (
"errors"
"os"
"path/filepath"
"strings"
"github.com/Arapak/sio-tool/codeforces_client"
"github.com/Arapak/sio-tool/config"
"github.com/Arapak/sio-tool/sio_client"
"github.com/Arapak/sio-tool/szkopul_client"
"github.com/docopt/docopt-go"
)
type ParsedArgs struct {
CodeforcesInfo codeforces_client.Info
SzkopulInfo szkopul_client.Info
SioInfo sio_client.Info
File string
Generator string
Solve string
Brute string
Source string
Name string
Path string
Link string
Shortname string
Contest string
Stage string
TimeLimit string `docopt:"--time_limit"`
MemoryLimit string `docopt:"--memory_limit"`
Specifier []string `docopt:"<specifier>"`
Alias string `docopt:"<alias>"`
Accepted bool `docopt:"ac"`
All bool `docopt:"all"`
Handle string `docopt:"<handle>"`
Version string `docopt:"{version}"`
Config bool `docopt:"config"`
Submit bool `docopt:"submit"`
List bool `docopt:"list"`
Parse bool `docopt:"parse"`
Gen bool `docopt:"gen"`
Test bool `docopt:"test"`
PackageTest bool `docopt:"package_test"`
AddPackage bool `docopt:"add_package"`
DownloadPackages bool `docopt:"download_packages"`
UploadPackage bool `docopt:"upload_package"`
Watch bool `docopt:"watch"`
Open bool `docopt:"open"`
Stand bool `docopt:"stand"`
Sid bool `docopt:"sid"`
Race bool `docopt:"race"`
Pull bool `docopt:"pull"`
Clone bool `docopt:"clone"`
Upgrade bool `docopt:"upgrade"`
StressTest bool `docopt:"stress-test"`
Database bool `docopt:"db"`
Add bool `docopt:"add"`
Find bool `docopt:"find"`
Goto bool `docopt:"goto"`
Codeforces bool
Szkopul bool
SioStaszic bool
SioMimuw bool
SioTalent bool
Oiejq bool
Verbose bool
}
var Args *ParsedArgs
func SubPath(parent, sub string) bool {
return strings.HasPrefix(parent, sub)
}
func determineClient() error {
path, err := os.Getwd()
if err != nil {
return err
}
cfg := config.Instance
codeforcesDir := SubPath(path, cfg.FolderName["codeforces-root"])
if codeforcesDir {
Args.Codeforces = true
return nil
}
sioStaszicDir := SubPath(path, cfg.FolderName["sio-staszic-root"])
if sioStaszicDir {
Args.SioStaszic = true
return nil
}
sioMimuwDir := SubPath(path, cfg.FolderName["sio-mimuw-root"])
if sioMimuwDir {
Args.SioMimuw = true
return nil
}
sioTalentDir := SubPath(path, cfg.FolderName["sio-talent-root"])
if sioTalentDir {
Args.SioTalent = true
return nil
}
szkopulDir := SubPath(path, cfg.FolderName["szkopul-root"])
if szkopulDir {
Args.Szkopul = true
return nil
}
for _, arg := range Args.Specifier {
if strings.Contains(arg, config.Instance.CodeforcesHost) {
Args.Codeforces = true
return nil
}
if strings.Contains(arg, config.Instance.SioStaszicHost) {
Args.SioStaszic = true
return nil
}
if strings.Contains(arg, config.Instance.SioMimuwHost) {
Args.SioMimuw = true
return nil
}
if strings.Contains(arg, config.Instance.SioTalentHost) {
Args.SioTalent = true
return nil
}
if strings.Contains(arg, config.Instance.SzkopulHost) {
Args.Szkopul = true
return nil
}
}
return nil
}
func parseArgs(opts docopt.Opts) error {
if file, ok := opts["--file"].(string); ok {
Args.File = file
} else if file, ok := opts["<file>"].(string); ok {
Args.File = file
}
err := determineClient()
if err != nil {
return err
}
cfg := config.Instance
if Args.Codeforces {
return parseArgsCodeforces()
}
if Args.SioStaszic {
if Args.Handle == "" {
Args.Handle = sio_client.StaszicInstance.Username
}
return parseArgsSio(cfg.FolderName["sio-staszic-root"])
}
if Args.SioMimuw {
if Args.Handle == "" {
Args.Handle = sio_client.MimuwInstance.Username
}
return parseArgsSio(cfg.FolderName["sio-mimuw-root"])
}
if Args.SioTalent {
if Args.Handle == "" {
Args.Handle = sio_client.TalentInstance.Username
}
return parseArgsSio(cfg.FolderName["sio-talent-root"])
}
if Args.Szkopul {
return parseArgsSzkopul()
}
return nil
}
const ErrorPackageCouldntBeDetermined = "package path couldn't be determined"
func ArgsPackagePath() (path string, err error) {
cfg := config.Instance
if Args.Codeforces {
Args.CodeforcesInfo.RootPath = filepath.Join(cfg.PackagesPath, "codeforces")
return Args.CodeforcesInfo.PackagePath()
} else if Args.Szkopul {
Args.SzkopulInfo.RootPath = filepath.Join(cfg.PackagesPath, "szkopul")
return Args.SzkopulInfo.PackagePath()
} else if Args.SioStaszic {
Args.SioInfo.RootPath = filepath.Join(cfg.PackagesPath, "sio-staszic")
return Args.SioInfo.PackagePath()
} else if Args.SioMimuw {
Args.SioInfo.RootPath = filepath.Join(cfg.PackagesPath, "sio-mimuw")
return Args.SioInfo.PackagePath()
}
return "", errors.New(ErrorPackageCouldntBeDetermined)
}