package luaInGo
|
|
|
|
import (
|
|
"fmt"
|
|
"io/ioutil"
|
|
"os"
|
|
"path"
|
|
)
|
|
|
|
// LuaFile lua文件
|
|
type LuaFile struct {
|
|
FileName string
|
|
}
|
|
|
|
// LuaFileExist 判断某lua脚本是否存在
|
|
func LuaFileExist(fileName string) bool {
|
|
fileList := GetAllLuaFile()
|
|
for _, file := range fileList {
|
|
if file.FileName == fileName {
|
|
return true
|
|
}
|
|
}
|
|
return false
|
|
}
|
|
|
|
// GetAllLuaFile 获取所有文件
|
|
func GetAllLuaFile() []LuaFile {
|
|
// 声明一个文件列表切片
|
|
var fileList []LuaFile
|
|
rd, err := ioutil.ReadDir("luafile")
|
|
if err != nil {
|
|
fmt.Println("GetAllLuaFile error", err)
|
|
return fileList
|
|
}
|
|
|
|
for _, fi := range rd {
|
|
fileList = append(fileList, LuaFile{fi.Name()})
|
|
}
|
|
return fileList
|
|
}
|
|
|
|
// DeleteLuaFile 删除文件
|
|
func DeleteLuaFile(fileName string) bool {
|
|
err := os.Remove("luafile/" + fileName)
|
|
if err != nil {
|
|
fmt.Println("DeleteLuaFile error", err)
|
|
return false
|
|
}
|
|
return true
|
|
}
|
|
|
|
func ListLuaFile(folder string, luas []string) {
|
|
files, _ := ioutil.ReadDir(folder)
|
|
for _, file := range files {
|
|
if file.IsDir() {
|
|
ListLuaFile(folder+"/"+file.Name(), luas)
|
|
} else {
|
|
var filenameWithSuffix string = path.Base(file.Name()) //获取文件名带后缀
|
|
var fileSuffix string = path.Ext(filenameWithSuffix) //获取文件后缀
|
|
if fileSuffix == ".lua" {
|
|
filePath := fmt.Sprintf("%s%s%s", folder, "/", file.Name())
|
|
luas = append(luas, filePath)
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
func GetFileModtime(file string) (int64, error) {
|
|
stat, err := os.Stat(file)
|
|
if err != nil {
|
|
fmt.Printf("stat fail, file=%v err=%v", file, err)
|
|
return 0, err
|
|
}
|
|
return stat.ModTime().Unix(), nil
|
|
}
|