Skip to content

Commit

Permalink
feat: allow force update tags toc files
Browse files Browse the repository at this point in the history
  • Loading branch information
zcong1993 committed Dec 3, 2021
1 parent de160ca commit b25eee7
Show file tree
Hide file tree
Showing 4 changed files with 24 additions and 21 deletions.
13 changes: 13 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,19 @@ number 为网页上显示的题目序号, 例如: `leetcode-tool new 1` 创建

从 leetcode 网站拉取最新分类, 并创建 `toc` 对应文件, 一般不需要运行.

### 重置项目

假如想要重新从零开始, 或者你的项目是基于别人项目 fork 的, 可以使用如下方式清理已有题解:

```shell
# 1. 删除所有题解文件
rm -rf solve/*
# 2. 重新构建 toc 文件, -f 参数会强制覆盖
leetcode-tool tags -f
# 2.1 假如你还保留了部分题解, 还需要更新下题目状态
leetcode-tool update
```

## Workflow

如何刷题?
Expand Down
14 changes: 0 additions & 14 deletions circle.yml

This file was deleted.

5 changes: 3 additions & 2 deletions cmd/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,8 @@ var (
metaCmd = app.Command("meta", "Show problem meta by number.")
metaNumber = metaCmd.Arg("number", "problem number").Required().String()

tagsCmd = app.Command("tags", "Update tag toc files.")
tagsCmd = app.Command("tags", "Update tag toc files.")
tagsForce = tagsCmd.Flag("force", "force update file").Short('f').Bool()
)

func showMeta(number string) {
Expand Down Expand Up @@ -62,7 +63,7 @@ func main() {
case metaCmd.FullCommand():
showMeta(*metaNumber)
case tagsCmd.FullCommand():
tags.Run()
tags.Run(*tagsForce)
}
}

Expand Down
13 changes: 8 additions & 5 deletions cmd/tags/tag.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,17 +21,17 @@ func fileExists(path string) bool {
return !os.IsNotExist(err)
}

func Run() {
func Run(force bool) {
tags, err := leetcode.GetTags()
if err != nil {
log.Fatal(err)
}

tags = append(tags, leetcode.Tag{
tags = append([]leetcode.Tag{{
Name: "all",
Slug: "all",
TranslatedName: "汇总",
})
}}, tags...)

wg := sync.WaitGroup{}
sb := strings.Builder{}
Expand All @@ -46,7 +46,7 @@ func Run() {
tag := tag
wg.Add(1)
go func() {
if fileExists(fp) {
if !force && fileExists(fp) {
wg.Done()
return
}
Expand All @@ -55,7 +55,10 @@ func Run() {
if err != nil {
log.Fatal(err)
}
ioutil.WriteFile(fp, content.Bytes(), 0644)
err = ioutil.WriteFile(fp, content.Bytes(), 0644)
if err != nil {
log.Printf("write file %s error, %s\n", fp, err)
}
wg.Done()
}()
}
Expand Down

0 comments on commit b25eee7

Please sign in to comment.