excel-export/pkg/path.go

62 lines
1.2 KiB
Go

package pkg
import (
"fmt"
"os"
"path"
"regexp"
"runtime"
)
func GetRootDir() string {
_, filename, _, _ := runtime.Caller(0)
return fmt.Sprintf("%s/", path.Dir(path.Dir(filename)))
}
func GetPath(path string, pathType string) string {
directory := fmt.Sprintf("%s/%s/", path, pathType)
err := CheckOrCreatePathOrFile(directory)
if err != nil {
panic(err)
}
return directory
}
func MissionPathFile(taskIdStr string) (string, error) {
pathDir, _ := os.Getwd()
return fmt.Sprintf("%s/%s/%s", pathDir, "zip", taskIdStr), nil
}
func CheckOrCreatePathOrFile(directoryOrFile string) error {
_, err := os.Stat(GetRootDir() + directoryOrFile)
if err != nil {
if os.IsNotExist(err) {
//判断是文件还是文件夹
filePattern := `^.*\.[^\\/]*$`
fileRe := regexp.MustCompile(filePattern)
isFile := fileRe.MatchString(directoryOrFile)
if isFile {
file, err := os.Create(directoryOrFile)
if err != nil {
return err
}
defer file.Close()
} else {
err = os.MkdirAll(directoryOrFile, 0755)
if err != nil {
return err
}
}
} else {
return err
}
}
return nil
}
type TreeList struct {
Path string `json:"path"`
File []string `json:"file"`
Children []*TreeList `json:"children"`
}