1
0

feat: search file in directory by substring

This commit is contained in:
dump_stack() 2023-05-14 21:03:57 +00:00
parent b4a75dc66e
commit c75f10e692
Signed by: dump_stack
GPG Key ID: BE44DA8C062D87DC

View File

@ -1,8 +1,10 @@
package fs
import (
"errors"
"os"
"path/filepath"
"strings"
"code.dumpstack.io/tools/out-of-tree/config"
)
@ -51,3 +53,20 @@ func PathExists(path string) bool {
func TempDir() (string, error) {
return os.MkdirTemp(config.Dir("tmp"), "")
}
func FindBySubstring(dir, substring string) (k string, err error) {
files, err := os.ReadDir(dir)
if err != nil {
return
}
for _, file := range files {
if strings.Contains(file.Name(), substring) {
k = filepath.Join(dir, file.Name())
return
}
}
err = errors.New("not found")
return
}