Skip to content

Commit

Permalink
quick and dirty implementation of selective fs
Browse files Browse the repository at this point in the history
  • Loading branch information
sio4 committed Mar 12, 2022
1 parent 774b5b9 commit eca7606
Show file tree
Hide file tree
Showing 9 changed files with 450 additions and 2 deletions.
79 changes: 79 additions & 0 deletions generator.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"io/fs"
"math/rand"
"os/exec"
"strings"
"sync"
"time"

Expand Down Expand Up @@ -80,15 +81,93 @@ func (g *Generator) Box(box packd.Walker) error {
})
}

// ExceptFS walks through a given fs.FS and adds all files into Runner's
// virtual disk, except files that starts with excludePrefix and ends with
// excludeSuffix.
// includePrefix and includeSuffix will be interpreted as AND condition
// but an empty option will not be used. So if you give a prefix but
// suffix as nil, it will only check if files starts with the given prefix.
// It is usuful if you want to exclude entire subdirs.
func (g *Generator) ExceptFS(fsys fs.FS, excludePrefix []string, excludeSuffix []string) error {
return g.SelectiveFS(fsys, nil, nil, excludePrefix, excludeSuffix)
}

// OnlyFS walks through a given fs.FS and adds only matching files into
// Runner's virtual disk, matching files that starts with includePrefix
// and ends with includeSuffix.
// includePrefix and includeSuffix will be interpreted as AND condition
// but an empty option will not be used. So if you give a prefix but
// suffix as nil, it will only check if files starts with the given prefix.
// It is usuful if you want to include specific subdirs only.
func (g *Generator) OnlyFS(fsys fs.FS, includePrefix []string, includeSuffix []string) error {
return g.SelectiveFS(fsys, includePrefix, includeSuffix, nil, nil)
}

// FS walks through a fs.FS and adds Files for each entry.
func (g *Generator) FS(fsys fs.FS) error {
return g.SelectiveFS(fsys, nil, nil, nil, nil)
}

// FS walks through a fs.FS and adds Files for each entry.
func (g *Generator) SelectiveFS(fsys fs.FS, includePrefix, includeSuffix, excludePrefix, excludeSuffix []string) error {
return fs.WalkDir(fsys, ".", func(path string, d fs.DirEntry, err error) error {
if err != nil {
return err
}
if d.IsDir() {
return nil
}

isIncluded := false
isExcluded := false
if len(includePrefix) > 0 {
for _, fix := range includePrefix {
if strings.HasPrefix(path, fix) {
isIncluded = true
break
}
}
} else {
isIncluded = true
}
inSuffix := false
if len(includeSuffix) > 0 {
for _, fix := range includeSuffix {
if strings.HasSuffix(path, fix) {
inSuffix = true
break
}
}
} else {
inSuffix = true
}
isIncluded = isIncluded && inSuffix

for _, fix := range excludePrefix {
if strings.HasPrefix(path, fix) {
isExcluded = true
break
}
}
exSuffix := false
if len(excludeSuffix) == 0 {
exSuffix = isExcluded
}
for _, fix := range excludeSuffix {
if len(excludePrefix) == 0 {
isExcluded = true
}
exSuffix = isExcluded && strings.HasSuffix(path, fix)
if exSuffix {
break
}
}
isExcluded = exSuffix

if !isIncluded || isExcluded {
return nil
}

f, err := fsys.Open(path)
if err != nil {
return err
Expand Down
Loading

0 comments on commit eca7606

Please sign in to comment.