Add hooks functionality
This commit is contained in:
44
internal/torrent/hooks.go
Normal file
44
internal/torrent/hooks.go
Normal file
@@ -0,0 +1,44 @@
|
||||
package torrent
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"fmt"
|
||||
"log"
|
||||
"os/exec"
|
||||
|
||||
"github.com/debridmediamanager.com/zurg/internal/config"
|
||||
)
|
||||
|
||||
type ScriptExecutor struct {
|
||||
Script string
|
||||
}
|
||||
|
||||
func (se *ScriptExecutor) Execute() (string, error) {
|
||||
if se.Script == "" {
|
||||
return "", nil
|
||||
}
|
||||
cmd := exec.Command("/bin/sh", "-c", se.Script)
|
||||
var out bytes.Buffer
|
||||
var stderr bytes.Buffer
|
||||
cmd.Stdout = &out
|
||||
cmd.Stderr = &stderr
|
||||
err := cmd.Run()
|
||||
if err != nil {
|
||||
return "", fmt.Errorf("error executing script: %v; stderr: %s", err, stderr.String())
|
||||
}
|
||||
return out.String(), nil
|
||||
}
|
||||
|
||||
func OnLibraryUpdateHook(config config.ConfigInterface) {
|
||||
executor := &ScriptExecutor{
|
||||
Script: config.GetOnLibraryUpdate(),
|
||||
}
|
||||
output, err := executor.Execute()
|
||||
if err != nil {
|
||||
log.Printf("Failed to execute hook on_library_update:\n%v\n", err)
|
||||
return
|
||||
}
|
||||
if output != "" {
|
||||
log.Printf("Output of hook on_library_update:\n%s\n", output)
|
||||
}
|
||||
}
|
||||
@@ -202,6 +202,7 @@ func (t *TorrentManager) startRefreshJob() {
|
||||
go t.repairAll(&wg)
|
||||
}
|
||||
go t.mapToDirectories()
|
||||
go OnLibraryUpdateHook(t.config)
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user