|
1 | 1 | package cmd
|
2 | 2 |
|
3 | 3 | 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" |
14 | 13 | )
|
15 | 14 |
|
16 | 15 | 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" |
20 | 20 | )
|
21 | 21 |
|
22 | 22 | 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"` |
25 | 30 | }
|
26 | 31 |
|
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 |
56 | 69 | }
|
57 | 70 |
|
58 | 71 | 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, |
63 | 77 | }
|
64 | 78 |
|
65 | 79 | func Execute() {
|
66 |
| - cobra.CheckErr(rootCmd.Execute()) |
| 80 | + cobra.CheckErr(rootCmd.Execute()) |
67 | 81 | }
|
68 | 82 |
|
69 | 83 | 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() |
74 | 88 | }
|
75 | 89 |
|
76 | 90 | 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 | + } |
80 | 94 | }
|
81 | 95 |
|
82 | 96 | 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) |
101 | 114 | }
|
0 commit comments