-
Notifications
You must be signed in to change notification settings - Fork 46
/
root.go
64 lines (54 loc) · 1.53 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
package cmd
import (
"fmt"
"github.com/spf13/cobra"
"github.com/infraboard/mcube/v2/ioc"
"github.com/infraboard/mcube/v2/ioc/config/application"
"github.com/infraboard/mcube/v2/ioc/server"
)
var (
confType string
confFile string
vers bool
)
// Root represents the base command when called without any subcommands
var Root = &cobra.Command{
RunE: func(cmd *cobra.Command, args []string) error {
if vers {
fmt.Println(application.FullVersion())
return nil
}
return cmd.Help()
},
}
// 初始化Ioc
func initail() error {
req := server.DefaultConfig
switch confType {
case "file":
req.ConfigFile.Enabled = true
req.ConfigFile.Path = confFile
default:
req.ConfigEnv.Enabled = true
}
// 初始化ioc
if err := ioc.ConfigIocObject(server.DefaultConfig); err != nil {
return err
}
// 补充Root命令信息
Root.Use = application.Get().AppName
Root.Short = application.Get().AppDescription
Root.Long = application.Get().AppDescription
return nil
}
// Execute adds all child commands to the root command sets flags appropriately.
// This is called by main.main(). It only needs to happen once to the rootCmd.
func Execute() {
cobra.CheckErr(initail())
cobra.CheckErr(Root.Execute())
}
func init() {
Root.PersistentFlags().StringVarP(&confType, "config-type", "t", "file", "the service config type [file/env]")
Root.PersistentFlags().StringVarP(&confFile, "config-file", "f", "etc/application.toml", "the service config from file")
Root.PersistentFlags().BoolVarP(&vers, "version", "v", false, "the mcenter version")
}