Improve LookPath

This commit is contained in:
Bjørn Erik Pedersen
2020-12-18 18:20:12 +01:00
parent ae2d1bd52d
commit 10ae7c3210
18 changed files with 129 additions and 42 deletions

View File

@@ -18,9 +18,10 @@ package asciidocext
import (
"bytes"
"os/exec"
"path/filepath"
"github.com/cli/safeexec"
"github.com/gohugoio/hugo/identity"
"github.com/gohugoio/hugo/markup/asciidocext/asciidocext_config"
"github.com/gohugoio/hugo/markup/converter"
@@ -193,7 +194,7 @@ func (a *asciidocConverter) appendArg(args []string, option, value, defaultValue
}
func getAsciidoctorExecPath() string {
path, err := exec.LookPath("asciidoctor")
path, err := safeexec.LookPath("asciidoctor")
if err != nil {
return ""
}

View File

@@ -2,9 +2,11 @@ package internal
import (
"bytes"
"os/exec"
"strings"
"github.com/cli/safeexec"
"github.com/gohugoio/hugo/common/hexec"
"github.com/gohugoio/hugo/markup/converter"
)
@@ -13,12 +15,16 @@ func ExternallyRenderContent(
ctx converter.DocumentContext,
content []byte, path string, args []string) []byte {
logger := cfg.Logger
cmd := exec.Command(path, args...)
cmd, err := hexec.SafeCommand(path, args...)
if err != nil {
logger.Errorf("%s rendering %s: %v", path, ctx.DocumentName, err)
return nil
}
cmd.Stdin = bytes.NewReader(content)
var out, cmderr bytes.Buffer
cmd.Stdout = &out
cmd.Stderr = &cmderr
err := cmd.Run()
err = cmd.Run()
// Most external helpers exit w/ non-zero exit code only if severe, i.e.
// halting errors occurred. -> log stderr output regardless of state of err
for _, item := range strings.Split(cmderr.String(), "\n") {
@@ -40,9 +46,9 @@ func normalizeExternalHelperLineFeeds(content []byte) []byte {
}
func GetPythonExecPath() string {
path, err := exec.LookPath("python")
path, err := safeexec.LookPath("python")
if err != nil {
path, err = exec.LookPath("python.exe")
path, err = safeexec.LookPath("python.exe")
if err != nil {
return ""
}

View File

@@ -15,8 +15,7 @@
package pandoc
import (
"os/exec"
"github.com/cli/safeexec"
"github.com/gohugoio/hugo/identity"
"github.com/gohugoio/hugo/markup/internal"
@@ -65,7 +64,7 @@ func (c *pandocConverter) getPandocContent(src []byte, ctx converter.DocumentCon
}
func getPandocExecPath() string {
path, err := exec.LookPath("pandoc")
path, err := safeexec.LookPath("pandoc")
if err != nil {
return ""
}

View File

@@ -16,9 +16,10 @@ package rst
import (
"bytes"
"os/exec"
"runtime"
"github.com/cli/safeexec"
"github.com/gohugoio/hugo/identity"
"github.com/gohugoio/hugo/markup/internal"
@@ -96,9 +97,9 @@ func (c *rstConverter) getRstContent(src []byte, ctx converter.DocumentContext)
}
func getRstExecPath() string {
path, err := exec.LookPath("rst2html")
path, err := safeexec.LookPath("rst2html")
if err != nil {
path, err = exec.LookPath("rst2html.py")
path, err = safeexec.LookPath("rst2html.py")
if err != nil {
return ""
}