Skip to content

Commit b50fb7e

Browse files
author
Openset
committed
Add: CmdHelper
1 parent a22497e commit b50fb7e

File tree

4 files changed

+55
-1
lines changed

4 files changed

+55
-1
lines changed

helper/README.md

+5
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
<!--This file generated by command(leetcode helper); DO NOT EDIT.-->
2+
13
# Helper
24

35
```text
@@ -7,8 +9,11 @@ Usage:
79
leetcode <command> [arguments]
810
The commands are:
911
readme build README.md file
12+
helper build helper file
13+
question build problem description file
1014
test run go test
1115
update update self
16+
description build all problems description file
1217
version print leetcode version
1318
help show command usage
1419

internal/base/base.go

+17
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,9 @@ package base
22

33
import (
44
"fmt"
5+
"io/ioutil"
56
"os"
7+
"path"
68
"strings"
79
)
810

@@ -51,6 +53,21 @@ func Usage() {
5153
Exit()
5254
}
5355

56+
func FilePutContents(filename string, data []byte) {
57+
filename = getFilePath(filename)
58+
err := ioutil.WriteFile(filename, data, 0644)
59+
CheckErr(err)
60+
}
61+
62+
func getFilePath(filename string) string {
63+
if dir := path.Dir(filename); dir != "" {
64+
if err := os.MkdirAll(dir, 0755); err != nil {
65+
CheckErr(err)
66+
}
67+
}
68+
return filename
69+
}
70+
5471
func CheckErr(err error) {
5572
if err != nil {
5673
panic(err)

internal/helper/helper.go

+30
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
package helper
2+
3+
import (
4+
"bytes"
5+
"os/exec"
6+
7+
"github.com/openset/leetcode/internal/base"
8+
)
9+
10+
var CmdHelper = &base.Command{
11+
Run: runHelper,
12+
UsageLine: "helper",
13+
Short: "build helper file",
14+
Long: "build helper README.md file.",
15+
}
16+
17+
func runHelper(cmd *base.Command, args []string) {
18+
if len(args) != 0 {
19+
cmd.Usage()
20+
}
21+
var bf bytes.Buffer
22+
bf.WriteString("<!--This file generated by command(leetcode helper); DO NOT EDIT.-->\n\n")
23+
bf.WriteString("# Helper\n\n")
24+
bf.WriteString("```text\n")
25+
out, err := exec.Command(base.CmdName).Output()
26+
base.CheckErr(err)
27+
bf.Write(out)
28+
bf.WriteString("```\n")
29+
base.FilePutContents("helper/README.md", bf.Bytes())
30+
}

main.go

+3-1
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ import (
88
"github.com/openset/leetcode/internal/base"
99
"github.com/openset/leetcode/internal/description"
1010
"github.com/openset/leetcode/internal/help"
11+
"github.com/openset/leetcode/internal/helper"
1112
"github.com/openset/leetcode/internal/question"
1213
"github.com/openset/leetcode/internal/readme"
1314
"github.com/openset/leetcode/internal/test"
@@ -19,9 +20,10 @@ func init() {
1920
base.Commands = []*base.Command{
2021
readme.CmdReadme,
2122
question.CmdQuestion,
23+
description.CmdDescription,
2224
test.CmdTest,
2325
update.CmdUpdate,
24-
description.CmdDescription,
26+
helper.CmdHelper,
2527
version.CmdVersion,
2628
help.CmdHelp,
2729
}

0 commit comments

Comments
 (0)