install.go 1.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768
  1. package cmd
  2. import (
  3. "fmt"
  4. "git.wanbits.cc/sin/flytalk/config"
  5. "git.wanbits.cc/sin/flytalk/models"
  6. "git.wanbits.cc/sin/flytalk/tools"
  7. "github.com/spf13/cobra"
  8. "io/ioutil"
  9. "os"
  10. "path/filepath"
  11. "strings"
  12. )
  13. var installCmd = &cobra.Command{
  14. Use: "install",
  15. Short: "install database specified in config file",
  16. Run: func(cmd *cobra.Command, args []string) {
  17. install()
  18. },
  19. }
  20. func install() {
  21. _, err := config.LoadConf(Confile)
  22. if err != nil {
  23. panic(err)
  24. }
  25. models.InitDb()
  26. sqlFile := filepath.Join(config.C.Basic.DataDir, "flytalk.sql")
  27. fileExists, _ := tools.IsFileExist(sqlFile)
  28. if !fileExists {
  29. fmt.Printf("sql-file not exists:%v\n", sqlFile)
  30. os.Exit(1)
  31. }
  32. sqls, _ := ioutil.ReadFile(sqlFile)
  33. sqlArr := strings.Split(string(sqls), "|")
  34. for _, sql := range sqlArr {
  35. if sql == "" {
  36. continue
  37. }
  38. models.Execute(sql)
  39. }
  40. }
  41. // gorm 的实现功能不强, 很难通过映射的方式去完整的还原表结构
  42. //func installByGorm() {
  43. // _, err := config.LoadConf(Confile)
  44. // if err != nil {
  45. // panic(err)
  46. // }
  47. // models.InitDb()
  48. // models.DB.Set("gorm:table_options", "ENGINE=InnoDB").AutoMigrate(
  49. // &models.Message{},
  50. // &models.User{},
  51. // &models.Visitor{},
  52. // &models.Role{},
  53. // &models.User_role{},
  54. //
  55. // &models.About{},
  56. // &models.Config{},
  57. // &models.Ipblack{},
  58. // &models.Welcome{},
  59. // )
  60. // if models.DB.Error != nil {
  61. // panic(models.DB.Error)
  62. // }
  63. //}