Allow multiple directories, Rename module

This commit is contained in:
MrLetsplay 2023-09-24 22:13:16 +02:00
parent e4774d0d6a
commit 41504f2ea1
Signed by: mr
SSH Key Fingerprint: SHA256:92jBH80vpXyaZHjaIl47pjRq+Yt7XGTArqQg1V7hSqg
3 changed files with 45 additions and 43 deletions

2
.gitignore vendored
View File

@ -1,2 +1,2 @@
pdf PDFIndexer
index.json index.json

2
go.mod
View File

@ -1,4 +1,4 @@
module cringe-studios.com/pdf module git.cringe-studios.com/CringeStudios/PDFIndexer
go 1.20 go 1.20

16
main.go
View File

@ -13,15 +13,16 @@ import (
"code.sajari.com/docconv" "code.sajari.com/docconv"
) )
var fileFormats = []string{".pdf", ".jpg", ".png", ".txt", ".docx", ".doc", ".tif"} var fileFormats = []string{".pdf", ".jpg", ".png", ".txt", ".docx", ".doc", ".tif", "tiff"}
func buildIndex(dir string, old map[string]string) (map[string]string, error) { func buildIndex(dirs []string, old map[string]string) (map[string]string, error) {
var index = old var index = old
if index == nil { if index == nil {
index = make(map[string]string) index = make(map[string]string)
} }
for _, dir := range dirs {
err := filepath.Walk(dir, func(path string, info fs.FileInfo, err error) error { err := filepath.Walk(dir, func(path string, info fs.FileInfo, err error) error {
if err != nil { if err != nil {
return err return err
@ -63,6 +64,7 @@ func buildIndex(dir string, old map[string]string) (map[string]string, error) {
if err != nil { if err != nil {
return nil, err return nil, err
} }
}
return index, nil return index, nil
} }
@ -72,13 +74,13 @@ func main() {
flag.Parse() flag.Parse()
if len(flag.Args()) != 2 { if len(flag.Args()) < 2 {
fmt.Println("Usage:", os.Args[0], "<directory> <destination file>") fmt.Println("Usage:", os.Args[0], "<destination file> <directory> [directories...]")
os.Exit(1) os.Exit(1)
} }
directory := flag.Arg(0) indexFile := flag.Arg(0)
indexFile := flag.Arg(1) directories := flag.Args()[1:]
var oldIndex = make(map[string]string) var oldIndex = make(map[string]string)
if !*force { if !*force {
@ -97,7 +99,7 @@ func main() {
} }
} }
index, err := buildIndex(directory, oldIndex) index, err := buildIndex(directories, oldIndex)
if err != nil { if err != nil {
log.Panicln(err) log.Panicln(err)
} }