2025-01-08 22:31:57 +00:00
|
|
|
package hook
|
|
|
|
|
|
|
|
import (
|
|
|
|
"github.com/cyphar/filepath-securejoin"
|
2025-01-19 00:37:29 +00:00
|
|
|
"os"
|
2025-01-08 22:31:57 +00:00
|
|
|
"os/exec"
|
|
|
|
"path/filepath"
|
|
|
|
)
|
|
|
|
|
|
|
|
type Hook struct {
|
|
|
|
hookDir string
|
|
|
|
sitesDir string
|
|
|
|
}
|
|
|
|
|
|
|
|
func New(hookDir string, sitesDir string) *Hook {
|
|
|
|
return &Hook{hookDir: hookDir, sitesDir: sitesDir}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (h *Hook) Run(site, branch string) error {
|
2025-01-08 23:01:58 +00:00
|
|
|
if h.sitesDir == "" || h.hookDir == "" {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2025-01-08 22:53:52 +00:00
|
|
|
sitePath, err := securejoin.SecureJoin(h.sitesDir, site+"/work@"+branch)
|
2025-01-08 22:31:57 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
scriptPath, err := securejoin.SecureJoin(h.hookDir, site)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2025-01-19 00:37:29 +00:00
|
|
|
|
|
|
|
// check if the script exists before failing to execute
|
|
|
|
_, err = os.Stat(scriptPath)
|
|
|
|
if err != nil {
|
|
|
|
if os.IsNotExist(err) {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2025-01-08 22:31:57 +00:00
|
|
|
cmd := exec.Cmd{
|
|
|
|
Path: scriptPath,
|
|
|
|
Args: []string{filepath.Base(scriptPath)},
|
|
|
|
Dir: sitePath,
|
|
|
|
}
|
|
|
|
return cmd.Run()
|
|
|
|
}
|