dir: Return errors from executing process commands

This commit is contained in:
adnano 2021-09-03 00:31:07 -04:00
parent d300bc23c0
commit bfdbace471
2 changed files with 14 additions and 7 deletions

13
dir.go
View file

@ -116,7 +116,9 @@ func (d *Dir) _read(srcDir, path string, task *Task, cfg *Site) error {
if cmd, ok := task.Preprocess[strings.TrimPrefix(ext, ".")]; ok { if cmd, ok := task.Preprocess[strings.TrimPrefix(ext, ".")]; ok {
var buf bytes.Buffer var buf bytes.Buffer
execute(cmd, bytes.NewReader(content), &buf) if err := execute(cmd, bytes.NewReader(content), &buf); err != nil {
return err
}
content = buf.Bytes() content = buf.Bytes()
} }
page.Content = string(content) page.Content = string(content)
@ -266,7 +268,9 @@ func (d *Dir) write(dstDir string, task *Task) error {
var content []byte var content []byte
if cmd := task.Postprocess; cmd != "" { if cmd := task.Postprocess; cmd != "" {
var buf bytes.Buffer var buf bytes.Buffer
execute(cmd, strings.NewReader(page.Content), &buf) if err := execute(cmd, strings.NewReader(page.Content), &buf); err != nil {
return err
}
content = buf.Bytes() content = buf.Bytes()
} else { } else {
content = []byte(page.Content) content = []byte(page.Content)
@ -329,7 +333,7 @@ func (d *Dir) sort() {
} }
// execute runs a command. // execute runs a command.
func execute(command string, input io.Reader, output io.Writer) { func execute(command string, input io.Reader, output io.Writer) error {
split := strings.Split(command, " ") split := strings.Split(command, " ")
cmd := exec.Command(split[0], split[1:]...) cmd := exec.Command(split[0], split[1:]...)
cmd.Stdin = input cmd.Stdin = input
@ -337,8 +341,9 @@ func execute(command string, input io.Reader, output io.Writer) {
cmd.Stdout = output cmd.Stdout = output
err := cmd.Run() err := cmd.Run()
if err != nil { if err != nil {
log.Fatal(err) return err
} }
return nil
} }
func (d *Dir) Title() string { func (d *Dir) Title() string {

View file

@ -55,10 +55,12 @@ func (_strings) TrimRight(a, b string) string { return strings.TrimRight
func (_strings) TrimSpace(s string) string { return strings.TrimSpace(s) } func (_strings) TrimSpace(s string) string { return strings.TrimSpace(s) }
func (_strings) TrimSuffix(a, b string) string { return strings.TrimSuffix(a, b) } func (_strings) TrimSuffix(a, b string) string { return strings.TrimSuffix(a, b) }
func executeString(command, input string) string { func executeString(command, input string) (string, error) {
var b strings.Builder var b strings.Builder
execute(command, strings.NewReader(input), &b) if err := execute(command, strings.NewReader(input), &b); err != nil {
return b.String() return "", err
}
return b.String(), nil
} }
func reverse(s interface{}) interface{} { func reverse(s interface{}) interface{} {