feat(api): add debug route

This commit is contained in:
Julien Neuhart
2025-02-04 10:20:29 +01:00
parent 740e701ad3
commit a1596f7c62
22 changed files with 628 additions and 4 deletions

View File

@@ -1,11 +1,14 @@
package pdftk
import (
"bytes"
"context"
"errors"
"fmt"
"os"
"os/exec"
"path/filepath"
"syscall"
"go.uber.org/zap"
@@ -52,6 +55,29 @@ func (engine *PdfTk) Validate() error {
return nil
}
// Debug returns additional debug data.
func (engine *PdfTk) Debug() map[string]interface{} {
debug := make(map[string]interface{})
cmd := exec.Command(engine.binPath, "--version") //nolint:gosec
cmd.SysProcAttr = &syscall.SysProcAttr{Setpgid: true}
output, err := cmd.Output()
if err != nil {
debug["version"] = err.Error()
return debug
}
lines := bytes.SplitN(output, []byte("\n"), 2)
if len(lines) > 0 {
debug["version"] = string(lines[0])
} else {
debug["version"] = "Unable to determine PDFtk version"
}
return debug
}
// Split splits a given PDF file.
func (engine *PdfTk) Split(ctx context.Context, logger *zap.Logger, mode gotenberg.SplitMode, inputPath, outputDirPath string) ([]string, error) {
var args []string
@@ -124,5 +150,6 @@ var (
_ gotenberg.Module = (*PdfTk)(nil)
_ gotenberg.Provisioner = (*PdfTk)(nil)
_ gotenberg.Validator = (*PdfTk)(nil)
_ gotenberg.Debuggable = (*PdfTk)(nil)
_ gotenberg.PdfEngine = (*PdfTk)(nil)
)

View File

@@ -71,6 +71,50 @@ func TestPdfTk_Validate(t *testing.T) {
}
}
func TestPdfTk_Debug(t *testing.T) {
for _, tc := range []struct {
scenario string
engine *PdfTk
expect map[string]interface{}
doNotExpect map[string]interface{}
}{
{
scenario: "cannot determine version",
engine: &PdfTk{
binPath: "foo",
},
expect: map[string]interface{}{
"version": `exec: "foo": executable file not found in $PATH`,
},
},
{
scenario: "success",
engine: &PdfTk{
binPath: "echo",
},
doNotExpect: map[string]interface{}{
"version": `exec: "echo": executable file not found in $PATH`,
},
},
} {
t.Run(tc.scenario, func(t *testing.T) {
d := tc.engine.Debug()
if tc.expect != nil {
if !reflect.DeepEqual(d, tc.expect) {
t.Errorf("expected '%v' but got '%v'", tc.expect, d)
}
}
if tc.doNotExpect != nil {
if reflect.DeepEqual(d, tc.doNotExpect) {
t.Errorf("did not expect '%v'", d)
}
}
})
}
}
func TestPdfTk_Merge(t *testing.T) {
for _, tc := range []struct {
scenario string