WebUI/src/util/util.go
Theta-Dev 60034b4815
All checks were successful
continuous-integration/drone/push Build is passing
add server base
2022-02-01 23:08:45 +01:00

55 lines
1 KiB
Go

package util
import (
"fmt"
"os"
"strings"
)
func DoesFileExist(filepath string) bool {
_, err := os.Stat(filepath)
return !os.IsNotExist(err)
}
func CreateDirIfNotExists(dirpath string) error {
if _, err := os.Stat(dirpath); os.IsNotExist(err) {
createErr := os.MkdirAll(dirpath, 0o777)
if createErr != nil {
return createErr
}
}
return nil
}
func FindFile(explicitPath string, locations, endings []string) (string, error) {
if explicitPath != "" {
if !DoesFileExist(explicitPath) {
return "", fmt.Errorf("file %s not found", explicitPath)
}
return explicitPath, nil
}
notFound := []string{}
for _, f := range locations {
if endings != nil {
for _, t := range endings {
fpath := f + "." + t
if DoesFileExist(fpath) {
return fpath, nil
} else {
notFound = append(notFound, fpath)
}
}
} else {
if DoesFileExist(f) {
return f, nil
} else {
notFound = append(notFound, f)
}
}
}
return "", fmt.Errorf("none of the following files found: %s",
strings.Join(notFound, "; "))
}