-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathroot.go
More file actions
60 lines (48 loc) · 1.53 KB
/
root.go
File metadata and controls
60 lines (48 loc) · 1.53 KB
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
/*
Copyright © 2023 ZheNing Hu <[email protected]>
*/
package cmd
import (
"os"
"strings"
log "github.com/sirupsen/logrus"
"github.com/spf13/cobra"
"github.com/spf13/viper"
"github.com/adlternative/tinygithub/pkg/config"
)
var configFile string
// rootCmd represents the base command when called without any subcommands
var rootCmd = &cobra.Command{
Use: "tinygithub",
Short: "a tiny github",
Long: `a tiny github which can support git code repo service`,
}
// Execute adds all child commands to the root command and sets flags appropriately.
// This is called by main.main(). It only needs to happen once to the rootCmd.
func Execute() {
err := rootCmd.Execute()
if err != nil {
os.Exit(1)
}
}
func init() {
rootCmd.Flags().BoolP("toggle", "t", false, "Help message for toggle")
rootCmd.PersistentFlags().String(config.LogLevel, "info", "log level")
rootCmd.PersistentFlags().String(config.LogFile, "", "log file")
rootCmd.PersistentFlags().String(config.GitBinPath, "/usr/bin/git", "git bin path")
rootCmd.PersistentFlags().StringVar(&configFile, "config", "config.json", "config file")
if err := viper.BindPFlags(rootCmd.PersistentFlags()); err != nil {
log.Fatalf("viper bind flags failed with %v", err)
}
cobra.OnInitialize(initConfig)
}
func initConfig() {
viper.SetConfigFile(configFile)
viper.AutomaticEnv()
viper.SetEnvKeyReplacer(strings.NewReplacer(".", "_", "-", "_"))
if err := viper.ReadInConfig(); err != nil {
log.Info("no config file specified")
}
config.InitLog()
config.InitGitBinPath()
}