-
Notifications
You must be signed in to change notification settings - Fork 220
/
parse.go
92 lines (88 loc) · 2.21 KB
/
parse.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
package cmd
import (
"errors"
"os"
"path/filepath"
"strings"
"github.com/fatih/color"
ansi "github.com/k0kubun/go-ansi"
"github.com/xalanq/cf-tool/client"
"github.com/xalanq/cf-tool/config"
)
// Parse command
func Parse(args map[string]interface{}) error {
currentPath, err := os.Getwd()
if err != nil {
return err
}
cfg := config.Instance
cln := client.Instance
source := ""
ext := ""
if cfg.GenAfterParse {
if len(cfg.Template) == 0 {
return errors.New("You have to add at least one code template by `cf config`")
}
path := cfg.Template[cfg.Default].Path
ext = filepath.Ext(path)
if source, err = readTemplateSource(path, cln); err != nil {
return err
}
}
parseContest := func(contestID, rootPath string) error {
problems, err := cln.ParseContest(contestID, rootPath)
if err == nil && cfg.GenAfterParse {
for _, problem := range problems {
problemID := strings.ToLower(problem.ID)
path := filepath.Join(rootPath, problemID)
gen(source, path, ext)
}
}
return err
}
work := func() error {
contestID := ""
problemID := ""
path := currentPath
var ok bool
if contestID, ok = args["<contest-id>"].(string); ok {
if problemID, ok = args["<problem-id>"].(string); !ok {
return parseContest(contestID, filepath.Join(currentPath, contestID))
}
problemID = strings.ToLower(problemID)
path = filepath.Join(currentPath, contestID, problemID)
} else {
contestID, err = getContestID(args)
if err != nil {
return err
}
problemID, err = getProblemID(args)
if err != nil {
return err
}
if problemID == contestID {
return parseContest(contestID, currentPath)
}
}
samples, standardIO, err := cln.ParseContestProblem(contestID, problemID, path)
if err != nil {
color.Red("Failed %v %v", contestID, problemID)
return err
}
warns := ""
if !standardIO {
warns = color.YellowString("Non standard input output format.")
}
ansi.Printf("%v %v\n", color.GreenString("Parsed %v %v with %v samples.", contestID, problemID, samples), warns)
if cfg.GenAfterParse {
gen(source, path, ext)
}
return nil
}
if err = work(); err != nil {
if err = loginAgain(cfg, cln, err); err == nil {
err = work()
}
}
return err
}