Skip to content

Commit

Permalink
working version 0.2.1 with make install target.
Browse files Browse the repository at this point in the history
  • Loading branch information
gernotstarke committed Aug 6, 2024
1 parent 853d922 commit 528d5d8
Show file tree
Hide file tree
Showing 5 changed files with 144 additions and 149 deletions.
59 changes: 51 additions & 8 deletions Makefile
Original file line number Diff line number Diff line change
@@ -1,13 +1,56 @@
# Makefile for PDFminion

# Go parameters
GOCMD=go
GOBUILD=$(GOCMD) build
GOCLEAN=$(GOCMD) clean
GOTEST=$(GOCMD) test
GOGET=$(GOCMD) get
BINARY_NAME=pdfminion
BINARY_UNIX=$(BINARY_NAME)_unix

# Build information
BUILDTIME=$(shell date -u +'%Y %b %d %H:%M')

# Build flags
LDFLAGS=-ldflags "-X 'pdfminion/internal/config.BuildTime=$(BUILDTIME)'"

# Install directory
INSTALL_DIR=/usr/local/bin

.PHONY: all build clean test run install uninstall

all: test build

.PHONY: build
build:
go build -o pdfminion ./cmd/pdfminion
@echo "Build Time: $(BUILDTIME)"
@echo "LDFLAGS: $(LDFLAGS)"
$(GOBUILD) $(LDFLAGS) -o $(BINARY_NAME) -v ./cmd/pdfminion

.PHONY: install
install:
go install ./cmd/pdfminion
test:
$(GOTEST) -v ./...

.PHONY: clean
clean:
rm -f pdfminion
rm -f pdfminion.exe
$(GOCLEAN)
rm -f $(BINARY_NAME)
rm -f $(BINARY_UNIX)

run:
$(GOBUILD) $(LDFLAGS) -o $(BINARY_NAME) -v ./cmd/pdfminion
./$(BINARY_NAME)

# Cross compilation
build-linux:
CGO_ENABLED=0 GOOS=linux GOARCH=amd64 $(GOBUILD) $(LDFLAGS) -o $(BINARY_UNIX) -v ./cmd/pdfminion

# Install the binary
install: build
@echo "Installing $(BINARY_NAME) to $(INSTALL_DIR)"
@sudo mv $(BINARY_NAME) $(INSTALL_DIR)/$(BINARY_NAME)
@echo "Installation complete. You can now run '$(BINARY_NAME)' from anywhere."

# Uninstall the binary
uninstall:
@echo "Uninstalling $(BINARY_NAME) from $(INSTALL_DIR)"
@sudo rm -f $(INSTALL_DIR)/$(BINARY_NAME)
@echo "Uninstallation complete. $(BINARY_NAME) has been removed."
24 changes: 16 additions & 8 deletions cmd/main_orig.go → cmd/main_orig.txt
Original file line number Diff line number Diff line change
Expand Up @@ -160,6 +160,19 @@ func main() {
}
}

pdfFiles, relaxedConf, nrOfValidPDFs := validateAndCopyPDFFiles(nrOfCandidatePDFs, err, files)

log.Printf("%v", pdfFiles)

// handle uneven page count: pad with empty page
evenify(nrOfValidPDFs, pdfFiles, relaxedConf)

// add page numbers
addPageNumbers(nrOfValidPDFs, pdfFiles, relaxedConf)

}

func validateAndCopyPDFFiles(nrOfCandidatePDFs int, err error, files []string) ([]singleFileToProcess, *model.Configuration, int) {
// create slice of singleFileToProcess of required length
pdfFiles := make([]singleFileToProcess, nrOfCandidatePDFs)

Expand Down Expand Up @@ -222,16 +235,12 @@ func main() {
}
pdfFiles[i].origByteCount = bytesWritten
}

}
}
return pdfFiles, relaxedConf, nrOfValidPDFs
}

log.Printf("%v", pdfFiles)

evenify(nrOfValidPDFs, pdfFiles, relaxedConf)

// add page numbers

func addPageNumbers(nrOfValidPDFs int, pdfFiles []singleFileToProcess, relaxedConf *model.Configuration) {
// currentOffset is the _previous_ pagenumber
var currentOffset = 0

Expand All @@ -252,7 +261,6 @@ func main() {
}
currentOffset += currentFilePageCount
}

}

func evenify(nrOfValidPDFs int, pdfFiles []singleFileToProcess, relaxedConf *model.Configuration) {
Expand Down
4 changes: 0 additions & 4 deletions domain/appVersion.go

This file was deleted.

100 changes: 0 additions & 100 deletions domain/directoriesAndFiles.go

This file was deleted.

106 changes: 77 additions & 29 deletions internal/config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@ import (
"fmt"
"io/ioutil"
"os"
"strings"
"time"
)

type Config struct {
Expand All @@ -17,14 +19,11 @@ type Config struct {
const (
defaultSourceDir = "_pdfs"
defaultTargetDir = "_target"
Version = "0.2.1"
)

// Version information
var (
Version = "development"
BuildTime = "unknown"
GitCommit = "unknown"
)
// BuildTime will be injected at build time
var BuildTime string

func New() *Config {
return &Config{
Expand All @@ -34,30 +33,45 @@ func New() *Config {
}

func (c *Config) ParseFlags() {
var sourceFlag, targetFlag string

flag.StringVar(&sourceFlag, "s", "", "Specify the source directory")
flag.StringVar(&sourceFlag, "source", "", "Specify the source directory")
flag.StringVar(&targetFlag, "t", "", "Specify the target directory")
flag.StringVar(&targetFlag, "target", "", "Specify the target directory")
flag.StringVar(&c.SourceDir, "s", defaultSourceDir, "Specify the source directory")
flag.StringVar(&c.SourceDir, "source", defaultSourceDir, "Specify the source directory")
flag.StringVar(&c.TargetDir, "t", defaultTargetDir, "Specify the target directory")
flag.StringVar(&c.TargetDir, "target", defaultTargetDir, "Specify the target directory")
flag.BoolVar(&c.showHelp, "h", false, "Show help information")
flag.BoolVar(&c.showHelp, "help", false, "Show help information")
flag.BoolVar(&c.showVersion, "v", false, "Show version information")
flag.BoolVar(&c.showVersion, "version", false, "Show version information")

// Custom usage function
flag.Usage = c.printHelp

// Parse flags
flag.Parse()

// Store the parsed values
if sourceFlag != "" {
c.SourceDir = sourceFlag
}
if targetFlag != "" {
c.TargetDir = targetFlag
// Check for unrecognized arguments or "help"
if flag.NArg() > 0 {
arg := flag.Arg(0)
if arg == "help" || arg == "?" || strings.HasPrefix(arg, "-") {
c.showHelp = true
}
}

// Check for "help" or "?" as the first argument
if flag.Arg(0) == "help" || flag.Arg(0) == "?" {
c.showHelp = true
// Check for "--" prefixed long-form flags
for i, arg := range os.Args {
switch arg {
case "--source":
if i+1 < len(os.Args) {
c.SourceDir = os.Args[i+1]
}
case "--target":
if i+1 < len(os.Args) {
c.TargetDir = os.Args[i+1]
}
case "--help":
c.showHelp = true
case "--version":
c.showVersion = true
}
}
}

Expand All @@ -76,18 +90,39 @@ func (c *Config) Evaluate() error {
}

func (c *Config) printHelp() {
fmt.Println("Usage of PDFminion:")
fmt.Printf(" -s, -source string\n\tSpecify the source directory (default \"%s\")\n", defaultSourceDir)
fmt.Printf(" -t, -target string\n\tSpecify the target directory (default \"%s\")\n", defaultTargetDir)
fmt.Println(" -h, -help\n\tShow this help message")
fmt.Println(" -v, -version\n\tShow version information")
fmt.Println(" help, ?\n\tShow this help message")
fmt.Println("PDFMinion adds page numbers to existing PDF files.")
fmt.Println("It will take all PDF files from the source directory and put the numbered copies into the target directory.")
fmt.Println("Furthermore, it will ensure that every chapter (aka file) starts with an odd number")
fmt.Println("by adding a single blank page to files with an un-even page count.")
fmt.Println("When printed double-sided, every chapter will start on a right side with an odd pagenumber.")
fmt.Println("\n\nUsage:")
fmt.Printf(" -s, --source string\n\tSpecify the source directory (default \"%s\")\n", defaultSourceDir)
fmt.Printf(" -t, --target string\n\tSpecify the target directory (default \"%s\")\n", defaultTargetDir)
fmt.Println(" -h, --help, ?, -?, help\n\tShow this help message")
fmt.Println(" -v, --version\n\tShow version information")
}

func (c *Config) printVersion() {
fmt.Printf("PDFminion version %s\n", Version)
fmt.Printf("Build time: %s\n", BuildTime)
fmt.Printf("Git commit: %s\n", GitCommit)
if BuildTime != "" {
t, err := time.Parse("2006 Jan 02 15:04", BuildTime)
if err == nil {
formattedBuildTime := t.Format("2006 Jan 02 15:04")
parts := strings.Split(formattedBuildTime, " ")
if len(parts) == 4 {
day := parts[2]
suffix := getSuffix(day)
parts[2] = day + suffix
parts[3] += "h"
formattedBuildTime = strings.Join(parts, " ")
}
fmt.Printf("Build time: %s\n", formattedBuildTime)
} else {
fmt.Printf("Build time: %s\n", BuildTime)
}
} else {
fmt.Println("Build time: Not available")
}
}

func (c *Config) validate() error {
Expand Down Expand Up @@ -122,3 +157,16 @@ func (c *Config) validateTargetDir() error {
}
return nil
}

func getSuffix(day string) string {
switch day {
case "01", "21", "31":
return "st"
case "02", "22":
return "nd"
case "03", "23":
return "rd"
default:
return "th"
}
}

0 comments on commit 528d5d8

Please sign in to comment.