new attribute interpreter for commands (#7)

This commit is contained in:
Julien Neuhart
2018-05-16 10:55:08 +02:00
committed by GitHub
parent faec55dd01
commit 5100693789
15 changed files with 102 additions and 25 deletions

View File

@@ -24,7 +24,7 @@ type commandTimeoutError struct {
timeout int
}
const commandTimeoutErrorMessage = "the command '%s' has reached the %d second(s) timeout"
const commandTimeoutErrorMessage = "the command %s has reached the %d second(s) timeout"
func (e *commandTimeoutError) Error() string {
return fmt.Sprintf(commandTimeoutErrorMessage, e.command, e.timeout)
@@ -32,11 +32,13 @@ func (e *commandTimeoutError) Error() string {
// run runs the given command. If timeout is reached or
// something bad happened, returns an error.
func (r *runner) run(command string, timeout int) error {
func (r *runner) run(command string, interpreter []string, timeout int) error {
r.mu.Lock()
defer r.mu.Unlock()
cmd := exec.Command("/bin/sh", "-c", command)
binary := interpreter[0]
parameters := append(interpreter[1:], command)
cmd := exec.Command(binary, parameters...)
logger.Debugf("executing command %s", cmd.Args)
if err := cmd.Start(); err != nil {
@@ -85,7 +87,7 @@ func Unconv(workingDir string, file *gfile.File) (string, error) {
return "", err
}
err = forest.run(data.String(), cmd.Timeout)
err = forest.run(data.String(), cmd.Interpreter, cmd.Timeout)
if err != nil {
return "", err
}
@@ -114,7 +116,7 @@ func Merge(workingDir string, filesPaths []string) (string, error) {
return "", err
}
err = forest.run(data.String(), cmd.Timeout)
err = forest.run(data.String(), cmd.Interpreter, cmd.Timeout)
if err != nil {
return "", err
}

View File

@@ -4,6 +4,7 @@ import (
"fmt"
"os"
"path/filepath"
"strings"
"testing"
"github.com/thecodingmachine/gotenberg/app/config"
@@ -42,19 +43,19 @@ func TestRun(t *testing.T) {
// case 1: uses a simple command.
cmd = "echo Hello world"
if err := forest.run(cmd, 30); err != nil {
if err := forest.run(cmd, strings.Fields("/bin/sh -c"), 30); err != nil {
t.Errorf("Command '%s' should have worked", cmd)
}
// case 2: uses a simple command but with an unsuitable timeout.
cmd = "sleep 5"
if err := forest.run(cmd, 0); err == nil {
if err := forest.run(cmd, strings.Fields("/bin/sh -c"), 0); err == nil {
t.Errorf("Command '%s' should not have worked", cmd)
}
// case 3: uses a broken command.
cmd = "helloworld"
if err := forest.run(cmd, 30); err == nil {
if err := forest.run(cmd, strings.Fields("/bin/sh -c"), 30); err == nil {
t.Errorf("Command '%s' should not have worked", cmd)
}
}