20
vendor/github.com/mholt/caddy/onevent/hook/config.go
generated
vendored
Normal file
20
vendor/github.com/mholt/caddy/onevent/hook/config.go
generated
vendored
Normal file
@@ -0,0 +1,20 @@
|
||||
package hook
|
||||
|
||||
import (
|
||||
"github.com/mholt/caddy"
|
||||
)
|
||||
|
||||
// Config describes how Hook should be configured and used.
|
||||
type Config struct {
|
||||
ID string
|
||||
Event caddy.EventName
|
||||
Command string
|
||||
Args []string
|
||||
}
|
||||
|
||||
// SupportedEvents is a map of supported events.
|
||||
var SupportedEvents = map[string]caddy.EventName{
|
||||
"startup": caddy.InstanceStartupEvent,
|
||||
"shutdown": caddy.ShutdownEvent,
|
||||
"certrenew": caddy.CertRenewEvent,
|
||||
}
|
||||
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
|
||||
}
|
||||
68
vendor/github.com/mholt/caddy/onevent/on.go
generated
vendored
Normal file
68
vendor/github.com/mholt/caddy/onevent/on.go
generated
vendored
Normal file
@@ -0,0 +1,68 @@
|
||||
package onevent
|
||||
|
||||
import (
|
||||
"strings"
|
||||
|
||||
"github.com/google/uuid"
|
||||
"github.com/mholt/caddy"
|
||||
"github.com/mholt/caddy/onevent/hook"
|
||||
)
|
||||
|
||||
func init() {
|
||||
// Register Directive.
|
||||
caddy.RegisterPlugin("on", caddy.Plugin{Action: setup})
|
||||
}
|
||||
|
||||
func setup(c *caddy.Controller) error {
|
||||
config, err := onParse(c)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
// Register Event Hooks.
|
||||
c.OncePerServerBlock(func() error {
|
||||
for _, cfg := range config {
|
||||
caddy.RegisterEventHook("on-"+cfg.ID, cfg.Hook)
|
||||
}
|
||||
return nil
|
||||
})
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
func onParse(c *caddy.Controller) ([]*hook.Config, error) {
|
||||
var config []*hook.Config
|
||||
|
||||
for c.Next() {
|
||||
cfg := new(hook.Config)
|
||||
|
||||
if !c.NextArg() {
|
||||
return config, c.ArgErr()
|
||||
}
|
||||
|
||||
// Configure Event.
|
||||
event, ok := hook.SupportedEvents[strings.ToLower(c.Val())]
|
||||
if !ok {
|
||||
return config, c.Errf("Wrong event name or event not supported: '%s'", c.Val())
|
||||
}
|
||||
cfg.Event = event
|
||||
|
||||
// Assign an unique ID.
|
||||
cfg.ID = uuid.New().String()
|
||||
|
||||
args := c.RemainingArgs()
|
||||
|
||||
// Extract command and arguments.
|
||||
command, args, err := caddy.SplitCommandAndArgs(strings.Join(args, " "))
|
||||
if err != nil {
|
||||
return config, c.Err(err.Error())
|
||||
}
|
||||
|
||||
cfg.Command = command
|
||||
cfg.Args = args
|
||||
|
||||
config = append(config, cfg)
|
||||
}
|
||||
|
||||
return config, nil
|
||||
}
|
||||
Reference in New Issue
Block a user