File: hook.go

package info (click to toggle)
golang-github-coredns-caddy 1.1.1-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 360 kB
  • sloc: makefile: 2
file content (41 lines) | stat: -rw-r--r-- 887 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
package hook

import (
	"log"
	"os"
	"os/exec"
	"strings"

	"github.com/coredns/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
}