Skip to content

Commit

Permalink
Added more information to the FileInfo like the full path. This is …
Browse files Browse the repository at this point in the history
…helpful in some cases where you need the reference inside of the box itself.
  • Loading branch information
blaubaer committed Nov 13, 2020
1 parent d22edea commit befe02a
Show file tree
Hide file tree
Showing 6 changed files with 15 additions and 14 deletions.
2 changes: 1 addition & 1 deletion box.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ type Box interface {
}

type Iterable interface {
ForEach(common.FilePredicate, func(string, common.FileInfo) error) error
ForEach(common.FilePredicate, func(common.FileInfo) error) error
}

func OpenBox(base ...string) (Box, error) {
Expand Down
4 changes: 2 additions & 2 deletions box/fs/box.go
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,7 @@ func (instance *Box) Info(name string) (common.FileInfo, error) {
}
}

func (instance *Box) ForEach(predicate common.FilePredicate, callback func(string, common.FileInfo) error) error {
func (instance *Box) ForEach(predicate common.FilePredicate, callback func(common.FileInfo) error) error {
base, err := filepath.Abs(instance.base)
if err != nil {
return fmt.Errorf("cannot iterate over box %s: %v", instance.base, err)
Expand Down Expand Up @@ -112,7 +112,7 @@ func (instance *Box) ForEach(predicate common.FilePredicate, callback func(strin
return nil
}
}
return callback(p, &fileInfo{info, p})
return callback(&fileInfo{info, p})
}); err != nil {
return fmt.Errorf("cannot iterate over box %s: %v", instance.base, err)
}
Expand Down
4 changes: 2 additions & 2 deletions box/packed/box.go
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,7 @@ func (instance *Box) Close() error {
return nil
}

func (instance *Box) ForEach(predicate common.FilePredicate, callback func(string, common.FileInfo) error) error {
func (instance *Box) ForEach(predicate common.FilePredicate, callback func(common.FileInfo) error) error {
for p, e := range instance.Entries {
if predicate != nil {
if ok, err := predicate(p); err != nil {
Expand All @@ -93,7 +93,7 @@ func (instance *Box) ForEach(predicate common.FilePredicate, callback func(strin
continue
}
}
if err := callback(p, e); err != nil {
if err := callback(e); err != nil {
return err
}
}
Expand Down
2 changes: 1 addition & 1 deletion combined.go
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ func (instance CombinedBox) Close() error {
}
}

func (instance CombinedBox) ForEach(predicate common.FilePredicate, callback func(string, common.FileInfo) error) error {
func (instance CombinedBox) ForEach(predicate common.FilePredicate, callback func(common.FileInfo) error) error {
for _, box := range instance {
if ib, ok := box.(Iterable); ok {
if err := ib.ForEach(predicate, callback); err != nil {
Expand Down
11 changes: 6 additions & 5 deletions main/catCommand.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package main

import (
"github.com/echocat/goxr/box/packed"
"github.com/echocat/goxr/common"
"github.com/echocat/goxr/log"
"github.com/urfave/cli"
"io"
Expand Down Expand Up @@ -52,15 +53,15 @@ func (instance *CatCommand) ExecuteFromCli(_ *cli.Context) error {
WithField("builtBy", box.BuiltBy).
Infof("Displaying of %s...", instance.Filename)

return box.ForEach(instance.FilePredicate, func(path string, info os.FileInfo) error {
l.Infof(" %s", path)
return instance.displayEntry(path, info, box)
return box.ForEach(instance.FilePredicate, func(info common.FileInfo) error {
l.Infof(" %s", info.Path())
return instance.displayEntry(info, box)
})
})
}

func (instance *CatCommand) displayEntry(path string, _ os.FileInfo, box *packed.Box) error {
f, err := box.Open(path)
func (instance *CatCommand) displayEntry(info common.FileInfo, box *packed.Box) error {
f, err := box.Open(info.Path())
if err != nil {
return err
}
Expand Down
6 changes: 3 additions & 3 deletions main/listCommand.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,9 @@ package main

import (
"github.com/echocat/goxr/box/packed"
"github.com/echocat/goxr/common"
"github.com/echocat/goxr/log"
"github.com/urfave/cli"
"os"
"regexp"
"time"
)
Expand Down Expand Up @@ -51,8 +51,8 @@ func (instance *ListCommand) ExecuteFromCli(*cli.Context) error {
WithField("builtBy", box.BuiltBy).
Infof("Entries of %s...", instance.Filename)

return box.ForEach(instance.FilePredicate, func(path string, info os.FileInfo) error {
l.Infof(" %-30s (size: %10d, modified: %v, mod: %v)", path, info.Size(), info.ModTime().Truncate(time.Second), info.Mode())
return box.ForEach(instance.FilePredicate, func(info common.FileInfo) error {
l.Infof(" %-30s (size: %10d, modified: %v, mod: %v)", info.Path(), info.Size(), info.ModTime().Truncate(time.Second), info.Mode())
return nil
})
})
Expand Down

0 comments on commit befe02a

Please sign in to comment.