diff --git a/cmd/install.go b/cmd/install.go new file mode 100644 index 0000000..92d4f3b --- /dev/null +++ b/cmd/install.go @@ -0,0 +1,37 @@ +package cmd + +import ( + "fmt" + "github.com/spf13/cobra" + "github.com/taoshihan1991/imaptool/config" + "github.com/taoshihan1991/imaptool/models" + "github.com/taoshihan1991/imaptool/tools" + "io/ioutil" + "os" + "strings" +) + +var installCmd = &cobra.Command{ + Use: "install", + Short: "example:go-fly install", + Run: func(cmd *cobra.Command, args []string) { + install() + }, +} +func install(){ + sqlFile:=config.Dir+"go-fly.sql" + isExit,_:=tools.IsFileExist(config.MysqlConf) + dataExit,_:=tools.IsFileExist(sqlFile) + if !isExit||!dataExit{ + fmt.Println("config/mysql.json 数据库配置文件或者数据库文件go-fly.sql不存在") + os.Exit(1) + } + sqls,_:=ioutil.ReadFile(sqlFile) + sqlArr:=strings.Split(string(sqls),";") + for _,sql:=range sqlArr{ + if sql==""{ + continue + } + models.Execute(sql) + } +} \ No newline at end of file diff --git a/cmd/root.go b/cmd/root.go index 07ea804..2d8bcb1 100644 --- a/cmd/root.go +++ b/cmd/root.go @@ -30,4 +30,5 @@ func Execute() { func init() { rootCmd.AddCommand(versionCmd) rootCmd.AddCommand(serverCmd) + rootCmd.AddCommand(installCmd) } \ No newline at end of file diff --git a/cmd/version.go b/cmd/version.go index 92c36bb..c7eb13e 100644 --- a/cmd/version.go +++ b/cmd/version.go @@ -8,7 +8,7 @@ import ( var versionCmd = &cobra.Command{ Use: "version", - Short: "Print the version number of go-fly", + Short: "example:go-fly version", Run: func(cmd *cobra.Command, args []string) { fmt.Println("go-fly "+config.Version) }, diff --git a/models/models.go b/models/models.go index 4a842a0..faa0f0a 100644 --- a/models/models.go +++ b/models/models.go @@ -22,6 +22,9 @@ func init(){ DB.DB().SetMaxIdleConns(10) DB.DB().SetMaxOpenConns(100) } +func Execute(sql string){ + DB.Exec(sql) +} func CloseDB() { defer DB.Close() } \ No newline at end of file diff --git a/readme.md b/readme.md index d3b8ba7..e797241 100644 --- a/readme.md +++ b/readme.md @@ -65,6 +65,8 @@ 4. 源码打包 go build go-fly.go +5. 导入数据库(会删除表清空数据) go run go-fly.go install + ### nginx部署 访问:https://gofly.sopans.com