Skip to content

A: v1.6.1 #791

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
May 30, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
45 changes: 4 additions & 41 deletions internal/base/base.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,50 +10,13 @@ import (
"os"
"path/filepath"
"strings"
"sync"
)

// CmdName - base.CmdName
const (
CmdName = "leetcode"
URL = "https://github.com/openset/leetcode/tree/master"
)

// base var
var (
Commands []*Command
Mutex sync.Mutex
)

// Command - base.Command
type Command struct {
Run func(cmd *Command, args []string)
UsageLine string
Short string
Long string
Hidden bool
}

// Name - base.Name
func (c *Command) Name() string {
name := c.UsageLine
if i := strings.Index(name, " "); i > 0 {
name = name[0:i]
}
return name
}
// URL - base.URL
const URL = "https://github.com/openset/leetcode/tree/master"

// Usage - base.Usage
func (c *Command) Usage() {
fmt.Printf("usage: %s %s\n\n", CmdName, c.UsageLine)
fmt.Printf("Run '%s help %s' for details.\n", CmdName, c.Name())
}

// UsageHelp - base.UsageHelp
func (c *Command) UsageHelp() {
fmt.Printf("usage: %s %s\n\n", CmdName, c.UsageLine)
fmt.Println(c.Long)
}
// CmdName - base.CmdName
var CmdName = filepath.Base(os.Args[0])

// Usage - base.Usage
func Usage() {
Expand Down
45 changes: 45 additions & 0 deletions internal/base/cmd.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
// Package base provides base support.
package base

import (
"fmt"
"strings"
)

// Commands - base.Commands
var Commands []*Command

// Command - base.Command
type Command struct {
Run func(cmd *Command, args []string)
UsageLine string
Short string
Long string
Hidden bool
}

// Name - base.Command.Name
func (c *Command) Name() string {
name := c.UsageLine
if i := strings.Index(name, " "); i > 0 {
name = name[0:i]
}
return name
}

// Usage - base.Command.Usage
func (c *Command) Usage() {
fmt.Printf("usage: %s %s\n\n", CmdName, c.UsageLine)
fmt.Printf("Run '%s help %s' for details.\n", CmdName, c.Name())
}

// UsageHelp - base.Command.UsageHelp
func (c *Command) UsageHelp() {
fmt.Printf("usage: %s %s\n\n", CmdName, c.UsageLine)
fmt.Println(c.Long)
}

// Register - base.Register
func Register(cmds ...*Command) {
Commands = append(Commands, cmds...)
}
25 changes: 25 additions & 0 deletions internal/base/run.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
package base

import (
"flag"
"fmt"
)

func Run() {
flag.Usage = Usage
flag.Parse()
if flag.NArg() < 1 {
flag.Usage()
return
}
args := flag.Args()
cmdName := flag.Arg(0)
for _, cmd := range Commands {
if cmd.Name() == cmdName {
cmd.Run(cmd, args[1:])
return
}
}
fmt.Printf("%s %s: unknown command\n\n", CmdName, cmdName)
fmt.Printf("Run '%s help' for usage.\n", CmdName)
}
7 changes: 4 additions & 3 deletions internal/leetcode/topic_tag.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,12 +9,13 @@ import (
"regexp"
"sort"
"strconv"
"sync"

"github.com/openset/leetcode/internal/base"
"github.com/openset/leetcode/internal/client"
)

var (
mu sync.Mutex
initTags []TagType
tagsFile = filepath.Join("tag", "tags.json")
)
Expand Down Expand Up @@ -135,10 +136,10 @@ func GetTopicTag(slug string) (tt TopicTagType) {
}

func saveTags(tags []TagType) {
base.Mutex.Lock()
mu.Lock()
defer mu.Unlock()
tags = append(GetTags(), tags...)
filePutContents(tagsFile, jsonEncode(tagsUnique(tags)))
base.Mutex.Unlock()
}

func tagsUnique(tags []TagType) []TagType {
Expand Down
2 changes: 1 addition & 1 deletion internal/version/version.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import (
"github.com/openset/leetcode/internal/base"
)

const version = "1.6.0"
const version = "1.6.1"

// CmdVersion - version.CmdVersion
var CmdVersion = &base.Command{
Expand Down
29 changes: 4 additions & 25 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,6 @@
package main

import (
"flag"
"fmt"

"github.com/openset/leetcode/internal/base"
"github.com/openset/leetcode/internal/build"
"github.com/openset/leetcode/internal/clean"
Expand All @@ -22,8 +19,8 @@ import (
"github.com/openset/leetcode/internal/version"
)

func init() {
base.Commands = []*base.Command{
func main() {
base.Register(
readme.CmdReadme,
page.CmdPage,
tag.CmdTag,
Expand All @@ -38,24 +35,6 @@ func init() {
version.CmdVersion,
help.CmdHelp,
post.CmdPost,
}
}

func main() {
flag.Usage = base.Usage
flag.Parse()
if flag.NArg() < 1 {
flag.Usage()
return
}
args := flag.Args()
cmdName := flag.Arg(0)
for _, cmd := range base.Commands {
if cmd.Name() == cmdName {
cmd.Run(cmd, args[1:])
return
}
}
fmt.Printf("%s %s: unknown command\n\n", base.CmdName, cmdName)
fmt.Printf("Run '%s help' for usage.\n", base.CmdName)
)
base.Run()
}