Skip to content

Commit

Permalink
validate cmd: wildcard support
Browse files Browse the repository at this point in the history
  • Loading branch information
hhrutter committed Aug 26, 2021
1 parent 8837dd1 commit 7e1546b
Show file tree
Hide file tree
Showing 5 changed files with 33 additions and 11 deletions.
20 changes: 16 additions & 4 deletions cmd/pdfcpu/process.go
Original file line number Diff line number Diff line change
Expand Up @@ -106,13 +106,25 @@ func process(cmd *cli.Command) {
os.Exit(0)
}
func processValidateCommand(conf *pdfcpu.Configuration) {
if len(flag.Args()) == 0 || len(flag.Args()) > 1 || selectedPages != "" {
if len(flag.Args()) == 0 || selectedPages != "" {
fmt.Fprintf(os.Stderr, "%s\n\n", usageValidate)
os.Exit(1)
}

inFile := flag.Arg(0)
ensurePdfExtension(inFile)
inFiles := []string{}
for _, arg := range flag.Args() {
ensurePdfExtension(arg)
if strings.Contains(arg, "*") {
matches, err := filepath.Glob(arg)
if err != nil {
fmt.Fprintf(os.Stderr, "%s", err)
os.Exit(1)
}
inFiles = append(inFiles, matches...)
continue
}
inFiles = append(inFiles, arg)
}

if mode != "" && mode != "strict" && mode != "s" && mode != "relaxed" && mode != "r" {
fmt.Fprintf(os.Stderr, "%s\n\n", usageValidate)
Expand All @@ -130,7 +142,7 @@ func processValidateCommand(conf *pdfcpu.Configuration) {
conf.ValidateLinks = true
}

process(cli.ValidateCommand(inFile, conf))
process(cli.ValidateCommand(inFiles, conf))
}

func processOptimizeCommand(conf *pdfcpu.Configuration) {
Expand Down
4 changes: 2 additions & 2 deletions cmd/pdfcpu/usage.go
Original file line number Diff line number Diff line change
Expand Up @@ -77,13 +77,13 @@ common flags: -v(erbose) ... turn on logging
cm ... centimetres
mm ... millimetres`

usageValidate = "usage: pdfcpu validate [-m(ode) strict|relaxed] [-l(inks)] inFile" + generalFlags
usageValidate = "usage: pdfcpu validate [-m(ode) strict|relaxed] [-l(inks)] inFile..." + generalFlags

usageLongValidate = `Check inFile for specification compliance.
mode ... validation mode
links ... check for broken links
inFile ... input pdf file
inFile ... a list of pdf input files
The validation modes are:
Expand Down
10 changes: 10 additions & 0 deletions pkg/api/validate.go
Original file line number Diff line number Diff line change
Expand Up @@ -97,3 +97,13 @@ func ValidateFile(inFile string, conf *pdfcpu.Configuration) error {

return nil
}

// ValidateFiles validates inFiles.
func ValidateFiles(inFiles []string, conf *pdfcpu.Configuration) error {
for _, inFile := range inFiles {
if err := ValidateFile(inFile, conf); err != nil {
return err
}
}
return nil
}
2 changes: 1 addition & 1 deletion pkg/cli/cli.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ func Validate(cmd *Command) ([]string, error) {
return nil, errors.New("validate: mode == ValidationNone")
}

return nil, api.ValidateFile(*cmd.InFile, conf)
return nil, api.ValidateFiles(cmd.InFiles, conf)
}

// Optimize inFile and write result to outFile.
Expand Down
8 changes: 4 additions & 4 deletions pkg/cli/cmd.go
Original file line number Diff line number Diff line change
Expand Up @@ -101,15 +101,15 @@ var cmdMap = map[pdfcpu.CommandMode]func(cmd *Command) ([]string, error){
}

// ValidateCommand creates a new command to validate a file.
func ValidateCommand(inFile string, conf *pdfcpu.Configuration) *Command {
func ValidateCommand(inFiles []string, conf *pdfcpu.Configuration) *Command {
if conf == nil {
conf = pdfcpu.NewDefaultConfiguration()
}
conf.Cmd = pdfcpu.VALIDATE
return &Command{
Mode: pdfcpu.VALIDATE,
InFile: &inFile,
Conf: conf}
Mode: pdfcpu.VALIDATE,
InFiles: inFiles,
Conf: conf}
}

// OptimizeCommand creates a new command to optimize a file.
Expand Down

0 comments on commit 7e1546b

Please sign in to comment.