From 0966a322d1e521096d5d187940bc651dc4d1bdf9 Mon Sep 17 00:00:00 2001 From: Julien Neuhart Date: Tue, 3 Apr 2018 11:35:35 +0200 Subject: [PATCH] godoc for process package + renaming some of its functions --- app/handlers/converter/converter.go | 4 ++-- app/handlers/converter/process/process.go | 18 +++++++++++++----- 2 files changed, 15 insertions(+), 7 deletions(-) diff --git a/app/handlers/converter/converter.go b/app/handlers/converter/converter.go index 7ea85c02..0b95fd4f 100644 --- a/app/handlers/converter/converter.go +++ b/app/handlers/converter/converter.go @@ -81,7 +81,7 @@ func (c *Converter) Convert() (string, error) { var filesPaths []string for _, f := range c.files { if f.Type != gfile.PDFType { - path, err := process.ExecConversion(c.workingDir, f) + path, err := process.Unconv(c.workingDir, f) if err != nil { return "", err } @@ -96,7 +96,7 @@ func (c *Converter) Convert() (string, error) { return filesPaths[0], nil } - path, err := process.ExecMerge(c.workingDir, filesPaths) + path, err := process.Merge(c.workingDir, filesPaths) if err != nil { return "", err } diff --git a/app/handlers/converter/process/process.go b/app/handlers/converter/process/process.go index ddc97ad1..4b358224 100644 --- a/app/handlers/converter/process/process.go +++ b/app/handlers/converter/process/process.go @@ -1,3 +1,4 @@ +// Package process handles all commands executions. package process import ( @@ -13,10 +14,12 @@ import ( var commandsConfig *config.CommandsConfig +// Load loads the commands configuration coming from the application configuration. func Load(config *config.CommandsConfig) { commandsConfig = config } +// conversionData will be applied to the data-driven templates of conversions commands. type conversionData struct { FilePath string ResultFilePath string @@ -28,7 +31,8 @@ func (e *impossibleConversionError) Error() string { return "Impossible conversion" } -func ExecConversion(workingDir string, file *gfile.File) (string, error) { +// Unconv converts a file to PDF and returns the new file path. +func Unconv(workingDir string, file *gfile.File) (string, error) { cmdData := &conversionData{ FilePath: file.Path, ResultFilePath: fmt.Sprintf("%s%s", gfile.MakeFilePath(workingDir), gfile.PDFExt), @@ -57,7 +61,7 @@ func ExecConversion(workingDir string, file *gfile.File) (string, error) { return "", err } - err := execCommand(data.String(), cmdTimeout) + err := run(data.String(), cmdTimeout) if err != nil { return "", err } @@ -65,12 +69,14 @@ func ExecConversion(workingDir string, file *gfile.File) (string, error) { return cmdData.ResultFilePath, nil } +// mergeDAta will be applied to the data-driven template of the merge command. type mergeData struct { FilesPaths []string ResultFilePath string } -func ExecMerge(workingDir string, filesPaths []string) (string, error) { +// Merge merges many PDF files to one unique PDF file and returns the new file path. +func Merge(workingDir string, filesPaths []string) (string, error) { cmdData := &mergeData{ FilesPaths: filesPaths, ResultFilePath: fmt.Sprintf("%s%s", gfile.MakeFilePath(workingDir), gfile.PDFExt), @@ -84,7 +90,7 @@ func ExecMerge(workingDir string, filesPaths []string) (string, error) { return "", err } - err := execCommand(data.String(), cmdTimeout) + err := run(data.String(), cmdTimeout) if err != nil { return "", err } @@ -98,7 +104,9 @@ func (e *commandTimeoutError) Error() string { return "The command has reached timeout" } -func execCommand(command string, timeout int) error { +// run runs the given command. If timeout is reached or +// something bad happened, returns an error. +func run(command string, timeout int) error { cmd := exec.Command("/bin/sh", "-c", command) if err := cmd.Start(); err != nil { return err