代码拉取完成,页面将自动刷新
package cmd
import (
"errors"
"fmt"
"io/ioutil"
"os"
"path/filepath"
"regexp"
"github.com/docopt/docopt-go"
"github.com/fatih/color"
"github.com/xalanq/cf-tool/client"
"github.com/xalanq/cf-tool/config"
"github.com/xalanq/cf-tool/util"
)
// Eval opts
func Eval(opts docopt.Opts) error {
Args = &ParsedArgs{}
opts.Bind(Args)
if err := parseArgs(opts); err != nil {
return err
}
if Args.Config {
return Config()
} else if Args.Submit {
return Submit()
} else if Args.List {
return List()
} else if Args.Parse {
return Parse()
} else if Args.Gen {
return Gen()
} else if Args.Test {
return Test()
} else if Args.Watch {
return Watch()
} else if Args.Open {
return Open()
} else if Args.Stand {
return Stand()
} else if Args.Sid {
return Sid()
} else if Args.Race {
return Race()
} else if Args.Pull {
return Pull()
} else if Args.Clone {
return Clone()
} else if Args.Upgrade {
return Upgrade()
}
return nil
}
func getSampleID() (samples []string) {
path, err := os.Getwd()
if err != nil {
return
}
paths, err := ioutil.ReadDir(path)
if err != nil {
return
}
reg := regexp.MustCompile(`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(filename string, templates []config.CodeTemplate) (codes []CodeList, err error) {
mp := make(map[string][]int)
for i, temp := range templates {
suffixMap := map[string]bool{}
for _, suffix := range temp.Suffix {
if _, ok := suffixMap[suffix]; !ok {
suffixMap[suffix] = true
sf := "." + suffix
mp[sf] = append(mp[sf], i)
}
}
}
if filename != "" {
ext := filepath.Ext(filename)
if idx, ok := mp[ext]; ok {
return []CodeList{CodeList{filename, idx}}, nil
}
return nil, fmt.Errorf("%v can not match any template. You could add a new template by `cf config`", filename)
}
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, nil
}
func getOneCode(filename string, templates []config.CodeTemplate) (name string, index int, err error) {
codes, err := getCode(filename, templates)
if err != nil {
return
}
if len(codes) < 1 {
return "", 0, errors.New("Cannot find any code.\nMaybe you should add a new template by `cf config`")
}
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 languages 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[0].Index[i]
}
return codes[0].Name, codes[0].Index[0], nil
}
func loginAgain(cln *client.Client, err error) error {
if err != nil && err.Error() == client.ErrorNotLogged {
color.Red("Not logged. Try to login\n")
err = cln.Login()
}
return err
}
此处可能存在不合适展示的内容,页面不予展示。您可通过相关编辑功能自查并修改。
如您确认内容无涉及 不当用语 / 纯广告导流 / 暴力 / 低俗色情 / 侵权 / 盗版 / 虚假 / 无价值内容或违法国家有关法律法规的内容,可点击提交进行申诉,我们将尽快为您处理。