From 7917f1fcaea2ad67a04044b04e2fc0191c2cea7c Mon Sep 17 00:00:00 2001 From: Max Date: Tue, 14 Sep 2021 12:57:03 +0800 Subject: [PATCH] =?UTF-8?q?=E8=B0=83=E6=95=B4=E5=8D=95=E5=85=83=E6=B5=8B?= =?UTF-8?q?=E8=AF=95?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- Makefile | 2 +- cmd/help.go | 14 ++++++++++ cmd/root.go | 6 ++++ config_test.go => global/config_test.go | 5 ++-- global/init_test.go | 21 ++++++++++++++ load_test.go => global/load_test.go | 13 ++++----- main.go | 37 ------------------------- 7 files changed, 50 insertions(+), 48 deletions(-) create mode 100644 cmd/help.go rename config_test.go => global/config_test.go (95%) create mode 100644 global/init_test.go rename load_test.go => global/load_test.go (62%) diff --git a/Makefile b/Makefile index 6c9d545e..2147799b 100644 --- a/Makefile +++ b/Makefile @@ -5,7 +5,7 @@ VETPACKAGES ?= $(shell $(GO) list ./... | grep -v /examples/) GOFILES := $(shell find . -name "*.go") # ROOT_DIR := $(shell dirname $(realpath $(firstword $(MAKEFILE_LIST)))) -TESTFOLDER := $(shell $(GO) list ./... | grep -E './xiang$$' | grep -v examples) +TESTFOLDER := $(shell $(GO) list ./... | grep -E 'xiang$$|global$$' | grep -v examples) TESTTAGS ?= "" # 运行单元测试 diff --git a/cmd/help.go b/cmd/help.go new file mode 100644 index 00000000..1cdf1da2 --- /dev/null +++ b/cmd/help.go @@ -0,0 +1,14 @@ +package cmd + +import ( + "github.com/spf13/cobra" +) + +var helpCmd = &cobra.Command{ + Use: "help", + Short: "显示命令帮助文档", + Long: `显示命令帮助文档`, + Run: func(cmd *cobra.Command, args []string) { + cmd.Help() + }, +} diff --git a/cmd/root.go b/cmd/root.go index 986e2d12..1d45eed1 100644 --- a/cmd/root.go +++ b/cmd/root.go @@ -12,6 +12,9 @@ var rootCmd = &cobra.Command{ 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) @@ -27,6 +30,9 @@ func init() { infoCmd, startCmd, ) + + rootCmd.SetHelpCommand(helpCmd) + } // Execute 运行Root diff --git a/config_test.go b/global/config_test.go similarity index 95% rename from config_test.go rename to global/config_test.go index a247dc95..cc608233 100644 --- a/config_test.go +++ b/global/config_test.go @@ -1,4 +1,4 @@ -package main +package global import ( "os" @@ -7,11 +7,10 @@ import ( "github.com/stretchr/testify/assert" "github.com/yaoapp/kun/any" - "github.com/yaoapp/xiang/global" ) func TestNewConfig(t *testing.T) { - cfg := global.NewConfig() + cfg := NewConfig() var vBool = func(name string) bool { if name == "true" || name == "1" { return true diff --git a/global/init_test.go b/global/init_test.go new file mode 100644 index 00000000..f0aec264 --- /dev/null +++ b/global/init_test.go @@ -0,0 +1,21 @@ +package global + +import ( + "os" + "testing" + + "github.com/yaoapp/gou" +) + +var cfg Config + +func TestMain(m *testing.M) { + + // Run test suites + exitVal := m.Run() + + // we can do clean up code here + gou.KillPlugins() + + os.Exit(exitVal) +} diff --git a/load_test.go b/global/load_test.go similarity index 62% rename from load_test.go rename to global/load_test.go index 5ac03dba..9907e239 100644 --- a/load_test.go +++ b/global/load_test.go @@ -1,24 +1,23 @@ -package main +package global import ( "path" "testing" "github.com/stretchr/testify/assert" - "github.com/yaoapp/xiang/global" ) func TestLoad(t *testing.T) { assert.NotPanics(t, func() { - global.Load(global.Conf) + Load(Conf) }) } // 从文件系统载入引擎文件 func TestLoadEngineFS(t *testing.T) { - root := "fs://" + path.Join(global.Conf.Source, "/xiang") + root := "fs://" + path.Join(Conf.Source, "/xiang") assert.NotPanics(t, func() { - global.LoadEngine(root) + LoadEngine(root) }) } @@ -27,13 +26,13 @@ func TestLoadEngineFS(t *testing.T) { func TestLoadEngineBin(t *testing.T) { root := "bin://xiang" assert.NotPanics(t, func() { - global.LoadEngine(root) + LoadEngine(root) }) } // 从文件系统载入应用脚本 func TestLoadAppFS(t *testing.T) { assert.NotPanics(t, func() { - global.LoadApp(global.Conf.RootAPI, global.Conf.RootFLow, global.Conf.RootModel, global.Conf.RootPlugin) + LoadApp(Conf.RootAPI, Conf.RootFLow, Conf.RootModel, Conf.RootPlugin) }) } diff --git a/main.go b/main.go index a6c179fd..6a6956a5 100644 --- a/main.go +++ b/main.go @@ -1,47 +1,10 @@ package main import ( - "github.com/yaoapp/gou" - "github.com/yaoapp/kun/utils" "github.com/yaoapp/xiang/cmd" ) // 主程序 func main() { cmd.Execute() - - // fmt.Printf("象传应用引擎 %s %s\n", VERSION, DOMAIN) - // cfg := NewConfig() - - // // 加载脚本 - // capsule.AddConn("primary", "mysql", cfg.Database.Primary[0]).SetAsGlobal() - - // // 加密密钥 - // gou.LoadCrypt(fmt.Sprintf(`{"key":"%s"}`, cfg.Database.AESKey), "AES") - // gou.LoadCrypt(`{}`, "PASSWORD") - - // // 加载模型 - // Load(cfg) - - // // 启动服务 - // for _, api := range gou.APIs { - // for _, p := range api.HTTP.Paths { - // utils.Dump(api.Name + ":" + p.Path) - // } - - // } - // gou.ServeHTTP(gou.Server{ - // Host: cfg.Service.Host, - // Port: cfg.Service.Port, - // Allows: cfg.Service.Allow, - // Root: "/api", - // }) -} - -// Migrate 数据迁移 -func Migrate() { - for name, mod := range gou.Models { - utils.Dump(name) - mod.Migrate(true) - } }