-
Notifications
You must be signed in to change notification settings - Fork 33
/
root.go
178 lines (161 loc) · 4.41 KB
/
root.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
package cmd
import (
"errors"
"fmt"
"os"
"runtime"
"runtime/debug"
"github.com/charmbracelet/log"
cc "github.com/ivanpirog/coloredcobra"
"github.com/joho/godotenv"
"github.com/spf13/cobra"
"github.com/spf13/viper"
"github.com/j178/leetgo/config"
"github.com/j178/leetgo/constants"
"github.com/j178/leetgo/lang"
)
func buildVersion() string {
result := constants.Version
if constants.Commit != "" {
result = fmt.Sprintf("%s\ncommit: %s", result, constants.Commit)
}
if constants.BuildDate != "" {
result = fmt.Sprintf("%s\nbuilt at: %s", result, constants.BuildDate)
}
result = fmt.Sprintf("%s\ngoos: %s\ngoarch: %s", result, runtime.GOOS, runtime.GOARCH)
if info, ok := debug.ReadBuildInfo(); ok && info.Main.Sum != "" {
result = fmt.Sprintf("%s\nmodule version: %s, checksum: %s", result, info.Main.Version, info.Main.Sum)
}
return result
}
var rootCmd = &cobra.Command{
Use: constants.CmdName,
Short: "Leetcode",
Long: "Leetcode friend for geek.",
Version: buildVersion() + "\n\n" + constants.ProjectURL,
SilenceErrors: true,
SilenceUsage: true,
}
type exitCode int
func (e exitCode) Error() string {
return fmt.Sprintf("exit code %d", e)
}
func Execute() {
err := rootCmd.Execute()
if err != nil {
var e exitCode
if errors.As(err, &e) {
os.Exit(int(e))
}
log.Fatal(err)
}
}
func preRun(cmd *cobra.Command, _ []string) error {
initLogger()
err := initWorkDir()
if err != nil {
return err
}
err = config.Load(cmd == initCmd)
if err != nil {
return err
}
err = godotenv.Load()
if err != nil && !os.IsNotExist(err) {
return err
}
return nil
}
func UsageString() string {
return rootCmd.UsageString()
}
func initWorkDir() error {
if dir := os.Getenv("LEETGO_WORKDIR"); dir != "" {
log.Debug("change workdir to LEETGO_WORKDIR", "dir", dir)
return os.Chdir(dir)
}
return nil
}
func initLogger() {
if config.Debug {
log.SetReportTimestamp(true)
log.SetLevel(log.DebugLevel)
} else {
style := log.DefaultStyles()
style.Levels[log.DebugLevel] = style.Levels[log.DebugLevel].SetString("●")
style.Levels[log.InfoLevel] = style.Levels[log.InfoLevel].SetString("●")
style.Levels[log.WarnLevel] = style.Levels[log.WarnLevel].SetString("●")
style.Levels[log.ErrorLevel] = style.Levels[log.ErrorLevel].SetString("×")
style.Levels[log.FatalLevel] = style.Levels[log.FatalLevel].SetString("×")
log.SetStyles(style)
log.SetReportTimestamp(false)
log.SetLevel(log.InfoLevel)
}
}
func initCommands() {
cobra.EnableCommandSorting = false
rootCmd.SetOut(os.Stdout)
rootCmd.InitDefaultVersionFlag()
rootCmd.Flags().SortFlags = false
rootCmd.PersistentFlags().StringP("lang", "l", "", "language of code to generate: cpp, go, python ...")
rootCmd.PersistentFlags().StringP("site", "", "", "leetcode site: cn, us")
rootCmd.PersistentFlags().BoolP("yes", "y", false, "answer yes to all prompts")
rootCmd.InitDefaultHelpFlag()
_ = viper.BindPFlag("code.lang", rootCmd.PersistentFlags().Lookup("lang"))
_ = viper.BindPFlag("leetcode.site", rootCmd.PersistentFlags().Lookup("site"))
_ = viper.BindPFlag("yes", rootCmd.PersistentFlags().Lookup("yes"))
_ = rootCmd.RegisterFlagCompletionFunc(
"lang", func(cmd *cobra.Command, args []string, toComplete string) ([]string, cobra.ShellCompDirective) {
langs := make([]string, 0, len(lang.SupportedLangs))
for _, l := range lang.SupportedLangs {
langs = append(langs, l.Slug())
}
return langs, cobra.ShellCompDirectiveNoFileComp
},
)
_ = rootCmd.RegisterFlagCompletionFunc(
"site",
func(cmd *cobra.Command, args []string, toComplete string) ([]string, cobra.ShellCompDirective) {
return []string{"cn", "us"}, cobra.ShellCompDirectiveNoFileComp
},
)
commands := []*cobra.Command{
initCmd,
pickCmd,
infoCmd,
testCmd,
submitCmd,
fixCmd,
editCmd,
extractCmd,
contestCmd,
cacheCmd,
debugCmd,
gitCmd,
inspectCmd,
whoamiCmd,
openCmd,
}
for _, cmd := range commands {
cmd.Flags().SortFlags = false
cmd.PersistentPreRunE = preRun
rootCmd.AddCommand(cmd)
}
rootCmd.InitDefaultHelpCmd()
rootCmd.CompletionOptions.HiddenDefaultCmd = true
cc.Init(
&cc.Config{
RootCmd: rootCmd,
Headings: cc.HiCyan + cc.Bold + cc.Underline,
Commands: cc.HiYellow + cc.Bold,
Example: cc.Italic,
ExecName: cc.Bold,
Flags: cc.Bold,
NoExtraNewlines: true,
NoBottomNewline: true,
},
)
}
func init() {
initCommands()
}