forked from jstemmer/rehook
/
execute_action.go
61 lines (49 loc) · 1.67 KB
/
execute_action.go
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
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
package main
import (
"errors"
"fmt"
"log"
"os/exec"
"github.com/boltdb/bolt"
)
func init() {
RegisterComponent("execute-action", ExecuteAction{})
}
// ExecuteAction is a component that executes a command.
type ExecuteAction struct {
}
// Name returns the name of this component.
func (ExecuteAction) Name() string { return "Execute Command" }
// Template returns the HTML template name of this component.
func (ExecuteAction) Template() string { return "execute-action" }
// Params returns the currently stored configuration parameters for hook h
// from bucket b.
func (ExecuteAction) Params(h Hook, b *bolt.Bucket) map[string]string {
m := make(map[string]string)
for _, k := range []string{"command"} {
m[k] = string(b.Get([]byte(fmt.Sprintf("%s-%s", h.ID, k))))
}
return m
}
// Init initializes this component. It requires a command to be present.
func (ExecuteAction) Init(h Hook, params map[string]string, b *bolt.Bucket) error {
command, ok := params["command"]
if !ok {
return errors.New("command is required")
}
return b.Put([]byte(fmt.Sprintf("%s-command", h.ID)), []byte(command))
}
// Process executes command and logs the output and errors
func (ExecuteAction) Process(h Hook, r Request, b *bolt.Bucket) error {
command := b.Get([]byte(fmt.Sprintf("%s-command", h.ID)))
if command == nil {
return errors.New("forward request action not initialized")
}
out, err := exec.Command("sh", "-c", string(command)).CombinedOutput()
log.Printf("[command-action][%s] executing command: %s\n --- STARTOUTPUT --- \n %s \n --- ENDOUTPUT ---", h.ID, command, out)
if err != nil {
log.Printf("[command-action][%s] error: %s", h.ID, err)
return err
}
return nil
}