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
|
package procs
import (
"os"
"strings"
)
// Builder helps construct commands using templates.
type Builder struct {
Context map[string]string
Templates []string
}
func (b *Builder) getConfig(ctx map[string]string) func(string) string {
return func(key string) string {
if v, ok := ctx[key]; ok {
return v
}
return ""
}
}
func (b *Builder) expand(v string, ctx map[string]string) string {
return os.Expand(v, b.getConfig(ctx))
}
// Command returns the result of the templates as a single string.
func (b *Builder) Command() string {
parts := []string{}
for _, t := range b.Templates {
parts = append(parts, b.expand(t, b.Context))
}
return strings.Join(parts, " ")
}
// CommandContext returns the result of the templates as a single
// string, but allows providing an environment context as a
// map[string]string for expansions.
func (b *Builder) CommandContext(ctx map[string]string) string {
// Build our environment context by starting with our Builder
// context and overlay the passed in context map.
env := make(map[string]string)
for k, v := range b.Context {
env[k] = b.expand(v, b.Context)
}
for k, v := range ctx {
env[k] = b.expand(v, env)
}
parts := []string{}
for _, t := range b.Templates {
parts = append(parts, b.expand(t, env))
}
return strings.Join(parts, " ")
}
|