2022-10-12 11:49:28 +08:00
|
|
|
package cli
|
|
|
|
|
|
|
|
import (
|
2022-11-10 12:13:42 +08:00
|
|
|
"fmt"
|
|
|
|
|
2022-10-24 16:51:05 +08:00
|
|
|
"github.com/answerdev/answer/internal/base/data"
|
2022-11-03 20:09:04 +08:00
|
|
|
"github.com/answerdev/answer/internal/entity"
|
2022-10-24 16:51:05 +08:00
|
|
|
"github.com/answerdev/answer/pkg/dir"
|
2022-10-12 11:49:28 +08:00
|
|
|
)
|
|
|
|
|
2022-10-12 20:20:26 +08:00
|
|
|
func CheckConfigFile(configPath string) bool {
|
2022-10-24 20:26:00 +08:00
|
|
|
return dir.CheckFileExist(configPath)
|
2022-10-12 11:49:28 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
func CheckUploadDir() bool {
|
2022-10-24 20:26:00 +08:00
|
|
|
return dir.CheckDirExist(UploadFilePath)
|
2022-10-12 11:49:28 +08:00
|
|
|
}
|
|
|
|
|
2022-11-10 14:13:29 +08:00
|
|
|
// CheckDBConnection check database whether the connection is normal
|
|
|
|
func CheckDBConnection(dataConf *data.Database) bool {
|
2022-10-12 11:49:28 +08:00
|
|
|
db, err := data.NewDB(false, dataConf)
|
|
|
|
if err != nil {
|
2022-11-10 12:13:42 +08:00
|
|
|
fmt.Printf("connection database failed: %s\n", err)
|
2022-10-12 11:49:28 +08:00
|
|
|
return false
|
|
|
|
}
|
|
|
|
if err = db.Ping(); err != nil {
|
2022-11-10 12:13:42 +08:00
|
|
|
fmt.Printf("connection ping database failed: %s\n", err)
|
2022-10-12 11:49:28 +08:00
|
|
|
return false
|
|
|
|
}
|
2022-11-10 14:13:29 +08:00
|
|
|
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
|
|
|
|
// CheckDBTableExist check database whether the table is already exists
|
|
|
|
func CheckDBTableExist(dataConf *data.Database) bool {
|
|
|
|
db, err := data.NewDB(false, dataConf)
|
|
|
|
if err != nil {
|
|
|
|
fmt.Printf("connection database failed: %s\n", err)
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
if err = db.Ping(); err != nil {
|
|
|
|
fmt.Printf("connection ping database failed: %s\n", err)
|
|
|
|
return false
|
2022-11-03 20:09:04 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
exist, err := db.IsTableExist(&entity.Version{})
|
|
|
|
if err != nil {
|
2022-11-10 12:13:42 +08:00
|
|
|
fmt.Printf("check table exist failed: %s\n", err)
|
2022-11-03 20:09:04 +08:00
|
|
|
return false
|
|
|
|
}
|
|
|
|
if !exist {
|
2022-11-10 12:13:42 +08:00
|
|
|
fmt.Printf("check table not exist\n")
|
2022-11-03 20:09:04 +08:00
|
|
|
return false
|
|
|
|
}
|
2022-10-12 11:49:28 +08:00
|
|
|
return true
|
|
|
|
}
|