owl-blogs/directories.go

63 lines
1.1 KiB
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"
"path/filepath"
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-07-23 15:19:47 +00:00
// recursive list of all files in a directory
func walkDir(path string) []string {
files := make([]string, 0)
filepath.Walk(path, func(subPath string, info os.FileInfo, err error) error {
if err != nil {
return err
}
if info.IsDir() {
return nil
}
files = append(files, subPath[len(path)+1:])
return nil
})
return files
}
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
}