1
0
Fork 0
mirror of https://git.sr.ht/~adnano/kiln synced 2024-05-20 22:36:04 +02:00

dir: Return errors from executing process commands

This commit is contained in:
Adnan Maolood 2021-09-03 00:31:07 -04:00
parent 3b04218396
commit 590a6c258c
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 {
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()
}
page.Content = string(content)
@ -266,7 +268,9 @@ func (d *Dir) write(dstDir string, task *Task) error {
var content []byte
if cmd := task.Postprocess; cmd != "" {
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()
} else {
content = []byte(page.Content)
@ -329,7 +333,7 @@ func (d *Dir) sort() {
}
// 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, " ")
cmd := exec.Command(split[0], split[1:]...)
cmd.Stdin = input
@ -337,8 +341,9 @@ func execute(command string, input io.Reader, output io.Writer) {
cmd.Stdout = output
err := cmd.Run()
if err != nil {
log.Fatal(err)
return err
}
return nil
}
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) 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
execute(command, strings.NewReader(input), &b)
return b.String()
if err := execute(command, strings.NewReader(input), &b); err != nil {
return "", err
}
return b.String(), nil
}
func reverse(s interface{}) interface{} {