generated from xmidt-org/.go-template
-
Notifications
You must be signed in to change notification settings - Fork 1
/
action.go
81 lines (67 loc) · 1.8 KB
/
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
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
package main
import (
"errors"
"os"
"os/exec"
"strings"
"go.uber.org/fx"
)
var (
// ErrEmptyCommand is returned by ParseExec to indicate that an exec action
// was blank or had a blank command path.
ErrEmptyCommand = errors.New("A non-empty command is required")
)
// Action represents something that will trigger unless postponed.
// The type *os/exec.Cmd implements this interface.
type Action interface {
String() string
Run() error
}
// Trigger executes each action in sequence, providing a standard output
// format for each action.
func Trigger(l Logger, actions ...Action) {
for _, a := range actions {
l.Printf("[%s]", a.String())
if err := a.Run(); err != nil {
l.Printf("action error: %s", err)
}
}
}
// ParseExec parses the executable actions from a command line.
func ParseExec(cl CommandLine) ([]Action, error) {
actions := make([]Action, 0, len(cl.Exec))
for _, e := range cl.Exec {
pieces := strings.Split(e, " ")
if len(pieces) == 0 || len(pieces[0]) == 0 {
return nil, ErrEmptyCommand
}
cmd := exec.Command(pieces[0], pieces[1:]...)
cmd.Dir = cl.Dir
cmd.Stdout = os.Stdout
cmd.Stderr = os.Stderr
actions = append(actions, cmd)
}
return actions, nil
}
// ShutdownerAction allows an uber/fx.Shutdowner to be used as an Action.
// This type is used to ensure that after trigger actions, the process exits.
type ShutdownerAction struct {
Shutdowner fx.Shutdowner
}
func (sa ShutdownerAction) String() string {
return "Shutdowner"
}
func (sa ShutdownerAction) Run() error {
return sa.Shutdowner.Shutdown()
}
func provideActions() fx.Option {
return fx.Provide(
func(cl CommandLine, s fx.Shutdowner) (actions []Action, err error) {
actions, err = ParseExec(cl)
if err == nil {
actions = append(actions, ShutdownerAction{Shutdowner: s})
}
return
},
)
}