feat: search file in directory by substring
This commit is contained in:
parent
b4a75dc66e
commit
c75f10e692
19
fs/fs.go
19
fs/fs.go
@ -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
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user