adding markdown conversion to PDF + fixing some issue in CI process

This commit is contained in:
Julien Neuhart
2018-04-10 16:56:30 +02:00
parent 563733f496
commit e29ab4760a
21 changed files with 114 additions and 19 deletions

View File

@@ -33,6 +33,9 @@ type (
// CommandsConfig gathers all commands' configurations as defined
// by the user in the gotenberg.yml file.
CommandsConfig struct {
// Markdown is the command's configuration for converting
// an markdown file to PDF.
Markdown *CommandConfig
// HTML is the command's configuration for converting
// an HTML file to PDF.
HTML *CommandConfig
@@ -79,13 +82,20 @@ func NewAppConfig(configurationFilePath string) (*AppConfig, error) {
c.Logs.Formatter = formatter
c.CommandsConfig = &CommandsConfig{}
c.CommandsConfig.Markdown = &CommandConfig{}
c.CommandsConfig.HTML = &CommandConfig{}
c.CommandsConfig.Office = &CommandConfig{}
c.CommandsConfig.Merge = &CommandConfig{}
c.CommandsConfig.Markdown.Timeout = fileConfig.Commands.Markdown.Timeout
c.CommandsConfig.HTML.Timeout = fileConfig.Commands.HTML.Timeout
c.CommandsConfig.Office.Timeout = fileConfig.Commands.Office.Timeout
c.CommandsConfig.Merge.Timeout = fileConfig.Commands.Merge.Timeout
tmplMarkdown, err := getCommandTemplate(fileConfig.Commands.Markdown.Template, "Markdown")
if err != nil {
return nil, err
}
tmplHTML, err := getCommandTemplate(fileConfig.Commands.HTML.Template, "HTML")
if err != nil {
return nil, err
@@ -101,6 +111,7 @@ func NewAppConfig(configurationFilePath string) (*AppConfig, error) {
return nil, err
}
c.CommandsConfig.Markdown.Template = tmplMarkdown
c.CommandsConfig.HTML.Template = tmplHTML
c.CommandsConfig.Office.Template = tmplOffice
c.CommandsConfig.Merge.Template = tmplMerge
@@ -116,6 +127,10 @@ type fileConfig struct {
Format string `yaml:"format"`
} `yaml:"logs"`
Commands struct {
Markdown struct {
Timeout int `yaml:"timeout"`
Template string `yaml:"template"`
} `yaml:"markdown"`
HTML struct {
Timeout int
Template string

View File

@@ -29,25 +29,31 @@ func TestNewAppConfig(t *testing.T) {
t.Error("AppConfig should not have been instantiated!")
}
// case 5: uses a configuration file with a wrong HTML command template.
// case 5: uses a configuration file with a wrong markdown command template.
path, _ = filepath.Abs("../../_tests/configurations/wrong-markdown-command-template-gotenberg.yml")
if _, err := NewAppConfig(path); err == nil {
t.Error("AppConfig should not have been instantiated!")
}
// case 6: uses a configuration file with a wrong HTML command template.
path, _ = filepath.Abs("../../_tests/configurations/wrong-html-command-template-gotenberg.yml")
if _, err := NewAppConfig(path); err == nil {
t.Error("AppConfig should not have been instantiated!")
}
// case 6: uses a configuration file with a wrong Office command template.
// case 7: uses a configuration file with a wrong Office command template.
path, _ = filepath.Abs("../../_tests/configurations/wrong-office-command-template-gotenberg.yml")
if _, err := NewAppConfig(path); err == nil {
t.Error("AppConfig should not have been instantiated!")
}
// case 7: uses a configuration file with a wrong merge command template.
// case 8: uses a configuration file with a wrong merge command template.
path, _ = filepath.Abs("../../_tests/configurations/wrong-merge-command-template-gotenberg.yml")
if _, err := NewAppConfig(path); err == nil {
t.Error("AppConfig should not have been instantiated!")
}
// case 8: uses a correct configuration file.
// case 9: uses a correct configuration file.
path, _ = filepath.Abs("../../_tests/configurations/gotenberg.yml")
if _, err := NewAppConfig(path); err != nil {
t.Error("AppConfig should have been instantiated!")

View File

@@ -25,6 +25,8 @@ type Type uint32
const (
// PDFType represents a... PDF file.
PDFType Type = iota
// MarkdownType represents a... markdown file.
MarkdownType
// HTMLType represents an... HTML file.
HTMLType
// OfficeType represents an... Office document.
@@ -34,6 +36,7 @@ const (
// filesTypes associates a file extension with its file kind counterpart.
var filesTypes = map[string]Type{
".pdf": PDFType,
".md": MarkdownType,
".htm": HTMLType,
".html": HTMLType,
".doc": OfficeType,

View File

@@ -3,6 +3,7 @@ package process
import (
"bytes"
"fmt"
"os/exec"
"text/template"
"time"
@@ -45,6 +46,10 @@ func Unconv(workingDir string, file *gfile.File) (string, error) {
)
switch file.Type {
case gfile.MarkdownType:
cmdTemplate = commandsConfig.Markdown.Template
cmdTimeout = commandsConfig.Markdown.Timeout
break
case gfile.HTMLType:
cmdTemplate = commandsConfig.HTML.Template
cmdTimeout = commandsConfig.HTML.Timeout
@@ -99,12 +104,13 @@ func Merge(workingDir string, filesPaths []string) (string, error) {
return cmdData.ResultFilePath, nil
}
type commandTimeoutError struct{}
const commandTimeoutErrorMessage = "The command has reached timeout"
type commandTimeoutError struct {
command string
timeout int
}
func (e *commandTimeoutError) Error() string {
return commandTimeoutErrorMessage
return fmt.Sprintf("The command '%s' has reached the %d second(s) timeout", e.command, e.timeout)
}
// run runs the given command. If timeout is reached or
@@ -126,7 +132,10 @@ func run(command string, timeout int) error {
if err := cmd.Process.Kill(); err != nil {
return err
}
return &commandTimeoutError{}
return &commandTimeoutError{
command: command,
timeout: timeout,
}
case err := <-done:
if err != nil {
return err

View File

@@ -120,8 +120,14 @@ func TestImpossibleConversionError(t *testing.T) {
}
func TestCommandTimeoutError(t *testing.T) {
err := &commandTimeoutError{}
if err.Error() != commandTimeoutErrorMessage {
t.Errorf("Error returned a wrong message: got %s want %s", err.Error(), commandTimeoutErrorMessage)
err := &commandTimeoutError{
command: "echo hello",
timeout: 30,
}
expected := fmt.Sprintf("The command '%s' has reached the %d second(s) timeout", err.command, err.timeout)
if err.Error() != expected {
t.Errorf("Error returned a wrong message: got %s want %s", err.Error(), expected)
}
}