Skip to content

Commit

Permalink
命令行工具 + 多语言支持
Browse files Browse the repository at this point in the history
  • Loading branch information
trheyi committed Feb 9, 2022
1 parent 85eeab8 commit 95a0d1e
Show file tree
Hide file tree
Showing 9 changed files with 112 additions and 36 deletions.
8 changes: 8 additions & 0 deletions app/app.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,14 @@ func Load(cfg config.Config) {
}
}

// L 语言包
func L(word string) string {
if trans, has := share.App.L[word]; has {
return trans
}
return word
}

// Init 应用初始化
func Init(cfg config.Config) {

Expand Down
4 changes: 2 additions & 2 deletions cmd/help.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,8 @@ import (

var helpCmd = &cobra.Command{
Use: "help",
Short: "显示命令帮助文档",
Long: `显示命令帮助文档`,
Short: L("Help for yao"),
Long: L("Help for yao"),
Run: func(cmd *cobra.Command, args []string) {
cmd.Help()
},
Expand Down
4 changes: 2 additions & 2 deletions cmd/inspect.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,8 @@ import (

var inspectCmd = &cobra.Command{
Use: "inspect",
Short: "显示当前配置信息",
Long: `显示当前配置信息`,
Short: L("Show app configure"),
Long: L("Show app configure"),
Run: func(cmd *cobra.Command, args []string) {
Boot()
res := maps.Map{
Expand Down
4 changes: 2 additions & 2 deletions cmd/migrate.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,8 @@ import (
var name string
var migrateCmd = &cobra.Command{
Use: "migrate",
Short: "更新数据结构",
Long: `更新数据库结构`,
Short: L("Update database schema"),
Long: L("Update database schema"),
Run: func(cmd *cobra.Command, args []string) {
Boot()
// 加载数据模型
Expand Down
38 changes: 32 additions & 6 deletions cmd/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,21 +7,47 @@ import (

"github.com/spf13/cobra"
"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": "显示当前版本号",
}

// 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: "xiang",
Short: "象传应用引擎命令行工具",
Long: `象传应用引擎命令行工具`,
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) {
fmt.Fprintln(os.Stderr, "参数错误", args)
fmt.Fprintln(os.Stderr, L("One or more arguments are not correct"), args)
os.Exit(1)
},
}
Expand All @@ -36,8 +62,8 @@ func init() {
runCmd,
)
rootCmd.SetHelpCommand(helpCmd)
rootCmd.PersistentFlags().StringVarP(&appPath, "app", "a", "", "指定应用目录")
rootCmd.PersistentFlags().StringVarP(&envFile, "env", "e", "", "指定环境变量文件")
rootCmd.PersistentFlags().StringVarP(&appPath, "app", "a", "", L("Application directory"))
rootCmd.PersistentFlags().StringVarP(&envFile, "env", "e", "", L("Environment file"))
}

// Execute 运行Root
Expand Down
4 changes: 2 additions & 2 deletions cmd/run.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,8 @@ import (

var runCmd = &cobra.Command{
Use: "run",
Short: "运行处理器",
Long: `运行处理器`,
Short: L("Execute process"),
Long: L("Execute process"),
Run: func(cmd *cobra.Command, args []string) {
defer gou.KillPlugins()
Boot()
Expand Down
14 changes: 9 additions & 5 deletions cmd/start.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package cmd

import (
"fmt"
"os"
"path/filepath"
"strings"

Expand All @@ -16,8 +17,8 @@ import (

var startCmd = &cobra.Command{
Use: "start",
Short: "启动象传应用引擎",
Long: `启动象传应用引擎`,
Short: L("Start Engine"),
Long: L("Start Engine"),
Run: func(cmd *cobra.Command, args []string) {
defer service.Stop(func() { fmt.Println("服务已关闭") })
Boot()
Expand All @@ -28,11 +29,14 @@ var startCmd = &cobra.Command{
mode = "\n"
}

fmt.Printf(color.GreenString("象传应用引擎 v%s %s", share.VERSION, mode))

engine.Load(config.Conf) // 加载数据模型 API 等
err := engine.Load(config.Conf) // 加载脚本等
if err != nil {
fmt.Printf(color.RedString("启动失败: %s\n", err.Error()))
os.Exit(1)
}

// 打印应用目录信息
fmt.Printf(color.GreenString("象传应用引擎 v%s %s", share.VERSION, mode))
fmt.Printf(color.WhiteString("\n---------------------------------"))
fmt.Printf(color.GreenString("\n应用名称: %s v%s", share.App.Name, share.App.Version))
fmt.Printf(color.GreenString("\n应用根目录: %s", config.Conf.Root))
Expand Down
4 changes: 2 additions & 2 deletions cmd/version.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,8 @@ import (

var versionCmd = &cobra.Command{
Use: "version",
Short: "显示当前版本号",
Long: `显示当前版本号`,
Short: L("Show version"),
Long: L("Show version"),
Run: func(cmd *cobra.Command, args []string) {
// Do Stuff Here
fmt.Println(share.VERSION)
Expand Down
68 changes: 53 additions & 15 deletions engine/load.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,38 +25,76 @@ import (
)

// Load 根据配置加载 API, FLow, Model, Plugin
func Load(cfg config.Config) {
func Load(cfg config.Config) (err error) {
defer func() { err = exception.Catch(recover()) }()

// 加载应用信息
// 第一步: 加载应用信息
app.Load(cfg)

// 第二步: 建立数据库 & 会话连接
share.DBConnect(cfg.DB) // 创建数据库连接
share.SessionConnect(cfg.Session) // 创建会话服务器链接

app.Load(cfg) // 加载应用信息

// 加载应用引擎
if os.Getenv("YAO_DEV") != "" {
LoadEngine(filepath.Join(os.Getenv("YAO_DEV"), "/xiang"))
} else {
LoadEngine()
}

// 第三步: 加载数据分析引擎
query.Load(cfg) // 加载数据分析引擎

share.Load(cfg) // 加载共享库 lib
script.Load(cfg) // 加载JS处理器 script
model.Load(cfg) // 加载数据模型 model
flow.Load(cfg) // 加载业务逻辑 Flow
plugin.Load(cfg) // 加载业务插件 plugin
table.Load(cfg) // 加载数据表格 table
chart.Load(cfg) // 加载分析图表 chart
page.Load(cfg) // 加载页面 page
// 第四步: 加载共享库 & JS 处理器
err = share.Load(cfg) // 加载共享库 lib
if err != nil {
return err
}
err = script.Load(cfg) // 加载JS处理器 script
if err != nil {
return err
}

// 第五步: 加载数据模型等
err = model.Load(cfg) // 加载数据模型 model
if err != nil {
return err
}
err = flow.Load(cfg) // 加载业务逻辑 Flow
if err != nil {
return err
}
err = plugin.Load(cfg) // 加载业务插件 plugin
if err != nil {
return err
}
err = table.Load(cfg) // 加载数据表格 table
if err != nil {
return err
}
err = chart.Load(cfg) // 加载分析图表 chart
if err != nil {
return err
}
err = page.Load(cfg) // 加载页面 page
if err != nil {
return err
}

importer.Load(cfg) // 加载数据导入 imports
workflow.Load(cfg) // 加载工作流 workflow
err = api.Load(cfg) // 加载业务接口 API
if err != nil {
return err
}

importer.Load(cfg) // 加载数据导入 imports
workflow.Load(cfg) // 加载工作流 workflow
api.Load(cfg) // 加载业务接口 API
server.Load(cfg) // 加载服务
server.Load(cfg) // 加载服务

// 加密密钥函数
gou.LoadCrypt(fmt.Sprintf(`{"key":"%s"}`, cfg.DB.AESKey), "AES")
gou.LoadCrypt(`{}`, "PASSWORD")
return nil
}

// Reload 根据配置重新加载 API, FLow, Model, Plugin
Expand Down

0 comments on commit 95a0d1e

Please sign in to comment.