Skip to content

Commit

Permalink
Add run 'cf gen' after 'cf parse' option in config cmd. Close #25
Browse files Browse the repository at this point in the history
  • Loading branch information
xalanq committed Aug 14, 2019
1 parent ecbaa42 commit 0b8945e
Show file tree
Hide file tree
Showing 7 changed files with 89 additions and 32 deletions.
2 changes: 1 addition & 1 deletion cf.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ import (
docopt "github.com/docopt/docopt-go"
)

const version = "v0.7.2"
const version = "v0.8.0"

func main() {
usage := `Codeforces Tool $%version%$ (cf). https://github.com/xalanq/cf-tool
Expand Down
4 changes: 2 additions & 2 deletions client/parse.go
Original file line number Diff line number Diff line change
Expand Up @@ -97,8 +97,8 @@ func (c *Client) ParseContestProblem(contestID, problemID, path string) (samples
}

// ParseContest parse for contest
func (c *Client) ParseContest(contestID, rootPath string, race bool) (err error) {
problems, err := c.StatisContest(contestID)
func (c *Client) ParseContest(contestID, rootPath string, race bool) (problems []StatisInfo, err error) {
problems, err = c.StatisContest(contestID)
if err != nil {
return
}
Expand Down
13 changes: 8 additions & 5 deletions cmd/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,11 +11,12 @@ import (
func Config(args map[string]interface{}) error {
color.Cyan("Configure the tool")
cfg := config.New(config.ConfigPath)
ansi.Println("0) username and password")
ansi.Println("1) add a template")
ansi.Println("2) delete a template")
ansi.Println("3) set default template")
index := util.ChooseIndex(4)
ansi.Println(`0) username and password`)
ansi.Println(`1) add a template`)
ansi.Println(`2) delete a template`)
ansi.Println(`3) set default template`)
ansi.Println(`4) run "cf gen" after "cf parse"`)
index := util.ChooseIndex(5)
if index == 0 {
return cfg.Login(config.SessionPath)
} else if index == 1 {
Expand All @@ -24,6 +25,8 @@ func Config(args map[string]interface{}) error {
return cfg.RemoveTemplate()
} else if index == 3 {
return cfg.SetDefaultTemplate()
} else if index == 4 {
return cfg.SetGenAfterParse()
}
return nil
}
49 changes: 32 additions & 17 deletions cmd/gen.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,34 @@ func parseTemplate(source string, cfg *config.Config) string {
return source
}

func readTemplateSource(path string, cfg *config.Config) (source string, err error) {
b, err := ioutil.ReadFile(path)
if err != nil {
return
}
source = parseTemplate(string(b), cfg)
return
}

func gen(source, currentPath, ext string) error {
path := filepath.Join(currentPath, filepath.Base(currentPath))

savePath := path + ext
i := 1
for _, err := os.Stat(savePath); err == nil; _, err = os.Stat(savePath) {
tmpPath := fmt.Sprintf("%v%v%v", path, i, ext)
fmt.Printf("%v exists. Rename to %v\n", filepath.Base(savePath), filepath.Base(tmpPath))
savePath = tmpPath
i++
}

err := ioutil.WriteFile(savePath, []byte(source), 0644)
if err == nil {
color.Green("Generated! See %v", filepath.Base(savePath))
}
return err
}

// Gen command
func Gen(args map[string]interface{}) error {
cfg := config.New(config.ConfigPath)
Expand Down Expand Up @@ -54,30 +82,17 @@ func Gen(args map[string]interface{}) error {
path = cfg.Template[cfg.Default].Path
}

b, err := ioutil.ReadFile(path)
source, err := readTemplateSource(path, cfg)
if err != nil {
return err
}
source := parseTemplate(string(b), cfg)

currentPath, err := os.Getwd()
if err != nil {
return err
}
ext := filepath.Ext(path)
path = filepath.Join(currentPath, filepath.Base(currentPath))

savePath := path + ext
i := 1
for _, err := os.Stat(savePath); err == nil; _, err = os.Stat(savePath) {
tmpPath := fmt.Sprintf("%v%v%v", path, i, ext)
fmt.Printf("%v exists. Rename to %v\n", filepath.Base(savePath), filepath.Base(tmpPath))
savePath = tmpPath
i++
}

err = ioutil.WriteFile(savePath, []byte(source), 0644)
if err == nil {
color.Green("Generated! See %v", filepath.Base(savePath))
}
ext := filepath.Ext(path)
gen(source, currentPath, ext)
return err
}
31 changes: 29 additions & 2 deletions cmd/parse.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package cmd

import (
"errors"
"os"
"path/filepath"
"strings"
Expand All @@ -17,15 +18,38 @@ func Parse(args map[string]interface{}) error {
return err
}
cfg := config.New(config.ConfigPath)
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 add`")
}
path := cfg.Template[cfg.Default].Path
ext = filepath.Ext(path)
if source, err = readTemplateSource(path, cfg); err != nil {
return err
}
}
cln := client.New(config.SessionPath)
parseContest := func(contestID, rootPath string, race bool) error {
problems, err := cln.ParseContest(contestID, rootPath, race)
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 cln.ParseContest(contestID, filepath.Join(currentPath, contestID), args["race"].(bool))
return parseContest(contestID, filepath.Join(currentPath, contestID), args["race"].(bool))
}
problemID = strings.ToLower(problemID)
path = filepath.Join(currentPath, contestID, problemID)
Expand All @@ -39,7 +63,7 @@ func Parse(args map[string]interface{}) error {
return err
}
if problemID == contestID {
return cln.ParseContest(contestID, currentPath, args["race"].(bool))
return parseContest(contestID, currentPath, args["race"].(bool))
}
}
samples, err := cln.ParseContestProblem(contestID, problemID, path)
Expand All @@ -48,6 +72,9 @@ func Parse(args map[string]interface{}) error {
return err
}
color.Green("Parsed %v %v with %v samples", contestID, problemID, samples)
if cfg.GenAfterParse {
gen(source, path, ext)
}
return nil
}
if err = work(); err != nil {
Expand Down
11 changes: 6 additions & 5 deletions config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,11 +21,12 @@ type CodeTemplate struct {

// Config load and save configuration
type Config struct {
Username string `json:"username"`
Password string `json:"password"`
Template []CodeTemplate `json:"template"`
Default int `json:"default"`
path string
Username string `json:"username"`
Password string `json:"password"`
Template []CodeTemplate `json:"template"`
Default int `json:"default"`
GenAfterParse bool `json:"gen_after_parse"`
path string
}

// New an empty config
Expand Down
11 changes: 11 additions & 0 deletions config/misc.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
package config

import (
"github.com/xalanq/cf-tool/util"
)

// SetGenAfterParse set it yes or no
func (c *Config) SetGenAfterParse() (err error) {
c.GenAfterParse = util.YesOrNo(`Run "cf gen" after "cf test" (y/n)? `)

This comment has been minimized.

Copy link
@Siyuanwww

Siyuanwww Aug 15, 2019

Please change "cf test" to "cf parse", maybe it is a mistake. 😃

This comment has been minimized.

Copy link
@xalanq

xalanq Aug 15, 2019

Author Owner

omg, THX!

return c.save()
}

0 comments on commit 0b8945e

Please sign in to comment.