From 36dc98452d2273ae33827b397f9b4dab7115b3a1 Mon Sep 17 00:00:00 2001 From: Jose Diaz-Gonzalez Date: Wed, 18 Nov 2020 02:18:08 -0500 Subject: [PATCH] feat: add function for listing files within a directory by prefix --- plugins/common/io.go | 22 ++++++++++++++++++++++ 1 file changed, 22 insertions(+) diff --git a/plugins/common/io.go b/plugins/common/io.go index aa22f9d37..93ee48aaa 100644 --- a/plugins/common/io.go +++ b/plugins/common/io.go @@ -4,6 +4,7 @@ import ( "bufio" "fmt" "io" + "io/ioutil" "os" "os/user" "strconv" @@ -130,6 +131,27 @@ func IsAbsPath(path string) bool { return strings.HasPrefix(path, "/") } +// ListFiles lists files within a given path that have a given prefix +func ListFilesWithPrefix(path string, prefix string) []string { + names, err := ioutil.ReadDir(path) + if err != nil { + return []string{} + } + + files := []string{} + for _, f := range names { + if prefix != "" && !strings.HasPrefix(f.Name(), prefix) { + continue + } + + if f.Mode().IsRegular() { + files = append(files, fmt.Sprintf("%s/%s", path, f.Name())) + } + } + + return files +} + // ReadFirstLine gets the first line of a file that has contents and returns it // if there are no contents, an empty string is returned // will also return an empty string if the file does not exist