-
Notifications
You must be signed in to change notification settings - Fork 0
/
dedup.go
95 lines (81 loc) · 2.8 KB
/
dedup.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
package cmd
import (
"bufio"
"io"
"log"
"os"
"strings"
"github.com/spf13/cobra"
"github.com/hikhvar/exifsorter/pkg/archive"
)
const (
directoryParameterName = "directory"
inputParameterName = "input"
delimiterParameterName = "delimiter"
dryrunParameterName = "dry-run"
)
// dedupCmd represents the dedup command
var dedupCmd = &cobra.Command{
Use: "dedup",
Short: "Deduplicate the files in the given directory",
Long: `Deduplicate the files in the given directory. The duplicated files must be given in a file. The format of the given file is the output of: https://gitlab.com/opennota/findimagedupes`,
Args: cobra.ExactArgs(0),
Run: func(cmd *cobra.Command, args []string) {
archiveRoot := cmd.Flag(directoryParameterName).Value.String()
inputFilePath := cmd.Flag(inputParameterName).Value.String()
delimiter := cmd.Flag("delimiter").Value.String()
f, err := os.Open(inputFilePath)
if err != nil {
log.Printf("can't open input file: %s", err)
os.Exit(1)
}
if len(delimiter) > 1 {
log.Printf("can only use a single character as delimiter. '%s' has the length %d", delimiter, len(delimiter))
os.Exit(1)
}
if len(delimiter) < 1 {
log.Printf("Empty string not allowed as delimiter")
os.Exit(1)
}
duplicates, err := readInput(f, delimiter)
if err != nil {
log.Printf("failed to read input file: %s", err)
os.Exit(1)
}
dryRun, err := cmd.PersistentFlags().GetBool(dryrunParameterName)
if err != nil {
log.Printf("expected dry-run flag, didn't found it: %s", err)
}
var fs archive.FileSystem = archive.NewOSFileSystem()
if dryRun {
fs = archive.NewLoggingFileSystem()
}
err = archive.DeduplicateAll(archiveRoot, duplicates, fs)
if err != nil {
log.Printf("failed to deduplicate files: %s", err)
os.Exit(1)
}
},
}
func readInput(reader io.Reader, delimiter string) ([][]string, error) {
var ret [][]string
s := bufio.NewScanner(reader)
for s.Scan() {
line := strings.Split(s.Text(), delimiter)
ret = append(ret, line)
}
return ret, s.Err()
}
func init() {
rootCmd.AddCommand(dedupCmd)
// Here you will define your flags and configuration settings.
// Cobra supports Persistent Flags which will work for this command
// and all subcommands, e.g.:
dedupCmd.PersistentFlags().StringP(directoryParameterName, "", "", "directory to deduplicate in")
dedupCmd.PersistentFlags().StringP(inputParameterName, "i", "", "path to a file with duplicated files")
dedupCmd.PersistentFlags().StringP(delimiterParameterName, "", " ", "delimiter used in the file given by INPUT")
dedupCmd.PersistentFlags().BoolP(dryrunParameterName, "", true, "don't deduplicate, only dry-run")
// Cobra supports local flags which will only run when this command
// is called directly, e.g.:
// listCmd.Flags().BoolP("toggle", "t", false, "Help message for toggle")
}