2022-09-08 15:57:59 +00:00
|
|
|
/*
|
|
|
|
Copyright © 2022 Seednode <seednode@seedno.de>
|
|
|
|
*/
|
|
|
|
|
|
|
|
package cmd
|
|
|
|
|
|
|
|
import (
|
|
|
|
"math/rand"
|
|
|
|
"os"
|
|
|
|
"path/filepath"
|
|
|
|
"time"
|
|
|
|
)
|
|
|
|
|
2022-09-08 18:12:50 +00:00
|
|
|
func getFiles(path string) ([]string, error) {
|
|
|
|
var paths []string
|
2022-09-08 15:57:59 +00:00
|
|
|
|
2022-09-08 18:12:50 +00:00
|
|
|
err := filepath.WalkDir(path, func(p string, info os.DirEntry, err error) error {
|
|
|
|
if info.IsDir() && p != path {
|
|
|
|
return filepath.SkipDir
|
|
|
|
} else {
|
2022-09-08 20:30:51 +00:00
|
|
|
absolutePath, err := filepath.Abs(p)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
paths = append(paths, absolutePath)
|
2022-09-08 18:12:50 +00:00
|
|
|
}
|
2022-09-08 20:30:51 +00:00
|
|
|
|
2022-09-08 18:12:50 +00:00
|
|
|
return err
|
|
|
|
})
|
2022-09-08 15:57:59 +00:00
|
|
|
if err != nil {
|
2022-09-08 18:12:50 +00:00
|
|
|
return nil, err
|
2022-09-08 15:57:59 +00:00
|
|
|
}
|
|
|
|
|
2022-09-08 18:12:50 +00:00
|
|
|
return paths, nil
|
2022-09-08 15:57:59 +00:00
|
|
|
}
|
|
|
|
|
2022-09-08 17:12:58 +00:00
|
|
|
func getFilesRecursive(path string) ([]string, error) {
|
|
|
|
var paths []string
|
|
|
|
|
|
|
|
err := filepath.WalkDir(path, func(p string, info os.DirEntry, err error) error {
|
|
|
|
if !info.IsDir() {
|
2022-09-08 20:30:51 +00:00
|
|
|
absolutePath, err := filepath.Abs(p)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
paths = append(paths, absolutePath)
|
2022-09-08 17:12:58 +00:00
|
|
|
}
|
|
|
|
return err
|
|
|
|
})
|
2022-09-08 18:12:50 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2022-09-08 17:12:58 +00:00
|
|
|
|
2022-09-08 18:12:50 +00:00
|
|
|
return paths, nil
|
2022-09-08 17:12:58 +00:00
|
|
|
}
|
|
|
|
|
2022-09-08 18:12:50 +00:00
|
|
|
func getFileList(args []string) ([]string, error) {
|
2022-09-08 17:12:58 +00:00
|
|
|
fileList := []string{}
|
2022-09-08 15:57:59 +00:00
|
|
|
|
|
|
|
for i := 0; i < len(args); i++ {
|
2022-09-08 17:12:58 +00:00
|
|
|
if Recursive {
|
|
|
|
f, err := getFilesRecursive(args[i])
|
|
|
|
if err != nil {
|
2022-09-08 18:12:50 +00:00
|
|
|
return nil, err
|
2022-09-08 17:12:58 +00:00
|
|
|
}
|
2022-09-08 20:30:51 +00:00
|
|
|
|
2022-09-08 17:12:58 +00:00
|
|
|
fileList = append(fileList, f...)
|
|
|
|
} else {
|
|
|
|
f, err := getFiles(args[i])
|
|
|
|
if err != nil {
|
2022-09-08 18:12:50 +00:00
|
|
|
return nil, err
|
2022-09-08 17:12:58 +00:00
|
|
|
}
|
2022-09-08 20:30:51 +00:00
|
|
|
|
2022-09-08 17:12:58 +00:00
|
|
|
fileList = append(fileList, f...)
|
|
|
|
}
|
2022-09-08 15:57:59 +00:00
|
|
|
}
|
|
|
|
|
2022-09-08 18:12:50 +00:00
|
|
|
return fileList, nil
|
2022-09-08 17:12:58 +00:00
|
|
|
}
|
2022-09-08 15:57:59 +00:00
|
|
|
|
2022-09-08 17:12:58 +00:00
|
|
|
func pickFile(fileList []string) (string, string) {
|
2022-09-08 20:30:51 +00:00
|
|
|
rand.Seed(time.Now().UnixMicro())
|
2022-09-08 17:12:58 +00:00
|
|
|
|
|
|
|
filePath := fileList[rand.Intn(len(fileList))]
|
|
|
|
fileName := filepath.Base(filePath)
|
2022-09-08 15:57:59 +00:00
|
|
|
|
2022-09-08 20:30:51 +00:00
|
|
|
return fileName, filePath
|
|
|
|
}
|
|
|
|
|
2022-09-09 19:28:57 +00:00
|
|
|
func normalizePaths(args []string) ([]string, error) {
|
2022-09-08 20:30:51 +00:00
|
|
|
var paths []string
|
|
|
|
|
|
|
|
for i := 0; i < len(args); i++ {
|
|
|
|
absolutePath, err := filepath.Abs(args[i])
|
|
|
|
if err != nil {
|
2022-09-09 19:28:57 +00:00
|
|
|
return nil, err
|
2022-09-08 20:30:51 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
paths = append(paths, absolutePath)
|
|
|
|
}
|
|
|
|
|
2022-09-09 19:28:57 +00:00
|
|
|
return paths, nil
|
2022-09-08 15:57:59 +00:00
|
|
|
}
|