forked from YaoApp/yao
-
Notifications
You must be signed in to change notification settings - Fork 0
/
root.go
66 lines (56 loc) · 1.23 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
package cmd
import (
"fmt"
"os"
"path/filepath"
"github.com/spf13/cobra"
"github.com/yaoapp/xiang/config"
)
var appPath string
var envFile string
var rootCmd = &cobra.Command{
Use: "xiang",
Short: "象传应用引擎命令行工具",
Long: `象传应用引擎命令行工具`,
Args: cobra.MinimumNArgs(1),
CompletionOptions: cobra.CompletionOptions{
DisableDefaultCmd: true,
},
Run: func(cmd *cobra.Command, args []string) {
fmt.Fprintln(os.Stderr, "参数错误", args)
os.Exit(1)
},
}
// 加载命令
func init() {
rootCmd.AddCommand(
versionCmd,
domainCmd,
migrateCmd,
inspectCmd,
startCmd,
)
rootCmd.SetHelpCommand(helpCmd)
rootCmd.PersistentFlags().StringVarP(&appPath, "app", "a", "", "指定应用目录")
rootCmd.PersistentFlags().StringVarP(&envFile, "env", "e", "", "指定环境变量文件")
}
// Execute 运行Root
func Execute() {
if err := rootCmd.Execute(); err != nil {
fmt.Fprintln(os.Stderr, err)
os.Exit(1)
}
}
// Boot 设定配置
func Boot() {
if envFile == "" && appPath != "" {
config.SetAppPath(appPath, filepath.Join(appPath, ".env"))
return
}
if envFile != "" { // 指定环境变量文件
config.SetEnvFile(envFile)
}
if appPath != "" {
config.SetAppPath(appPath)
}
}