blob: 58031663e95621f2062ec2ed257c94832b6d2c02 (
plain) (
blame)
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
|
package cmd
import (
"fmt"
"log/slog"
"os"
"github.com/spf13/cobra"
"github.com/spf13/viper"
)
var (
cfgFile string
dbFile string
debug bool
rootCmd = &cobra.Command{Use: "gorum"}
)
func init() {
cobra.OnInitialize(initLogger)
cobra.OnInitialize(initConfig)
rootCmd.PersistentFlags().StringVar(&cfgFile, "config", "gorum.yaml", "config file")
rootCmd.PersistentFlags().StringVar(&dbFile, "db", "gorum.db", "database file")
rootCmd.PersistentFlags().BoolVar(&debug, "debug", false, "verbose debug output")
viper.BindPFlag("db", serverCmd.Flags().Lookup("db"))
rootCmd.AddCommand(serverCmd)
rootCmd.AddCommand(migrateCmd)
rootCmd.AddCommand(userCmd)
}
func initLogger() {
level := slog.LevelInfo
if debug {
level = slog.LevelDebug
}
logger := slog.NewTextHandler(os.Stdout, &slog.HandlerOptions{Level: level})
slog.SetDefault(slog.New(logger))
}
func initConfig() {
viper.SetConfigFile(cfgFile)
if err := viper.ReadInConfig(); err != nil {
slog.Warn("can't read config", slog.Any("error", err))
}
}
func Execute() {
if err := rootCmd.Execute(); err != nil {
fmt.Println(err)
os.Exit(1)
}
}
|