Skip to content

Commit

Permalink
Add missing main.go
Browse files Browse the repository at this point in the history
  • Loading branch information
lixmal committed Dec 5, 2023
1 parent 7e86790 commit 72040fc
Show file tree
Hide file tree
Showing 2 changed files with 92 additions and 1 deletion.
1 change: 0 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@
*.dll
*.so
*.dylib
finddupes

# Test binary, build with `go test -c`
*.test
Expand Down
92 changes: 92 additions & 0 deletions cmd/finddupes/main.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,92 @@
package main

import (
"errors"
"flag"
"fmt"
"log"
"os"
"os/signal"
"regexp"
"syscall"

"github.com/lixmal/finddupes/pkg/config"
"github.com/lixmal/finddupes/pkg/dupe"
)

const (
workers int = 10
)

var (
storeonly = flag.Bool("storeonly", false, "store hashes to database without trying to find duplicates")

delete = flag.Bool("delete", false, "delete duplicates based on rules")
verbose = flag.Bool("verbose", false, "enable verbose messages")

path = flag.String("path", "", "path to the hash database, will be read/written to/from if specified")

delmatch = flag.String("delmatch", "", "delete duplicates files matching the given regex")
keepmatch = flag.String("keepmatch", "", "delete all duplicate files except those matching the given regex")

keepfirst = flag.Bool("keepfirst", false, "keep lexically first file and delete all others")
keeplast = flag.Bool("keeplast", false, "keep lexically last file and delete all others")

keepoldest = flag.Bool("keepoldest", false, "keep oldest file and delete all others")
keeprecent = flag.Bool("keeprecent", false, "keep most recent file and delete all others")
)

func init() {
flag.Parse()
}

func main() {
args := flag.Args()

if *storeonly {
if *path == "" {
log.Fatal("Storeonly given, but no path specified\n")
}
if len(args) == 0 {
log.Fatal("Storeonly given, but no directories provided\n")
}
}

var reDelMatch *regexp.Regexp
var reKeepMatch *regexp.Regexp
if *delmatch != "" {
reDelMatch = regexp.MustCompile(*delmatch)
}
if *keepmatch != "" {
reKeepMatch = regexp.MustCompile(*keepmatch)
}

conf := config.Config{
StoreOnly: *storeonly,
Path: *path,
Delete: *delete,
Verbose: *verbose,
DelMatch: reDelMatch,
KeepMatch: reKeepMatch,
KeepFirst: *keepfirst,
KeepLast: *keeplast,
KeepOldest: *keepoldest,
KeepRecent: *keeprecent,
Workers: workers,
}

dup := dupe.New(conf)

sigs := make(chan os.Signal, 1)
signal.Notify(sigs, syscall.SIGINT, syscall.SIGTERM, syscall.SIGQUIT, syscall.SIGHUP)

go func() {
sig := <-sigs
fmt.Printf("\n>>>>> got %s, finishing up <<<<<\n", sig)
dup.Stop()
}()

if err := dup.ProcessFiles(args); err != nil && !errors.Is(err, dupe.ErrProcessStopped) {
log.Fatalf("Failed to process files: %s\n", err)
}
}

0 comments on commit 72040fc

Please sign in to comment.