feat: use go-walk

Signed-off-by: Brian McGee <brian@bmcgee.ie>
This commit is contained in:
Brian McGee 2024-02-28 09:40:54 +00:00
parent 8333c99ebf
commit 6d9ce65f6c
Signed by: brianmcgee
GPG Key ID: D49016E76AD1E8C0
5 changed files with 66 additions and 29 deletions

3
go.mod
View File

@ -37,6 +37,7 @@ require (
github.com/mattn/go-runewidth v0.0.15 // indirect
github.com/muesli/reflow v0.3.0 // indirect
github.com/muesli/termenv v0.15.2 // indirect
github.com/opencoff/go-walk v0.6.0 // indirect
github.com/pjbgf/sha1cd v0.3.0 // indirect
github.com/pmezard/go-difflib v1.0.0 // indirect
github.com/rivo/uniseg v0.2.0 // indirect
@ -48,7 +49,7 @@ require (
golang.org/x/exp v0.0.0-20231006140011-7918f672742d // indirect
golang.org/x/mod v0.13.0 // indirect
golang.org/x/net v0.19.0 // indirect
golang.org/x/sys v0.15.0 // indirect
golang.org/x/sys v0.17.0 // indirect
golang.org/x/tools v0.14.0 // indirect
gopkg.in/warnings.v0 v0.1.2 // indirect
gopkg.in/yaml.v3 v3.0.1 // indirect

4
go.sum
View File

@ -81,6 +81,8 @@ github.com/muesli/termenv v0.15.2 h1:GohcuySI0QmI3wN8Ok9PtKGkgkFIk7y6Vpb5PvrY+Wo
github.com/muesli/termenv v0.15.2/go.mod h1:Epx+iuz8sNs7mNKhxzH4fWXGNpZwUaJKRS1noLXviQ8=
github.com/onsi/gomega v1.27.10 h1:naR28SdDFlqrG6kScpT8VWpu1xWY5nJRCF3XaYyBjhI=
github.com/onsi/gomega v1.27.10/go.mod h1:RsS8tutOdbdgzbPtzzATp12yT7kM5I5aElG3evPbQ0M=
github.com/opencoff/go-walk v0.6.0 h1:qx3ZtMDUaKmqb6ta/NgC/xlAQxuptfSB3KDOGnwIDh0=
github.com/opencoff/go-walk v0.6.0/go.mod h1:pR8u1+/gMbLGWw/7PoNd7AhBcgIr+rQ4krdI7hS85Wg=
github.com/otiai10/copy v1.14.0 h1:dCI/t1iTdYGtkvCuBG2BgR6KZa83PTclw4U5n2wAllU=
github.com/otiai10/copy v1.14.0/go.mod h1:ECfuL02W+/FkTWZWgQqXPWZgW9oeKCSQ5qVfSc4qc4w=
github.com/otiai10/mint v1.5.1 h1:XaPLeE+9vGbuyEHem1JNk3bYc7KKqyI/na0/mLd/Kks=
@ -159,6 +161,8 @@ golang.org/x/sys v0.5.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.6.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.15.0 h1:h48lPFYpsTvQJZF4EKyI4aLHaev3CxivZmv7yZig9pc=
golang.org/x/sys v0.15.0/go.mod h1:/VUhepiaJMQUp4+oa/7Zr1D23ma6VTLIYjOOTFZPUcA=
golang.org/x/sys v0.17.0 h1:25cE3gD+tdBA7lp7QfhuV+rJiE9YXTcS3VG1SqssI/Y=
golang.org/x/sys v0.17.0/go.mod h1:/VUhepiaJMQUp4+oa/7Zr1D23ma6VTLIYjOOTFZPUcA=
golang.org/x/term v0.0.0-20201126162022-7de9c90e9dd1/go.mod h1:bj7SfCRtBDWHUb9snDiAeCFNEtKQo2Wmx5Cou7ajbmo=
golang.org/x/term v0.0.0-20210927222741-03fcf44c2211/go.mod h1:jbD1KX2456YbFQfuXm/mYQcufACuNUgVhRMnK/tPxf8=
golang.org/x/term v0.2.0/go.mod h1:TVmDHMZPmdnySmBfhjOoOdhjzdE1h4u1VwSiw2l1Nuc=

View File

@ -79,6 +79,9 @@ schema = 3
[mod."github.com/muesli/termenv"]
version = "v0.15.2"
hash = "sha256-Eum/SpyytcNIchANPkG4bYGBgcezLgej7j/+6IhqoMU="
[mod."github.com/opencoff/go-walk"]
version = "v0.6.0"
hash = "sha256-kOMfoQgoDWJuCn5F3nFuQ6TIit1JcHwKt5ly0Oewpww="
[mod."github.com/otiai10/copy"]
version = "v1.14.0"
hash = "sha256-xsaL1ddkPS544y0Jv7u/INUALBYmYq29ddWvysLXk4A="
@ -128,8 +131,8 @@ schema = 3
version = "v0.5.0"
hash = "sha256-EAKeODSsct5HhXPmpWJfulKSCkuUu6kkDttnjyZMNcI="
[mod."golang.org/x/sys"]
version = "v0.15.0"
hash = "sha256-n7TlABF6179RzGq3gctPDKDPRtDfnwPdjNCMm8ps2KY="
version = "v0.17.0"
hash = "sha256-e0qnE+SitE02IzvnJKI4Uzpq9EOZY+zvE8Wf5b2e6Kg="
[mod."golang.org/x/tools"]
version = "v0.14.0"
hash = "sha256-BC/AesMg7LpIg0/e4a7Ab37rfyP2KaWBFrsonIp+JbE="

View File

@ -2,8 +2,9 @@ package walk
import (
"context"
"os"
"path/filepath"
"github.com/opencoff/go-walk"
)
type filesystemWalker struct {
@ -15,23 +16,36 @@ func (f filesystemWalker) Root() string {
return f.root
}
func (f filesystemWalker) Walk(_ context.Context, fn filepath.WalkFunc) error {
if len(f.paths) == 0 {
return filepath.Walk(f.root, fn)
func (f filesystemWalker) Walk(ctx context.Context, fn filepath.WalkFunc) error {
walkOpts := walk.Options{
OneFS: true,
Type: walk.FILE,
FollowSymlinks: false,
}
for _, path := range f.paths {
info, err := os.Stat(path)
if err = filepath.Walk(path, fn); err != nil {
return err
}
if err = fn(path, info, err); err != nil {
return err
}
names := f.paths
if len(names) == 0 {
names = []string{f.root}
}
return nil
ch, errCh := walk.Walk(names, &walkOpts)
for {
select {
case <-ctx.Done():
return ctx.Err()
case err := <-errCh:
if err != nil {
return err
}
case file, ok := <-ch:
if !ok {
return nil
} else if err := fn(file.Path, file.Stat, nil); err != nil {
return err
}
}
}
}
func NewFilesystem(root string, paths []string) (Walker, error) {

View File

@ -4,10 +4,11 @@ import (
"context"
"errors"
"fmt"
"io/fs"
"os"
"path/filepath"
"github.com/opencoff/go-walk"
"github.com/charmbracelet/log"
"github.com/go-git/go-git/v5/plumbing/format/index"
@ -31,31 +32,45 @@ func (g *gitWalker) Walk(ctx context.Context, fn filepath.WalkFunc) error {
}
if len(g.paths) > 0 {
for _, path := range g.paths {
err = filepath.Walk(path, func(path string, info fs.FileInfo, err error) error {
if info.IsDir() {
walkOpts := walk.Options{
OneFS: true,
Type: walk.FILE,
FollowSymlinks: false,
}
ch, errCh := walk.Walk(g.paths, &walkOpts)
for {
select {
case <-ctx.Done():
return ctx.Err()
case err := <-errCh:
if err != nil {
return err
}
case file, ok := <-ch:
if !ok {
return nil
}
relPath, err := filepath.Rel(g.root, path)
relPath, err := filepath.Rel(g.root, file.Path)
if err != nil {
return err
}
if _, err = idx.Entry(relPath); errors.Is(err, index.ErrEntryNotFound) {
// we skip this path as it's not staged
log.Debugf("Path not found in git index, skipping: %v, %v", relPath, path)
log.Debugf("Path not found in git index, skipping: %v, %v", relPath, file.Path)
return nil
} else if err = fn(file.Path, file.Stat, err); err != nil {
return err
}
return fn(path, info, err)
})
if err != nil {
return err
}
}
} else {
for _, entry := range idx.Entries {
select {