3 Star 0 Fork 0

mirrors_xalanq/cf-tool

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
文件
克隆/下载
cmd.go 3.79 KB
一键复制 编辑 原始数据 按行查看 历史
xalanq 提交于 2019-04-11 09:21 . Fix some err bugs
package cmd
import (
"errors"
"fmt"
"io/ioutil"
"os"
"path/filepath"
"regexp"
"strconv"
"github.com/fatih/color"
"github.com/xalanq/cf-tool/client"
"github.com/xalanq/cf-tool/config"
"github.com/xalanq/cf-tool/util"
)
// Eval args
func Eval(args map[string]interface{}) error {
if args["config"].(bool) {
return Config(args)
} else if args["submit"].(bool) {
return Submit(args)
} else if args["list"].(bool) {
return List(args)
} else if args["parse"].(bool) {
return Parse(args)
} else if args["gen"].(bool) {
return Gen(args)
} else if args["test"].(bool) {
return Test(args)
}
return nil
}
func getContestID(args map[string]interface{}) (string, error) {
if c, ok := args["<contest-id>"].(string); ok {
if _, err := strconv.Atoi(c); err == nil {
return c, nil
}
return "", fmt.Errorf(`Contest should be a number instead of "%v"`, c)
}
path, err := os.Getwd()
if err != nil {
return "", err
}
for {
c := filepath.Base(path)
if _, err := strconv.Atoi(c); err == nil {
return c, nil
}
if c == path {
break
}
path = filepath.Dir(path)
}
return "", errors.New("Cannot find any valid contest id")
}
func getProblemID(args map[string]interface{}) (string, error) {
if p, ok := args["<problem-id>"].(string); ok {
return p, nil
}
path, err := os.Getwd()
if err != nil {
return "", err
}
return filepath.Base(path), nil
}
func getSampleID() (samples []string) {
path, err := os.Getwd()
if err != nil {
return
}
paths, err := ioutil.ReadDir(path)
if err != nil {
return
}
reg, _ := regexp.Compile(`in(\d+).txt`)
for _, path := range paths {
name := path.Name()
tmp := reg.FindSubmatch([]byte(name))
if tmp != nil {
idx := string(tmp[1])
ans := fmt.Sprintf("ans%v.txt", idx)
if _, err := os.Stat(ans); err == nil {
samples = append(samples, idx)
}
}
}
return
}
// CodeList Name matches some template suffix, index are template array indexes
type CodeList struct {
Name string
Index []int
}
func getCode(args map[string]interface{}, templates []config.CodeTemplate) (codes []CodeList) {
mp := make(map[string][]int)
for i, temp := range templates {
for _, suffix := range temp.Suffix {
sf := "." + suffix
mp[sf] = append(mp[sf], i)
}
}
if filename, ok := args["<filename>"].(string); ok {
ext := filepath.Ext(filename)
if idx, ok := mp[ext]; ok {
return []CodeList{CodeList{filename, idx}}
}
}
path, err := os.Getwd()
if err != nil {
return
}
paths, err := ioutil.ReadDir(path)
if err != nil {
return
}
for _, path := range paths {
name := path.Name()
ext := filepath.Ext(name)
if idx, ok := mp[ext]; ok {
codes = append(codes, CodeList{name, idx})
}
}
return codes
}
func getOneCode(args map[string]interface{}, templates []config.CodeTemplate) (name string, index int, err error) {
codes := getCode(args, templates)
if len(codes) < 1 {
return "", 0, errors.New("Cannot find any supported file\nYou can add some suffixes by `cf config add`")
}
if len(codes) > 1 {
color.Cyan("There are multiple files can be selected.")
for i, code := range codes {
fmt.Printf("%3v: %v\n", i, code.Name)
}
i := util.ChooseIndex(len(codes))
codes[0] = codes[i]
}
if len(codes[0].Index) > 1 {
color.Cyan("There are multiple language match the file.")
for i, idx := range codes[0].Index {
fmt.Printf("%3v: %v\n", i, client.Langs[templates[idx].Lang])
}
i := util.ChooseIndex(len(codes[0].Index))
codes[0].Index[0] = codes[i].Index[i]
}
return codes[0].Name, codes[0].Index[0], nil
}
func loginAgain(cfg *config.Config, cln *client.Client, err error) error {
if err != nil && err.Error() == client.ErrorNotLogged {
color.Red("Not logged. Try to login\n")
password, e := cfg.DecryptPassword()
if e != nil {
return e
}
err = cln.Login(cfg.Username, password)
}
return err
}
Loading...
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
1
https://gitee.com/mirrors_xalanq/cf-tool.git
git@gitee.com:mirrors_xalanq/cf-tool.git
mirrors_xalanq
cf-tool
cf-tool
v0.1.2

搜索帮助

0d507c66 1850385 C8b1a773 1850385