Reenable output for hook script

This commit is contained in:
Ben Sarmiento
2023-12-02 23:59:46 +01:00
parent 5dc9da15cf
commit 311c9c7f2e

View File

@@ -1,6 +1,7 @@
package torrent
import (
"bytes"
"fmt"
"os/exec"
@@ -13,27 +14,22 @@ type ScriptExecutor struct {
Args []string
}
func (se *ScriptExecutor) Execute() error {
func (se *ScriptExecutor) Execute() (string, error) {
if se.Script == "" {
return nil
return "", nil
}
// Prepare the command
cmd := exec.Command("/bin/sh", "-c", se.Script)
cmd.Args = append(cmd.Args, "zurg")
cmd.Args = append(cmd.Args, se.Args...)
// Start the command and immediately return, not caring about its state
err := cmd.Start()
var out bytes.Buffer
var stderr bytes.Buffer
cmd.Stdout = &out
cmd.Stderr = &stderr
err := cmd.Run()
if err != nil {
return fmt.Errorf("error starting script: %v", err)
return "", fmt.Errorf("error executing script: %v; stderr: %s", err, stderr.String())
}
// Detach the process
go func() {
cmd.Wait()
}()
return nil
return out.String(), nil
}
func OnLibraryUpdateHook(paths []string, config config.ConfigInterface, log *zap.SugaredLogger) {
@@ -41,11 +37,12 @@ func OnLibraryUpdateHook(paths []string, config config.ConfigInterface, log *zap
Script: config.GetOnLibraryUpdate(),
Args: paths,
}
err := executor.Execute()
output, err := executor.Execute()
if err != nil {
log.Errorf("Failed to execute hook on_library_update: %v", err)
return
}
log.Infof("Successfully executed hook on_library_update")
// No need to log the output since we're not capturing it
if output != "" {
log.Infof("Output of hook on_library_update:\n%s", output)
}
}