Skip to content

Commit ab5631e

Browse files
committed
Add leet init
1 parent 831b3ef commit ab5631e

File tree

7 files changed

+282
-211
lines changed

7 files changed

+282
-211
lines changed

cmd/init.go

+41-9
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,46 @@
11
package cmd
22

3-
import "github.com/spf13/cobra"
3+
import (
4+
"os"
5+
"path/filepath"
6+
7+
"github.com/j178/leetgo/leetcode"
8+
"github.com/j178/leetgo/utils"
9+
"github.com/spf13/cobra"
10+
"gopkg.in/yaml.v3"
11+
)
412

513
var initCmd = &cobra.Command{
6-
Use: "init",
7-
Short: "Init a leetcode workspace",
8-
Run: func(cmd *cobra.Command, args []string) {
9-
// 生成配置文件
10-
// 生成数据库
11-
// 生成目录
12-
// 写入基础库代码
13-
},
14+
Use: "init",
15+
Short: "Init a leetcode workspace",
16+
Args: cobra.ExactArgs(1),
17+
RunE: func(cmd *cobra.Command, args []string) error {
18+
dir := args[0]
19+
if !utils.IsExist(dir) {
20+
if err := os.MkdirAll(dir, os.ModePerm); err != nil {
21+
return err
22+
}
23+
}
24+
createConfigFile(dir)
25+
createQuestionDB(dir)
26+
// 生成目录
27+
// 写入基础库代码
28+
29+
return nil
30+
},
31+
}
32+
33+
func createConfigFile(dir string) error {
34+
f, err := os.Create(filepath.Join(dir, defaultConfigFile))
35+
if err != nil {
36+
return err
37+
}
38+
enc := yaml.NewEncoder(f)
39+
return enc.Encode(Opts)
40+
}
41+
42+
func createQuestionDB(dir string) error {
43+
leetcode.QuestionsCachePath = filepath.Join(dir, defaultLeetcodeQuestionsCachePath)
44+
c := leetcode.NewClient()
45+
return leetcode.GetCache().Update(c)
1446
}

cmd/root.go

+87-74
Original file line numberDiff line numberDiff line change
@@ -1,101 +1,114 @@
11
package cmd
22

33
import (
4-
"bytes"
5-
"fmt"
6-
"os"
7-
"strings"
8-
9-
"github.com/j178/leetgo/lang"
10-
"github.com/j178/leetgo/leetcode"
11-
"github.com/mitchellh/mapstructure"
12-
"github.com/spf13/cobra"
13-
"github.com/spf13/viper"
4+
"bytes"
5+
"fmt"
6+
"strings"
7+
8+
"github.com/j178/leetgo/lang"
9+
"github.com/j178/leetgo/leetcode"
10+
"github.com/mitchellh/mapstructure"
11+
"github.com/spf13/cobra"
12+
"github.com/spf13/viper"
1413
)
1514

1615
var (
17-
configFile = ""
18-
Version = "0.0.1"
19-
Opts Config
16+
configFile = ""
17+
Version = "0.0.1"
18+
defaultConfigFile = "leet.yml"
19+
defaultLeetcodeQuestionsCachePath = "./data/leetcode-questions.json"
2020
)
2121

2222
type Config struct {
23-
QuestionsDB string `json:"questions_db"`
24-
Go lang.GoConfig `json:"go"`
23+
Cn bool `json:"cn" yaml:"cn"`
24+
LeetCode LeetCodeConfig `json:"leetcode" yaml:"leetcode"`
25+
Go lang.GoConfig `json:"go" yaml:"go"`
26+
}
27+
28+
type LeetCodeConfig struct {
29+
QuestionsCachePath string `json:"questions_cache_path" yaml:"questions_cache_path"`
2530
}
2631

27-
func initConfig() {
28-
Opts = Config{
29-
QuestionsDB: "./data/questions.json",
30-
Go: lang.GoConfig{
31-
SeparatePackage: true,
32-
FilenameTemplate: ``,
33-
},
34-
}
35-
36-
viper.SetConfigName("leet")
37-
viper.AddConfigPath(".")
38-
if configFile != "" {
39-
viper.SetConfigFile(configFile)
40-
}
41-
err := viper.ReadInConfig()
42-
if err != nil {
43-
if _, ok := err.(viper.ConfigFileNotFoundError); !ok {
44-
_, _ = fmt.Fprintln(os.Stderr, err)
45-
os.Exit(1)
46-
}
47-
}
48-
err = viper.Unmarshal(
49-
&Opts, func(c *mapstructure.DecoderConfig) {
50-
c.TagName = "json"
51-
},
52-
)
53-
cobra.CheckErr(err)
54-
55-
leetcode.DbPath = Opts.QuestionsDB
32+
var Opts = Config{
33+
Cn: true,
34+
LeetCode: LeetCodeConfig{
35+
QuestionsCachePath: defaultLeetcodeQuestionsCachePath,
36+
},
37+
Go: lang.GoConfig{
38+
SeparatePackage: true,
39+
FilenameTemplate: ``,
40+
},
41+
}
42+
43+
func initConfig(cmd *cobra.Command, args []string) error {
44+
if cmd == initCmd {
45+
return nil
46+
}
47+
viper.SetConfigName("leet")
48+
viper.AddConfigPath(".")
49+
if configFile != "" {
50+
viper.SetConfigFile(configFile)
51+
}
52+
err := viper.ReadInConfig()
53+
if err != nil {
54+
if _, ok := err.(viper.ConfigFileNotFoundError); !ok {
55+
return err
56+
}
57+
}
58+
err = viper.Unmarshal(
59+
&Opts, func(c *mapstructure.DecoderConfig) {
60+
c.TagName = "json"
61+
},
62+
)
63+
if err != nil {
64+
return err
65+
}
66+
67+
leetcode.QuestionsCachePath = Opts.LeetCode.QuestionsCachePath
68+
return err
5669
}
5770

5871
var rootCmd = &cobra.Command{
59-
Use: "leet",
60-
Short: "Leetcode",
61-
Long: "Leetcode command line tool.",
62-
Version: Version,
72+
Use: "leet",
73+
Short: "Leetcode",
74+
Long: "Leetcode command line tool.",
75+
Version: Version,
76+
PersistentPreRunE: initConfig,
6377
}
6478

6579
func Execute() {
66-
cobra.CheckErr(rootCmd.Execute())
80+
cobra.CheckErr(rootCmd.Execute())
6781
}
6882

6983
func HelpText() string {
70-
out := new(bytes.Buffer)
71-
rootCmd.SetOut(out)
72-
_ = rootCmd.Help()
73-
return out.String()
84+
out := new(bytes.Buffer)
85+
rootCmd.SetOut(out)
86+
_ = rootCmd.Help()
87+
return out.String()
7488
}
7589

7690
func addLangFlags(cmd *cobra.Command) {
77-
for _, l := range lang.SupportedLanguages {
78-
cmd.Flags().Bool(strings.ToLower(l.Name()), false, fmt.Sprintf("generate %s output", l.Name()))
79-
}
91+
for _, l := range lang.SupportedLanguages {
92+
cmd.Flags().Bool(strings.ToLower(l.Name()), false, fmt.Sprintf("generate %s output", l.Name()))
93+
}
8094
}
8195

8296
func init() {
83-
cobra.OnInitialize(initConfig)
84-
cobra.EnableCommandSorting = false
85-
86-
rootCmd.InitDefaultVersionFlag()
87-
rootCmd.UsageTemplate()
88-
rootCmd.PersistentFlags().StringVarP(&configFile, "config", "c", "", "config file path")
89-
rootCmd.PersistentFlags().Bool("cn", true, "use Chinese")
90-
91-
_ = rootCmd.MarkPersistentFlagFilename("config", "yml", "yaml")
92-
_ = viper.BindPFlag("cn", rootCmd.PersistentFlags().Lookup("cn"))
93-
94-
rootCmd.AddCommand(initCmd)
95-
rootCmd.AddCommand(newCmd)
96-
rootCmd.AddCommand(todayCmd)
97-
rootCmd.AddCommand(infoCmd)
98-
rootCmd.AddCommand(testCmd)
99-
rootCmd.AddCommand(contestCmd)
100-
rootCmd.AddCommand(updateCmd)
97+
cobra.EnableCommandSorting = false
98+
99+
rootCmd.InitDefaultVersionFlag()
100+
rootCmd.UsageTemplate()
101+
rootCmd.PersistentFlags().StringVarP(&configFile, "config", "c", "", "config file path")
102+
rootCmd.PersistentFlags().Bool("cn", true, "use Chinese")
103+
104+
_ = rootCmd.MarkPersistentFlagFilename("config", "yml", "yaml")
105+
_ = viper.BindPFlag("cn", rootCmd.PersistentFlags().Lookup("cn"))
106+
107+
rootCmd.AddCommand(initCmd)
108+
rootCmd.AddCommand(newCmd)
109+
rootCmd.AddCommand(todayCmd)
110+
rootCmd.AddCommand(infoCmd)
111+
rootCmd.AddCommand(testCmd)
112+
rootCmd.AddCommand(contestCmd)
113+
rootCmd.AddCommand(updateCmd)
101114
}

lang/go.go

+6-6
Original file line numberDiff line numberDiff line change
@@ -1,22 +1,22 @@
11
package lang
22

33
type GoConfig struct {
4-
SeparatePackage bool `json:"separate_package"`
5-
FilenameTemplate string `json:"filename_template"`
4+
SeparatePackage bool `json:"separate_package" yaml:"separate_package"`
5+
FilenameTemplate string `json:"filename_template" yaml:"filename_template"`
66
}
77

88
type golang struct {
9-
baseLang
9+
baseLang
1010
}
1111

1212
func (golang) Name() string {
13-
return "Go"
13+
return "Go"
1414
}
1515

1616
func (golang) Generate() []any {
17-
return nil
17+
return nil
1818
}
1919

2020
func (golang) GenerateContest() []any {
21-
return nil
21+
return nil
2222
}

0 commit comments

Comments
 (0)