mirror of
https://github.com/cloudflare/cloudflared.git
synced 2025-07-29 16:30:22 +00:00
TUN-528: Move cloudflared into a separate repo
This commit is contained in:
41
vendor/github.com/mholt/caddy/onevent/hook/hook.go
generated
vendored
Normal file
41
vendor/github.com/mholt/caddy/onevent/hook/hook.go
generated
vendored
Normal file
@@ -0,0 +1,41 @@
|
||||
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
|
||||
if len(cfg.Args) >= 1 && cfg.Args[len(cfg.Args)-1] == "&" {
|
||||
// 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 {
|
||||
log.Printf("[INFO] Nonblocking Command \"%s %s\" with ID %s", cfg.Command, strings.Join(cfg.Args, " "), cfg.ID)
|
||||
return cmd.Start()
|
||||
}
|
||||
log.Printf("[INFO] Blocking Command \"%s %s\" with ID %s", cfg.Command, strings.Join(cfg.Args, " "), cfg.ID)
|
||||
err := cmd.Run()
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
Reference in New Issue
Block a user