forked from YaoApp/yao
-
Notifications
You must be signed in to change notification settings - Fork 0
/
root.go
133 lines (122 loc) · 3.86 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
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
package cmd
import (
"fmt"
"os"
"path/filepath"
"github.com/spf13/cobra"
"github.com/yaoapp/kun/exception"
"github.com/yaoapp/yao/config"
"github.com/yaoapp/yao/share"
)
var appPath string
var envFile string
var lang = os.Getenv("YAO_LANG")
var langs = map[string]string{
"Start Engine": "启动象传应用引擎",
"One or more arguments are not correct": "参数错误",
"Application directory": "指定应用路径",
"Environment file": "指定环境变量文件",
"Help for yao": "显示命令帮助文档",
"Show app configure": "显示应用配置信息",
"Update database schema": "更新数据表结构",
"Execute process": "运行处理器",
"Show version": "显示当前版本号",
"Development mode": "使用开发模式启动",
"Enabled unstable features": "启用内测功能",
"Fatal: %s": "失败: %s",
"Service stopped": "服务已关闭",
"API": " API接口",
"API List": "API列表",
"Root": "应用目录",
"Frontend": "前台地址",
"Dashboard": "管理后台",
"Not enough arguments": "参数错误: 缺少参数",
"Run: %s": "运行: %s",
"Arguments: %s": "参数错误: %s",
"%s Response": "%s 返回结果",
"Update schema model: %s (%s) ": "更新表结构 model: %s (%s)",
"Model name": "模型名称",
"Initialize project": "项目初始化",
"✨DONE✨": "✨完成✨",
"NEXT:": "下一步:",
"Listening": " 监听",
"✨LISTENING✨": "✨服务正在运行✨",
"SessionPort": "会话服务端口",
"Force migrate": "强制更新数据表结构",
"Migrate is not allowed on production mode.": "Migrate 不能再生产环境下使用",
}
// L 多语言切换
func L(words string) string {
if lang == "" {
return words
}
if trans, has := langs[words]; has {
return trans
}
return words
}
var rootCmd = &cobra.Command{
Use: share.BUILDNAME,
Short: "Yao App Engine",
Long: `Yao App Engine`,
Args: cobra.MinimumNArgs(1),
CompletionOptions: cobra.CompletionOptions{
DisableDefaultCmd: true,
},
Run: func(cmd *cobra.Command, args []string) {
if len(args) > 0 {
switch args[0] {
case "fuxi":
fuxi()
}
}
fmt.Fprintln(os.Stderr, L("One or more arguments are not correct"), args)
os.Exit(1)
},
}
// 加载命令
func init() {
rootCmd.AddCommand(
versionCmd,
migrateCmd,
inspectCmd,
startCmd,
runCmd,
initCmd,
serviceCmd,
dumpCmd,
restoreCmd,
socketCmd,
)
// rootCmd.SetHelpCommand(helpCmd)
rootCmd.PersistentFlags().StringVarP(&appPath, "app", "a", "", L("Application directory"))
rootCmd.PersistentFlags().StringVarP(&envFile, "env", "e", "", L("Environment file"))
}
// Execute 运行Root
func Execute() {
if err := rootCmd.Execute(); err != nil {
fmt.Fprintln(os.Stderr, err)
os.Exit(1)
}
}
// Boot 设定配置
func Boot() {
root := config.Conf.Root
if appPath != "" {
r, err := filepath.Abs(appPath)
if err != nil {
exception.New("Root error %s", 500, err.Error()).Throw()
}
root = r
}
if envFile != "" {
config.Conf = config.LoadFrom(envFile)
} else {
config.Conf = config.LoadFrom(filepath.Join(root, ".env"))
}
if config.Conf.Mode == "production" {
config.Production()
} else if config.Conf.Mode == "development" {
config.Development()
}
}