-
Notifications
You must be signed in to change notification settings - Fork 29
/
Copy pathinject.go
59 lines (53 loc) · 1.28 KB
/
inject.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
package chartify
import (
"fmt"
"strings"
)
type InjectOpts struct {
injectors []string
injects []string
}
func (r *Runner) Inject(files []string, o InjectOpts) error {
var flagsTemplate string
for _, inj := range o.injectors {
tokens := strings.Split(inj, ",")
injector := tokens[0]
injectFlags := tokens[1:]
for _, flag := range injectFlags {
flagSplit := strings.Split(flag, "=")
switch len(flagSplit) {
case 1:
flagsTemplate += flagSplit[0]
case 2:
key, val := flagSplit[0], flagSplit[1]
flagsTemplate += createFlagChain(key, []string{val})
default:
return fmt.Errorf("inject-flags must be in the form of key1=value1[,key2=value2,...]: %v", flag)
}
}
for _, file := range files {
flags := strings.Replace(flagsTemplate, "FILE", file, 1)
command := fmt.Sprintf("%s %s", injector, flags)
stdout, err := r.runBytes("", command)
if err != nil {
return err
}
if err := r.WriteFile(file, stdout, 0644); err != nil {
return err
}
}
}
for _, tmpl := range o.injects {
for _, file := range files {
cmd := strings.Replace(tmpl, "FILE", file, 1)
stdout, err := r.runBytes("", cmd)
if err != nil {
return err
}
if err := r.WriteFile(file, stdout, 0644); err != nil {
return err
}
}
}
return nil
}