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