import cycle not allowed
This commit is contained in:
+111
-6
@@ -2,10 +2,14 @@ package config
|
||||
|
||||
import (
|
||||
config "cutego/pkg/config/models"
|
||||
"cutego/pkg/logging"
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"gopkg.in/yaml.v2"
|
||||
"io/ioutil"
|
||||
"os"
|
||||
"runtime"
|
||||
"strconv"
|
||||
)
|
||||
|
||||
var (
|
||||
@@ -80,16 +84,117 @@ func readAppYmlFile(resourcePath string) {
|
||||
LoadYamlFile(applicationEnvFilePath, AppEnvConfig)
|
||||
}
|
||||
|
||||
// ExecuteTip 执行日志
|
||||
func ExecuteTip(message string) {
|
||||
fmt.Println(fmt.Sprintf("-------------------- %s --------------------", message))
|
||||
// IntToString int转string
|
||||
func IntToString(n int) string {
|
||||
return strconv.Itoa(n)
|
||||
}
|
||||
|
||||
// StringToInt string转int
|
||||
func StringToInt(s string) int {
|
||||
i, err := strconv.Atoi(s)
|
||||
if err != nil {
|
||||
panic(err)
|
||||
}
|
||||
return i
|
||||
}
|
||||
|
||||
// StringToInt64 string转int64
|
||||
func StringToInt64(s string) int64 {
|
||||
i, err := strconv.ParseInt(s, 10, 64)
|
||||
if err != nil {
|
||||
panic(err)
|
||||
}
|
||||
return i
|
||||
}
|
||||
|
||||
// Int64ToString int64转string
|
||||
func Int64ToString(n int64) string {
|
||||
return strconv.FormatInt(n, 10)
|
||||
}
|
||||
|
||||
func mapToBytes(data map[string]interface{}) []byte {
|
||||
bytes, _ := json.Marshal(data)
|
||||
return bytes
|
||||
}
|
||||
|
||||
// MapToStruct map转struct
|
||||
func MapToStruct(data map[string]interface{}, v interface{}) {
|
||||
_ = json.Unmarshal(mapToBytes(data), v)
|
||||
}
|
||||
|
||||
// GetDirPath 获取目录路径
|
||||
func GetDirPath(resType string) string {
|
||||
sysType := runtime.GOOS
|
||||
switch sysType {
|
||||
case "linux":
|
||||
if resType == "logging" {
|
||||
return AppCoreConfig.CuteGoConfig.File.Linux.Logs
|
||||
} else if resType == "avatar" {
|
||||
return AppCoreConfig.CuteGoConfig.File.Linux.Avatar
|
||||
} else if resType == "file" {
|
||||
return AppCoreConfig.CuteGoConfig.File.Linux.Path
|
||||
}
|
||||
break
|
||||
case "windows":
|
||||
if resType == "logging" {
|
||||
return AppCoreConfig.CuteGoConfig.File.Windows.Logs
|
||||
} else if resType == "avatar" {
|
||||
return AppCoreConfig.CuteGoConfig.File.Windows.Avatar
|
||||
} else if resType == "file" {
|
||||
return AppCoreConfig.CuteGoConfig.File.Windows.Path
|
||||
}
|
||||
break
|
||||
case "mac":
|
||||
if resType == "logging" {
|
||||
return AppCoreConfig.CuteGoConfig.File.Mac.Logs
|
||||
} else if resType == "avatar" {
|
||||
return AppCoreConfig.CuteGoConfig.File.Mac.Avatar
|
||||
} else if resType == "file" {
|
||||
return AppCoreConfig.CuteGoConfig.File.Mac.Path
|
||||
}
|
||||
break
|
||||
case "darwin":
|
||||
if resType == "logging" {
|
||||
return AppCoreConfig.CuteGoConfig.File.Mac.Logs
|
||||
} else if resType == "avatar" {
|
||||
return AppCoreConfig.CuteGoConfig.File.Mac.Avatar
|
||||
} else if resType == "file" {
|
||||
return AppCoreConfig.CuteGoConfig.File.Mac.Path
|
||||
}
|
||||
}
|
||||
return AppCoreConfig.CuteGoConfig.File.Linux.Logs
|
||||
}
|
||||
|
||||
// CreateAllDir 递归创建文件夹
|
||||
func CreateAllDir(filePath string) error {
|
||||
if !IsFileOrDirExist(filePath) {
|
||||
err := os.MkdirAll(filePath, os.ModePerm)
|
||||
if err != nil {
|
||||
fmt.Println("创建文件夹失败, error info:", err)
|
||||
return err
|
||||
}
|
||||
return err
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
// IsFileOrDirExist 判断所给路径文件/文件夹是否存在(返回true是存在)
|
||||
func IsFileOrDirExist(path string) bool {
|
||||
// os.Stat获取文件信息
|
||||
_, err := os.Stat(path)
|
||||
if err != nil {
|
||||
if os.IsExist(err) {
|
||||
return true
|
||||
}
|
||||
return false
|
||||
}
|
||||
return true
|
||||
}
|
||||
|
||||
func init() {
|
||||
// 资源文件所在的路径
|
||||
resourcePath := GetRootPath()
|
||||
ExecuteTip("初始化应用配置 start")
|
||||
logging.InfoLog("application init start...")
|
||||
readAppYmlFile(resourcePath)
|
||||
ExecuteTip("初始化应用配置 end")
|
||||
return
|
||||
logging.InfoLog("application init start...")
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user