65 lines
1.4 KiB
Go
65 lines
1.4 KiB
Go
package core
|
|
|
|
import (
|
|
"backea/internal/logging"
|
|
"os/exec"
|
|
)
|
|
|
|
type HookRunner struct {
|
|
Directory string
|
|
BeforeHook string
|
|
AfterHook string
|
|
logger *logging.Logger
|
|
}
|
|
|
|
func NewHookRunner(directory, beforeHook, afterHook string) *HookRunner {
|
|
return &HookRunner{
|
|
Directory: directory,
|
|
BeforeHook: beforeHook,
|
|
AfterHook: afterHook,
|
|
logger: logging.GetLogger(),
|
|
}
|
|
}
|
|
|
|
func (h *HookRunner) RunBeforeHook() error {
|
|
if h.BeforeHook == "" {
|
|
return nil
|
|
}
|
|
h.logger.Info("Running before hook: %s", h.BeforeHook)
|
|
err := h.runCommand(h.BeforeHook)
|
|
if err != nil {
|
|
return NewBackupError("before hook execution failed", err)
|
|
}
|
|
return nil
|
|
}
|
|
|
|
func (h *HookRunner) RunAfterHook() error {
|
|
if h.AfterHook == "" {
|
|
return nil
|
|
}
|
|
h.logger.Info("Running after hook: %s", h.AfterHook)
|
|
err := h.runCommand(h.AfterHook)
|
|
if err != nil {
|
|
return NewBackupError("after hook execution failed", err)
|
|
}
|
|
return nil
|
|
}
|
|
|
|
func (h *HookRunner) runCommand(command string) error {
|
|
cmd := exec.Command("sh", "-c", command)
|
|
if h.Directory != "" {
|
|
cmd.Dir = h.Directory
|
|
}
|
|
|
|
h.logger.Info("Executing command in %s: %s", h.Directory, command)
|
|
|
|
outputBytes, err := cmd.CombinedOutput()
|
|
if err != nil {
|
|
h.logger.ErrorWithStack(err, "Command execution failed: %s", string(outputBytes))
|
|
return NewBackupError("command execution failed: "+string(outputBytes), err)
|
|
}
|
|
|
|
h.logger.Debug("Command output: %s", string(outputBytes))
|
|
return nil
|
|
}
|