package tools import ( "errors" "fmt" "io/fs" "os" "path" "path/filepath" "strconv" ) // exported function(s) func ScanDirectory(source string, extensions ...string) ([]string, error) { var files []string if IsEmpty(source) { return files, errors.New("no source directory specified") } stats, error := os.Stat(source) if error != nil || !stats.IsDir() { return files, error } results, error := os.ReadDir(source) if error != nil { return files, error } for _, file := range results { fileName := file.Name() filePath := filepath.Join(source, fileName) if file.IsDir() { subFiles, error := ScanDirectory(filePath, extensions...) for _, subFile := range subFiles { files = append(files, subFile) } if error != nil { return files, error } continue } if ContainsString(extensions, filepath.Ext("."+fileName)) { files = append(files, filePath) continue } } return files, nil } func MkDirs(destination string) error { if IsEmpty(destination) { return nil } destination = filepath.Clean(destination) stats, error := os.Stat(destination) if error != nil && !errors.Is(error, os.ErrNotExist) { return error } if stats != nil && stats.IsDir() { return nil } return os.MkdirAll(destination, os.ModePerm) } func GetSubDirectories(directory string) []string { var folders []string filepath.WalkDir(directory, func(path string, info fs.DirEntry, err error) error { if err != nil { return err } if !info.IsDir() { return nil } folders = append(folders, path) return nil }) return folders } func CopyFile(source string, destination string) error { if IsEmpty(source) || IsEmpty(destination) { return nil } stats, error := os.Stat(source) if error != nil { return error } source = filepath.Clean(source) destination = filepath.Clean(destination) error = MkDirs(filepath.Dir(destination)) if error != nil { return error } bytes, error := os.ReadFile(source) if error != nil { return error } return os.WriteFile(destination, bytes, stats.Mode()) } func MoveFile(source string, destination string) error { if IsEmpty(source) || IsEmpty(destination) { return nil } source = filepath.Clean(source) destination = filepath.Clean(destination) error := MkDirs(filepath.Dir(destination)) if error != nil { return error } error = os.Rename(source, destination) if error == nil { return nil } error = CopyFile(source, destination) if error != nil { return error } return os.Remove(source) } func CreateFile(file string) error { if Exists(file) { return nil } error := MkDirs(path.Dir(file)) if error != nil { return error } _, error = os.Create(file) if error != nil { return error } return nil } func Delete(source string) error { return os.Remove(source) } func DeleteForced(source string) error { return os.RemoveAll((source)) } func DeleteEmpty(source string) []error { var errors []error directories := GetSubDirectories(source) for index := len(directories) - 1; index > 0; index-- { directory := directories[index] error := Delete(directory) if error != nil { errors = append(errors, error) } } if len(errors) > 0 { return errors } return nil } func Exists(path string) bool { _, error := os.Stat(path) return error == nil } // unexported function(s) func getSubDirectories(directory string, folders *[]string) []string { if IsEmpty(directory) { return *folders } directory = filepath.Clean(directory) stats, error := os.Stat(directory) if error != nil { return *folders } test := stats.IsDir() fmt.Println(strconv.FormatBool(test)) if !stats.IsDir() { return *folders } files, error := os.ReadDir(directory) if error != nil { return *folders } for _, tmp := range files { if !tmp.IsDir() { continue } if tmp.IsDir() { path := directory + string(os.PathSeparator) + tmp.Name() *folders = append(*folders, path) getSubDirectories(path, folders) } } return *folders }