package cmd

import (
	"fmt"
	"git.wanbits.cc/sin/flytalk/config"
	"git.wanbits.cc/sin/flytalk/models"
	"git.wanbits.cc/sin/flytalk/tools"
	"github.com/spf13/cobra"
	"io/ioutil"
	"os"
	"strings"
)

var installCmd = &cobra.Command{
	Use:   "install",
	Short: "example:flytalk install",
	Run: func(cmd *cobra.Command, args []string) {
		install()
	},
}

func install() {
	sqlFile := config.Dir + "flytalk.sql"
	isExit, _ := tools.IsFileExist(config.MysqlConf)
	dataExit, _ := tools.IsFileExist(sqlFile)
	if !isExit || !dataExit {
		fmt.Println("config/mysql.json 数据库配置文件或者数据库文件flytalk.sql不存在")
		os.Exit(1)
	}
	sqls, _ := ioutil.ReadFile(sqlFile)
	sqlArr := strings.Split(string(sqls), "|")
	for _, sql := range sqlArr {
		if sql == "" {
			continue
		}
		models.Execute(sql)
	}
}
