57 lines
1.2 KiB
Go
57 lines
1.2 KiB
Go
package core
|
|
|
|
import (
|
|
"log"
|
|
"os"
|
|
"os/exec"
|
|
)
|
|
|
|
// HookRunner runs hooks before and after backups
|
|
type HookRunner struct {
|
|
Directory string
|
|
BeforeHook string
|
|
AfterHook string
|
|
}
|
|
|
|
// NewHookRunner creates a new hook runner
|
|
func NewHookRunner(directory, beforeHook, afterHook string) *HookRunner {
|
|
return &HookRunner{
|
|
Directory: directory,
|
|
BeforeHook: beforeHook,
|
|
AfterHook: afterHook,
|
|
}
|
|
}
|
|
|
|
// RunBeforeHook executes the before hook if defined
|
|
func (h *HookRunner) RunBeforeHook() error {
|
|
if h.BeforeHook == "" {
|
|
return nil
|
|
}
|
|
|
|
log.Printf("Running before hook: %s", h.BeforeHook)
|
|
return h.runCommand(h.BeforeHook)
|
|
}
|
|
|
|
// RunAfterHook executes the after hook if defined
|
|
func (h *HookRunner) RunAfterHook() error {
|
|
if h.AfterHook == "" {
|
|
return nil
|
|
}
|
|
|
|
log.Printf("Running after hook: %s", h.AfterHook)
|
|
return h.runCommand(h.AfterHook)
|
|
}
|
|
|
|
// runCommand executes a shell command in the specified directory
|
|
func (h *HookRunner) runCommand(command string) error {
|
|
cmd := exec.Command("sh", "-c", command)
|
|
if h.Directory != "" {
|
|
cmd.Dir = h.Directory
|
|
}
|
|
cmd.Stdout = os.Stdout
|
|
cmd.Stderr = os.Stderr
|
|
|
|
log.Printf("Executing command in %s: %s", h.Directory, command)
|
|
return cmd.Run()
|
|
}
|