2017-10-02 01:24:50 +08:00
|
|
|
package hook
|
|
|
|
|
|
|
|
import (
|
|
|
|
"log"
|
|
|
|
"os"
|
|
|
|
"os/exec"
|
|
|
|
"strings"
|
|
|
|
|
|
|
|
"github.com/mholt/caddy"
|
|
|
|
)
|
|
|
|
|
|
|
|
// Hook executes a command.
|
|
|
|
func (cfg *Config) Hook(event caddy.EventName, info interface{}) error {
|
|
|
|
if event != cfg.Event {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
nonblock := false
|
2017-10-13 00:11:50 +08:00
|
|
|
if len(cfg.Args) >= 1 && cfg.Args[len(cfg.Args)-1] == "&" {
|
2017-10-02 01:24:50 +08:00
|
|
|
// Run command in background; non-blocking
|
|
|
|
nonblock = true
|
|
|
|
cfg.Args = cfg.Args[:len(cfg.Args)-1]
|
|
|
|
}
|
|
|
|
|
|
|
|
// Execute command.
|
|
|
|
cmd := exec.Command(cfg.Command, cfg.Args...)
|
|
|
|
cmd.Stdin = os.Stdin
|
|
|
|
cmd.Stdout = os.Stdout
|
|
|
|
cmd.Stderr = os.Stderr
|
|
|
|
if nonblock {
|
2017-10-22 20:43:40 +08:00
|
|
|
log.Printf("[INFO] Nonblocking Command \"%s %s\" with ID %s", cfg.Command, strings.Join(cfg.Args, " "), cfg.ID)
|
2017-10-02 01:24:50 +08:00
|
|
|
return cmd.Start()
|
|
|
|
}
|
2017-10-22 20:43:40 +08:00
|
|
|
log.Printf("[INFO] Blocking Command \"%s %s\" with ID %s", cfg.Command, strings.Join(cfg.Args, " "), cfg.ID)
|
2017-10-02 01:24:50 +08:00
|
|
|
err := cmd.Run()
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|