Skip to content

Commit

Permalink
v 0.3.0, improved structure, minified main.go, moved logic to process.go
Browse files Browse the repository at this point in the history
  • Loading branch information
gernotstarke committed Sep 9, 2024
1 parent de5fade commit 6cb353c
Show file tree
Hide file tree
Showing 4 changed files with 44 additions and 52 deletions.
10 changes: 6 additions & 4 deletions changelog.md
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
## PDFminion version history

0.2.3 add --force flag to allow existing target directory
0.3.0 better structure, minimal main package, moved logic to process.go
0.2.5 fixed nasty bug in numbering
0.2.3 add --force flag to allow existing target directory
0.2.2
0.2.1 add more command line flags, -h, -s
0.2.0 re-structured packages, add command line flags -t
0.1.0 minimal viable product, make it work.
0.2.1 add more command line flags, -h, -s
0.2.0 re-structured packages, add command line flags -t
0.1.0 minimal viable product, make it work.
49 changes: 2 additions & 47 deletions cmd/pdfminion/main.go
Original file line number Diff line number Diff line change
@@ -1,24 +1,13 @@
package main

/**
minimal viable product version of PDFminion:
* works in the currently active directory
* no config parameters
* numbers all PDFs present in directory
*/
import (
"fmt"
"log"
"os"
"pdfminion/internal/config"
"pdfminion/internal/pdf"
"sort"
)

func main() {

// TODO: simplify cfg handling -> put all flag handling (parsing, eval + error handling) in config package
cfg := config.New()
cfg.ParseFlags()

Expand All @@ -27,42 +16,8 @@ func main() {
os.Exit(1)
}

// some technical stuff needs to be initialized before we can handle PDF files
pdf.InitializePDFInternals()

files, err := pdf.CollectCandidatePDFs(cfg)

// sort, validate, copy

// sort files alphabetically (as we cannot assume any sort order from `os.Glob)
// TODO: move sort to processFiles!!
sort.Slice(files, func(i, j int) bool {
return files[i] < files[j]
})

// ensure we process only valid PDFs
// * correct file format
// * at least one page
// TODO: handle zero-page PDFs
// TODO: validatePDFs should return error if error...
pdfFiles, nrOfValidPDFs := pdf.ValidatePDFs(files)

// copy all valid PDFs to target directory
// update pdfFiles slice to contain full path to target files
err = pdf.CopyValidatedPDFs(pdfFiles, cfg.SourceDir, cfg.TargetDir, cfg.Force)

if err != nil {
fmt.Fprintf(os.Stderr, "Error during copy, aborting %v\n", err)
if err := pdf.ProcessPDFs(cfg); err != nil {
fmt.Fprintf(os.Stderr, "Error processing PDFs: %v\n", err)
os.Exit(1)
}

// TODO: log only in DEBUG mode
log.Printf("%v", pdfFiles)

// Evenify: add empty page to every file with even page count
pdf.Evenify(nrOfValidPDFs, pdfFiles)

// add page numbers
pdf.AddPageNumbersToAllFiles(nrOfValidPDFs, pdfFiles)

}
2 changes: 1 addition & 1 deletion internal/config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ type Config struct {
const (
defaultSourceDir = "_pdfs"
defaultTargetDir = "_target"
Version = "0.2.5b"
Version = "0.3.0"
)

const PageNrPrefix = ""
Expand Down
35 changes: 35 additions & 0 deletions internal/pdf/process.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
package pdf

import (
"fmt"
"log"
"pdfminion/internal/config"
"sort"
)

func ProcessPDFs(cfg *config.Config) error {
InitializePDFInternals()

files, err := CollectCandidatePDFs(cfg)
if err != nil {
return fmt.Errorf("error collecting candidate PDFs: %w", err)
}

sort.Slice(files, func(i, j int) bool {
return files[i] < files[j]
})

pdfFiles, nrOfValidPDFs := ValidatePDFs(files)

err = CopyValidatedPDFs(pdfFiles, cfg.SourceDir, cfg.TargetDir, cfg.Force)
if err != nil {
return fmt.Errorf("error during copy: %w", err)
}

log.Printf("%v", pdfFiles)

Evenify(nrOfValidPDFs, pdfFiles)
AddPageNumbersToAllFiles(nrOfValidPDFs, pdfFiles)

return nil
}

0 comments on commit 6cb353c

Please sign in to comment.