Skip to content

Commit

Permalink
Refactoring and bug fixes
Browse files Browse the repository at this point in the history
- Better docs
- Fix bug where generate a new target path could unwittingly
overwrite an existing file
  • Loading branch information
ayoisaiah committed May 29, 2021
1 parent f3e2fb9 commit e806ff3
Show file tree
Hide file tree
Showing 8 changed files with 200 additions and 176 deletions.
2 changes: 1 addition & 1 deletion src/app.go
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,7 @@ func GetApp() *cli.App {
},
Usage: "F2 is a command-line tool for batch renaming multiple files and directories quickly and safely",
UsageText: "FLAGS [OPTIONS] [PATHS...]",
Version: "v1.6.5",
Version: "v1.6.6",
EnableBashCompletion: true,
Flags: []cli.Flag{
&cli.StringSliceFlag{
Expand Down
65 changes: 64 additions & 1 deletion src/operation.go
Original file line number Diff line number Diff line change
Expand Up @@ -682,6 +682,68 @@ func setOptions(op *Operation, c *cli.Context) error {
return nil
}

// walk is used to navigate directories recursively
// and include their contents in the pool of paths in
// which to find matches. It respects the following properties
// set on the operation: whether hidden files should be
// included, and the maximum depth limit (0 for no limit).
// The paths argument is modified in place
func (op *Operation) walk(paths map[string][]os.DirEntry) error {
var recursedPaths []string
var currentDepth int
// currentLevel represents the current level of directories
// and their contents
var currentLevel = make(map[string][]os.DirEntry)

loop:
// The goal of each iteration is to created entries for each
// unaccounted directory in the current level
for dir, dirContents := range paths {
if contains(recursedPaths, dir) {
continue
}

if !op.includeHidden {
var err error
dirContents, err = removeHidden(dirContents, dir)
if err != nil {
return err
}
}

for _, entry := range dirContents {
if entry.IsDir() {
fp := filepath.Join(dir, entry.Name())
dirEntry, err := os.ReadDir(fp)
if err != nil {
return err
}

currentLevel[fp] = dirEntry
}
}

recursedPaths = append(recursedPaths, dir)
}

// if there are directories in the current level
// store each directory entry and empty the
// currentLevel so that it may be repopulated
if len(currentLevel) > 0 {
for dir, dirContents := range currentLevel {
paths[dir] = dirContents
delete(currentLevel, dir)
}

currentDepth++
if !(op.maxDepth > 0 && currentDepth == op.maxDepth) {
goto loop
}
}

return nil
}

// newOperation returns an Operation constructed
// from command line flags & arguments
func newOperation(c *cli.Context) (*Operation, error) {
Expand All @@ -703,6 +765,7 @@ func newOperation(c *cli.Context) (*Operation, error) {
return nil, err
}

// If reverting an operation, no need to walk through directories
if op.revert {
return op, nil
}
Expand All @@ -724,7 +787,7 @@ func newOperation(c *cli.Context) (*Operation, error) {
}

if op.recursive {
paths, err = walk(paths, op.includeHidden, op.maxDepth)
err = op.walk(paths)
if err != nil {
return nil, err
}
Expand Down
62 changes: 3 additions & 59 deletions src/utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -90,60 +90,9 @@ func filenameWithoutExtension(fileName string) string {
return fileName[:len(fileName)-len(filepath.Ext(fileName))]
}

// walk is used to navigate directories recursively
// and include their contents in the pool of paths in
// which to find matches
func walk(
paths map[string][]os.DirEntry,
includeHidden bool,
maxDepth int,
) (map[string][]os.DirEntry, error) {
var iterated []string
var n = make(map[string][]os.DirEntry)
var counter int

loop:
for k, v := range paths {
if contains(iterated, k) {
continue
}

if !includeHidden {
var err error
v, err = removeHidden(v, k)
if err != nil {
return nil, err
}
}

for _, de := range v {
if de.IsDir() {
fp := filepath.Join(k, de.Name())
dirEntry, err := os.ReadDir(fp)
if err != nil {
return nil, err
}

n[fp] = dirEntry
}
}

iterated = append(iterated, k)
}

if len(n) > 0 {
for k, v := range n {
paths[k] = v
delete(n, k)
}

counter++
if !(maxDepth > 0 && counter == maxDepth) {
goto loop
}
}

return paths, nil
func prettyPrint(i interface{}) string {
s, _ := json.MarshalIndent(i, "", "\t")
return string(s)
}

func greatestCommonDivisor(a, b int) int {
Expand Down Expand Up @@ -183,8 +132,3 @@ func exifDivision(slice []string) string {

return ""
}

func prettyPrint(i interface{}) string {
s, _ := json.MarshalIndent(i, "", "\t")
return string(s)
}
Loading

0 comments on commit e806ff3

Please sign in to comment.