From b25eee72ab8b97d3dfa3dbee9e9162fc8fecb72c Mon Sep 17 00:00:00 2001 From: zcong1993 Date: Fri, 3 Dec 2021 15:00:17 +0800 Subject: [PATCH] feat: allow force update tags toc files --- README.md | 13 +++++++++++++ circle.yml | 14 -------------- cmd/main.go | 5 +++-- cmd/tags/tag.go | 13 ++++++++----- 4 files changed, 24 insertions(+), 21 deletions(-) delete mode 100644 circle.yml diff --git a/README.md b/README.md index a1ebb11..3bc20ab 100644 --- a/README.md +++ b/README.md @@ -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 如何刷题? diff --git a/circle.yml b/circle.yml deleted file mode 100644 index e45e86d..0000000 --- a/circle.yml +++ /dev/null @@ -1,14 +0,0 @@ -version: 2 -jobs: - build: - docker: - # CircleCI Go images available at: https://hub.docker.com/r/circleci/golang/ - - image: circleci/golang:1.10 - - working_directory: /go/src/github.com/zcong1993/leetcode-tool - - steps: - - checkout - - run: - name: build - command: make build diff --git a/cmd/main.go b/cmd/main.go index bb15c73..93bed7d 100644 --- a/cmd/main.go +++ b/cmd/main.go @@ -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) { @@ -62,7 +63,7 @@ func main() { case metaCmd.FullCommand(): showMeta(*metaNumber) case tagsCmd.FullCommand(): - tags.Run() + tags.Run(*tagsForce) } } diff --git a/cmd/tags/tag.go b/cmd/tags/tag.go index bd317b7..95b0722 100644 --- a/cmd/tags/tag.go +++ b/cmd/tags/tag.go @@ -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{} @@ -46,7 +46,7 @@ func Run() { tag := tag wg.Add(1) go func() { - if fileExists(fp) { + if !force && fileExists(fp) { wg.Done() return } @@ -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() }() }