This repository has been archived by the owner on Jul 7, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 13
/
options.go
85 lines (70 loc) · 2.15 KB
/
options.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
82
83
84
85
package supervisor
import (
"bytes"
"text/template"
"time"
"github.com/Masterminds/sprig"
)
// Options exposes settings to create a process supervisor
type Options struct {
Replicas int
Command string
Args []string
Dir string
Env []string
RedirectStdout string
RedirectStderr string
RestartPolicy RestartPolicy
TerminationGracePeriod time.Duration
}
// CreateOptions createnew SupervisorOptions with default settings
func CreateOptions() *Options {
return &Options{
Replicas: 1,
TerminationGracePeriod: 10 * time.Second,
}
}
// RestartPolicy determines when a supervised process should be restarted
type RestartPolicy string
const (
// RestartNever indicates to never restart the process
RestartNever = RestartPolicy("never")
// RestartOnFailure indicates to only restart the process after failures
RestartOnFailure = RestartPolicy("on_failure")
// RestartAlways indicates to always restart the process
RestartAlways = RestartPolicy("always")
)
func (options *Options) processTemplates(data *TemplateData) *Options {
result := Options{}
result.Command = processTemplates(options.Command, data)
result.Args = make([]string, len(options.Args))
for i, arg := range options.Args {
result.Args[i] = processTemplates(arg, data)
}
result.Dir = processTemplates(options.Dir, data)
result.Env = make([]string, len(options.Env))
for i, env := range options.Env {
result.Env[i] = processTemplates(env, data)
}
result.RedirectStdout = options.RedirectStdout
result.RedirectStderr = options.RedirectStderr
result.RestartPolicy = options.RestartPolicy
result.TerminationGracePeriod = options.TerminationGracePeriod
return &result
}
// TemplateData contains data to be accessed from templates
type TemplateData struct {
Replica int
}
func processTemplates(text string, data interface{}) string {
tmpl, err := template.New("test").Funcs(sprig.TxtFuncMap()).Parse(text)
if err != nil {
return err.Error()
}
var writer bytes.Buffer
err = tmpl.Execute(&writer, data)
if err != nil {
return err.Error()
}
return writer.String()
}