-
Notifications
You must be signed in to change notification settings - Fork 0
/
root.go
67 lines (55 loc) · 1.4 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
package cmd
import (
"errors"
"fmt"
"os"
"github.com/litsea/logger"
"github.com/spf13/cobra"
"github.com/spf13/viper"
"github.com/lostsnow/keqing/internal/config"
"github.com/lostsnow/keqing/internal/db"
_ "github.com/lostsnow/keqing/pkg/i18n/catalog"
)
var (
cfgFile string
profilerHost string
profilerPort int
)
var ErrInvalidCommand = errors.New("invalid command")
var rootCmd = &cobra.Command{
Use: "keqing",
Short: "Keqing bot",
RunE: func(*cobra.Command, []string) error {
return ErrInvalidCommand
},
}
func Execute() {
if err := rootCmd.Execute(); err != nil {
fmt.Println(err)
os.Exit(1)
}
}
func init() {
cobra.OnInitialize(initConfig)
rootCmd.PersistentFlags().StringVar(&cfgFile, "config", "", "config file (default is ./configs/app.yml)")
rootCmd.PersistentFlags().StringVar(&profilerHost, "profiler-host", "127.0.0.1", "profiler host")
rootCmd.PersistentFlags().IntVar(&profilerPort, "profiler-port", 0, "profiler port")
rootCmd.AddCommand(migrateCmd)
rootCmd.AddCommand(botCmd)
}
func initConfig() {
if err := config.ReadConfig(cfgFile, "./configs"); err == nil {
fmt.Printf("using config file: %s\n", viper.ConfigFileUsed())
viper.WatchConfig()
config.SetDefault()
config.InitLogger()
} else {
fmt.Println(err)
os.Exit(1)
}
if err := (&db.Client{}).Init(); err != nil {
logger.Error(err)
os.Exit(1)
}
config.InitProfiler(profilerHost, profilerPort)
}