From 1cb7873a3bc8ed5cbf64924b34125a6ce246a31d Mon Sep 17 00:00:00 2001 From: Ben Sarmiento Date: Sat, 28 Oct 2023 13:19:32 +0200 Subject: [PATCH] Add hooks functionality --- internal/config/load.go | 1 + internal/config/types.go | 33 ++++++++++++++++++++++++++++ internal/config/v1.go | 28 ----------------------- internal/torrent/hooks.go | 44 +++++++++++++++++++++++++++++++++++++ internal/torrent/manager.go | 1 + 5 files changed, 79 insertions(+), 28 deletions(-) create mode 100644 internal/torrent/hooks.go diff --git a/internal/config/load.go b/internal/config/load.go index 4616c0d..07d229f 100644 --- a/internal/config/load.go +++ b/internal/config/load.go @@ -17,6 +17,7 @@ type ConfigInterface interface { GetPort() string GetDirectories() []string MeetsConditions(directory, torrentID, torrentName string, fileNames []string) bool + GetOnLibraryUpdate() string } func LoadZurgConfig(filename string) (ConfigInterface, error) { diff --git a/internal/config/types.go b/internal/config/types.go index 0d5f52d..63ca8b6 100644 --- a/internal/config/types.go +++ b/internal/config/types.go @@ -8,4 +8,37 @@ type ZurgConfig struct { RefreshEverySeconds int `yaml:"check_for_changes_every_secs"` CacheTimeHours int `yaml:"info_cache_time_hours"` CanRepair bool `yaml:"enable_repair"` + OnLibraryUpdate string `yaml:"on_library_update"` +} + +func (z *ZurgConfig) GetVersion() string { + return "v1" +} + +func (z *ZurgConfig) GetToken() string { + return z.Token +} + +func (z *ZurgConfig) GetPort() string { + return z.Port +} + +func (z *ZurgConfig) GetNumOfWorkers() int { + return z.NumOfWorkers +} + +func (z *ZurgConfig) GetRefreshEverySeconds() int { + return z.RefreshEverySeconds +} + +func (z *ZurgConfig) GetCacheTimeHours() int { + return z.CacheTimeHours +} + +func (z *ZurgConfig) EnableRepair() bool { + return z.CanRepair +} + +func (z *ZurgConfig) GetOnLibraryUpdate() string { + return z.OnLibraryUpdate } diff --git a/internal/config/v1.go b/internal/config/v1.go index 35dfa66..d4a20c3 100644 --- a/internal/config/v1.go +++ b/internal/config/v1.go @@ -17,34 +17,6 @@ func loadV1Config(content []byte) (*ZurgConfigV1, error) { return &configV1, nil } -func (z *ZurgConfigV1) GetVersion() string { - return "v1" -} - -func (z *ZurgConfigV1) GetToken() string { - return z.Token -} - -func (z *ZurgConfigV1) GetPort() string { - return z.Port -} - -func (z *ZurgConfigV1) GetNumOfWorkers() int { - return z.NumOfWorkers -} - -func (z *ZurgConfigV1) GetRefreshEverySeconds() int { - return z.RefreshEverySeconds -} - -func (z *ZurgConfigV1) GetCacheTimeHours() int { - return z.CacheTimeHours -} - -func (z *ZurgConfigV1) EnableRepair() bool { - return z.CanRepair -} - func (z *ZurgConfigV1) GetDirectories() []string { rootDirectories := make([]string, len(z.Directories)) i := 0 diff --git a/internal/torrent/hooks.go b/internal/torrent/hooks.go new file mode 100644 index 0000000..79ea0bf --- /dev/null +++ b/internal/torrent/hooks.go @@ -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) + } +} diff --git a/internal/torrent/manager.go b/internal/torrent/manager.go index 5deb188..840d872 100644 --- a/internal/torrent/manager.go +++ b/internal/torrent/manager.go @@ -202,6 +202,7 @@ func (t *TorrentManager) startRefreshJob() { go t.repairAll(&wg) } go t.mapToDirectories() + go OnLibraryUpdateHook(t.config) } }