diff options
Diffstat (limited to 'main.go')
| -rw-r--r-- | main.go | 76 |
1 files changed, 57 insertions, 19 deletions
@@ -56,6 +56,26 @@ type ChecksummerInput struct { Info os.FileInfo } +type ChecksumSlice []*ChecksummerResult + +func (c ChecksumSlice) BuildRemoveList(reg *regexp.Regexp, matchPath bool) ChecksumSlice { + removing := ChecksumSlice{} + for _, result := range c { + s := "" + if !matchPath { + s = result.Info.Name() + } else { + s = result.Path + } + + if reg.MatchString(s) { + removing = append(removing, result) + } + + } + return removing +} + // Worker that takes in a filepath off of a channel and outputs the checksum, // and any errors func Checksummer(done chan<- bool, input <-chan *ChecksummerInput, results chan<- *ChecksummerResult) { @@ -145,7 +165,7 @@ func main() { log.Fatalf("Error compiling provided regular expression: %s", err) } - checksums := make(map[string][]*ChecksummerResult) + checksums := make(map[string]ChecksumSlice) pths := make(chan *ChecksummerInput) results := make(chan *ChecksummerResult) done := make(chan bool) @@ -191,27 +211,43 @@ wait: } switch { - case *script: - for _, result := range list { - fmt.Printf("%s::%d::%s\n", sum, result.Info.Size(), result.Path) + case *script && *remove: + removing := list.BuildRemoveList(removeRegex, *matchPath) + + if len(removing) >= len(list) || len(removing) == 0 { + for _, result := range list { + fmt.Printf("%s::%d::%s::%s\n", + sum, + result.Info.Size(), + result.Path, + "not removing") + } + continue } - case *remove: - - removing := []*ChecksummerResult{} - for _, result := range list { - s := "" - if !*matchPath { - s = result.Info.Name() - } else { - s = result.Path + if !*removeYes { + for _, f := range removing { + fmt.Printf("%s::%d::%s::%s\n", + sum, + f.Info.Size(), + f.Path, + "would remove") } - - if removeRegex.MatchString(s) { - removing = append(removing, result) + } else { + for _, f := range removing { + fmt.Printf("%s::%d::%s::%s\n", + sum, + f.Info.Size(), + f.Path, + "removing") + err := os.Remove(f.Path) + if err != nil { + fmt.Fprintln(os.Stderr, err) + } } - } + case *remove: + removing := list.BuildRemoveList(removeRegex, *matchPath) if len(removing) >= len(list) { fmt.Printf("%s: Not removing, matches all files\n", sum) @@ -236,12 +272,14 @@ wait: } } } - + case *script: + for _, result := range list { + fmt.Printf("%s::%d::%s\n", sum, result.Info.Size(), result.Path) + } default: fmt.Println(sum) for i, result := range list { if i == 0 { - // fmt.Printf("%d kbytes\n", result.Info.Size()/1024) fmt.Println(formatBytes(result.Info.Size())) } fmt.Println("\t" + result.Path) |
