markbates / pkger

Embed static files in Go binaries (replacement for gobuffalo/packr)
MIT License
1.19k stars 60 forks source link

Glob suggestion #36

Open orxobo opened 5 years ago

orxobo commented 5 years ago

Will you be adding a glob for directory file listing with a pattern? Here is a function you could add, adjusted from filepath

func glob(dir, pattern string, matches []string) (m []string, e error) {
    m = matches
    fi, err := pkger.Stat(dir)
    if err != nil {
        return
    }
    if !fi.IsDir() {
        return
    }
    d, err := pkger.Open(dir)
    if err != nil {
        return
    }
    defer d.Close()

    names, _ := d.Readdir(-1)
    sort.Strings(names)

    for _, n := range names {
        matched, err := filepath.Match(pattern, n)
        if err != nil {
            return m, err
        }
        if matched {
            m = append(m, Join(dir, n))
        }
    }
    return
}