owl-blogs/directories.go

46 lines
780 B
Go
Raw Normal View History

2022-08-03 14:55:48 +00:00
package owl
2022-07-20 17:33:22 +00:00
2022-07-23 15:19:47 +00:00
import (
"os"
2022-10-13 18:58:16 +00:00
"strings"
2022-07-23 15:19:47 +00:00
)
2022-07-20 17:33:22 +00:00
func dirExists(path string) bool {
_, err := os.Stat(path)
return err == nil
}
2022-07-20 17:35:31 +00:00
2022-07-23 15:19:47 +00:00
// lists all files/dirs in a directory, not recursive
2022-07-20 17:35:31 +00:00
func listDir(path string) []string {
dir, _ := os.Open(path)
defer dir.Close()
files, _ := dir.Readdirnames(-1)
return files
}
2022-07-23 15:19:47 +00:00
2022-08-03 16:03:10 +00:00
func fileExists(path string) bool {
_, err := os.Stat(path)
return err == nil
}
2022-10-13 18:58:16 +00:00
func toDirectoryName(name string) string {
name = strings.ToLower(strings.ReplaceAll(name, " ", "-"))
// remove all non-alphanumeric characters
name = strings.Map(func(r rune) rune {
if r >= 'a' && r <= 'z' {
return r
}
if r >= 'A' && r <= 'Z' {
return r
}
if r >= '0' && r <= '9' {
return r
}
if r == '-' {
return r
}
return -1
}, name)
return name
}