47 lines
799 B
Go
47 lines
799 B
Go
package path
|
|
|
|
import (
|
|
"os"
|
|
"path/filepath"
|
|
"strings"
|
|
|
|
"github.com/pkg/errors"
|
|
)
|
|
|
|
func GetSubDirs(root string) ([]string, error) {
|
|
fd, err := os.Open(root)
|
|
if err != nil {
|
|
return nil, errors.Wrapf(err, "open root directory: %s", root)
|
|
}
|
|
defer fd.Close()
|
|
|
|
entries, err := fd.Readdir(-1)
|
|
if err != nil {
|
|
return nil, errors.Wrapf(err, "read root directory: %s", root)
|
|
}
|
|
|
|
var paths []string
|
|
for _, entry := range entries {
|
|
if entry.IsDir() {
|
|
paths = append(paths, entry.Name())
|
|
}
|
|
}
|
|
|
|
return paths, nil
|
|
}
|
|
|
|
func DirExists(path string) bool {
|
|
st, err := os.Stat(path)
|
|
if err != nil {
|
|
return false
|
|
}
|
|
return st.IsDir()
|
|
}
|
|
|
|
func SplitNameExt(name string) (string, string) {
|
|
ext := filepath.Ext(name)
|
|
name = name[:len(name)-len(ext)]
|
|
|
|
return name, strings.TrimLeft(ext, ".")
|
|
}
|