Skip to content

Commit fa6d84a

Browse files
author
Shuo
authored
Merge pull request #791 from openset/develop
A: v1.6.1
2 parents 6a926fa + c9460fb commit fa6d84a

File tree

6 files changed

+83
-70
lines changed

6 files changed

+83
-70
lines changed

internal/base/base.go

+4-41
Original file line numberDiff line numberDiff line change
@@ -10,50 +10,13 @@ import (
1010
"os"
1111
"path/filepath"
1212
"strings"
13-
"sync"
1413
)
1514

16-
// CmdName - base.CmdName
17-
const (
18-
CmdName = "leetcode"
19-
URL = "https://github.com/openset/leetcode/tree/master"
20-
)
21-
22-
// base var
23-
var (
24-
Commands []*Command
25-
Mutex sync.Mutex
26-
)
27-
28-
// Command - base.Command
29-
type Command struct {
30-
Run func(cmd *Command, args []string)
31-
UsageLine string
32-
Short string
33-
Long string
34-
Hidden bool
35-
}
36-
37-
// Name - base.Name
38-
func (c *Command) Name() string {
39-
name := c.UsageLine
40-
if i := strings.Index(name, " "); i > 0 {
41-
name = name[0:i]
42-
}
43-
return name
44-
}
15+
// URL - base.URL
16+
const URL = "https://github.com/openset/leetcode/tree/master"
4517

46-
// Usage - base.Usage
47-
func (c *Command) Usage() {
48-
fmt.Printf("usage: %s %s\n\n", CmdName, c.UsageLine)
49-
fmt.Printf("Run '%s help %s' for details.\n", CmdName, c.Name())
50-
}
51-
52-
// UsageHelp - base.UsageHelp
53-
func (c *Command) UsageHelp() {
54-
fmt.Printf("usage: %s %s\n\n", CmdName, c.UsageLine)
55-
fmt.Println(c.Long)
56-
}
18+
// CmdName - base.CmdName
19+
var CmdName = filepath.Base(os.Args[0])
5720

5821
// Usage - base.Usage
5922
func Usage() {

internal/base/cmd.go

+45
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
// Package base provides base support.
2+
package base
3+
4+
import (
5+
"fmt"
6+
"strings"
7+
)
8+
9+
// Commands - base.Commands
10+
var Commands []*Command
11+
12+
// Command - base.Command
13+
type Command struct {
14+
Run func(cmd *Command, args []string)
15+
UsageLine string
16+
Short string
17+
Long string
18+
Hidden bool
19+
}
20+
21+
// Name - base.Command.Name
22+
func (c *Command) Name() string {
23+
name := c.UsageLine
24+
if i := strings.Index(name, " "); i > 0 {
25+
name = name[0:i]
26+
}
27+
return name
28+
}
29+
30+
// Usage - base.Command.Usage
31+
func (c *Command) Usage() {
32+
fmt.Printf("usage: %s %s\n\n", CmdName, c.UsageLine)
33+
fmt.Printf("Run '%s help %s' for details.\n", CmdName, c.Name())
34+
}
35+
36+
// UsageHelp - base.Command.UsageHelp
37+
func (c *Command) UsageHelp() {
38+
fmt.Printf("usage: %s %s\n\n", CmdName, c.UsageLine)
39+
fmt.Println(c.Long)
40+
}
41+
42+
// Register - base.Register
43+
func Register(cmds ...*Command) {
44+
Commands = append(Commands, cmds...)
45+
}

internal/base/run.go

+25
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
package base
2+
3+
import (
4+
"flag"
5+
"fmt"
6+
)
7+
8+
func Run() {
9+
flag.Usage = Usage
10+
flag.Parse()
11+
if flag.NArg() < 1 {
12+
flag.Usage()
13+
return
14+
}
15+
args := flag.Args()
16+
cmdName := flag.Arg(0)
17+
for _, cmd := range Commands {
18+
if cmd.Name() == cmdName {
19+
cmd.Run(cmd, args[1:])
20+
return
21+
}
22+
}
23+
fmt.Printf("%s %s: unknown command\n\n", CmdName, cmdName)
24+
fmt.Printf("Run '%s help' for usage.\n", CmdName)
25+
}

internal/leetcode/topic_tag.go

+4-3
Original file line numberDiff line numberDiff line change
@@ -9,12 +9,13 @@ import (
99
"regexp"
1010
"sort"
1111
"strconv"
12+
"sync"
1213

13-
"github.com/openset/leetcode/internal/base"
1414
"github.com/openset/leetcode/internal/client"
1515
)
1616

1717
var (
18+
mu sync.Mutex
1819
initTags []TagType
1920
tagsFile = filepath.Join("tag", "tags.json")
2021
)
@@ -135,10 +136,10 @@ func GetTopicTag(slug string) (tt TopicTagType) {
135136
}
136137

137138
func saveTags(tags []TagType) {
138-
base.Mutex.Lock()
139+
mu.Lock()
140+
defer mu.Unlock()
139141
tags = append(GetTags(), tags...)
140142
filePutContents(tagsFile, jsonEncode(tagsUnique(tags)))
141-
base.Mutex.Unlock()
142143
}
143144

144145
func tagsUnique(tags []TagType) []TagType {

internal/version/version.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ import (
88
"github.com/openset/leetcode/internal/base"
99
)
1010

11-
const version = "1.6.0"
11+
const version = "1.6.1"
1212

1313
// CmdVersion - version.CmdVersion
1414
var CmdVersion = &base.Command{

main.go

+4-25
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,6 @@
22
package main
33

44
import (
5-
"flag"
6-
"fmt"
7-
85
"github.com/openset/leetcode/internal/base"
96
"github.com/openset/leetcode/internal/build"
107
"github.com/openset/leetcode/internal/clean"
@@ -22,8 +19,8 @@ import (
2219
"github.com/openset/leetcode/internal/version"
2320
)
2421

25-
func init() {
26-
base.Commands = []*base.Command{
22+
func main() {
23+
base.Register(
2724
readme.CmdReadme,
2825
page.CmdPage,
2926
tag.CmdTag,
@@ -38,24 +35,6 @@ func init() {
3835
version.CmdVersion,
3936
help.CmdHelp,
4037
post.CmdPost,
41-
}
42-
}
43-
44-
func main() {
45-
flag.Usage = base.Usage
46-
flag.Parse()
47-
if flag.NArg() < 1 {
48-
flag.Usage()
49-
return
50-
}
51-
args := flag.Args()
52-
cmdName := flag.Arg(0)
53-
for _, cmd := range base.Commands {
54-
if cmd.Name() == cmdName {
55-
cmd.Run(cmd, args[1:])
56-
return
57-
}
58-
}
59-
fmt.Printf("%s %s: unknown command\n\n", base.CmdName, cmdName)
60-
fmt.Printf("Run '%s help' for usage.\n", base.CmdName)
38+
)
39+
base.Run()
6140
}

0 commit comments

Comments
 (0)