2023-02-01 13:31:46 +00:00
|
|
|
package osutils
|
|
|
|
|
|
|
|
import (
|
2023-02-04 11:13:00 +00:00
|
|
|
"io/fs"
|
|
|
|
"io/ioutil"
|
2023-02-01 13:31:46 +00:00
|
|
|
"log"
|
|
|
|
"os"
|
2023-02-04 11:13:00 +00:00
|
|
|
"path"
|
2023-02-01 13:31:46 +00:00
|
|
|
"path/filepath"
|
2023-02-04 11:13:00 +00:00
|
|
|
"strings"
|
2023-02-01 13:31:46 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
func IsDirectory(path string) (bool, error) {
|
|
|
|
fileInfo, err := os.Stat(path)
|
|
|
|
if err != nil {
|
|
|
|
return false, err
|
|
|
|
}
|
|
|
|
|
|
|
|
return fileInfo.IsDir(), err
|
|
|
|
}
|
|
|
|
|
|
|
|
func RecursiceWalk(pathToWalk string) ([]string, error) {
|
|
|
|
paths := []string{}
|
|
|
|
err := filepath.Walk(pathToWalk,
|
|
|
|
func(path string, info os.FileInfo, err error) error {
|
|
|
|
if err != nil {
|
|
|
|
println(err)
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
// fmt.Println(path[len(path)-4:])
|
|
|
|
libBool, err := IsDirectory(path)
|
|
|
|
if err != nil {
|
|
|
|
println("error in checking lib or dir")
|
|
|
|
}
|
|
|
|
if !libBool {
|
|
|
|
// println(path)
|
|
|
|
paths = append(paths, path)
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
})
|
|
|
|
if err != nil {
|
|
|
|
log.Println(err)
|
|
|
|
}
|
|
|
|
return paths, nil
|
|
|
|
}
|
2023-02-04 11:13:00 +00:00
|
|
|
|
|
|
|
func ListDir(path string) ([]fs.FileInfo, error) {
|
|
|
|
files, err := ioutil.ReadDir(path)
|
|
|
|
if err != nil {
|
|
|
|
log.Fatal(err)
|
|
|
|
return []fs.FileInfo{}, err
|
|
|
|
}
|
|
|
|
return files, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func GetParentDir(filePath string) (string, error) {
|
|
|
|
cuttedPath := strings.Split(filePath, "/")
|
|
|
|
cuttedPath = cuttedPath[:len(cuttedPath)-1]
|
2023-02-04 17:47:42 +00:00
|
|
|
finalPath := path.Join("/", path.Join(cuttedPath...))
|
|
|
|
return finalPath, nil
|
2023-02-04 11:13:00 +00:00
|
|
|
}
|