-
-
Notifications
You must be signed in to change notification settings - Fork 10
/
rel-script.go
164 lines (140 loc) · 2.92 KB
/
rel-script.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
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
package main
import (
"fmt"
"io"
"log"
"os"
"os/exec"
"path"
"sort"
"strings"
"text/template"
"github.com/otiai10/copy"
)
type relScript struct {
Description string
Remove []bomLine
Add []bomLine
Copy []string
Hooks []string
Required []string
}
func (rs *relScript) processBom(b bom) (bom, error) {
ret := b
for _, r := range rs.Remove {
if r.CmpName != "" {
retM := bom{}
for _, l := range ret {
if l.CmpName != r.CmpName {
retM = append(retM, l)
}
}
ret = retM
}
if r.Ref != "" {
retM := bom{}
for _, l := range ret {
l.removeRef(r.Ref)
if l.Qnty > 0 {
retM = append(retM, l)
}
}
ret = retM
}
}
for _, a := range rs.Add {
refs := strings.Split(a.Ref, ",")
a.Qnty = len(refs)
if a.Qnty < 0 {
a.Qnty = 1
}
// for some reason we need to make a copy or it
// will alias the last one
c := a
ret = append(ret, &c)
}
sort.Sort(ret)
return ret, nil
}
func (rs *relScript) copy(srcDir, destDir string) error {
for _, c := range rs.Copy {
opts := copy.Options{
OnSymlink: func(src string) copy.SymlinkAction {
return copy.Deep
},
OnDirExists: func(src, dest string) copy.DirExistsAction {
return copy.Replace
},
}
srcPath := path.Join(srcDir, c)
destPath := path.Join(destDir, c)
err := copy.Copy(srcPath, destPath, opts)
if err != nil {
return err
}
log.Printf("%v copied to release dir\n", c)
}
return nil
}
func (rs *relScript) hooks(pn string, srcDir, destDir string) error {
data := struct {
SrcDir string
RelDir string
IPN string
}{
SrcDir: srcDir,
RelDir: destDir,
IPN: pn,
}
for _, h := range rs.Hooks {
t, err := template.New("hook").Parse(h)
if err != nil {
return fmt.Errorf("Error parsing hook: %v: %v", h, err)
}
var out strings.Builder
err = t.Execute(&out, data)
if err != nil {
return fmt.Errorf("Error parsing hook: %v: %v", h, err)
}
cmd := exec.Command("/bin/sh", "-c", out.String())
stdout, err := cmd.StdoutPipe()
if err != nil {
log.Fatal(err)
}
stderr, err := cmd.StderrPipe()
if err != nil {
log.Fatal(err)
}
// Start the command
if err := cmd.Start(); err != nil {
log.Fatal(err)
}
// Copy the command's stdout and stderr to the Go program's stdout
go func() {
_, _ = io.Copy(os.Stdout, stdout)
}()
go func() {
_, _ = io.Copy(os.Stderr, stderr)
}()
// Wait for the command to exit
if err := cmd.Wait(); err != nil {
log.Println("Error running hook: ", err)
log.Println("Hook contents: ")
fmt.Print(out.String())
}
}
return nil
}
func (rs *relScript) required(destDir string) error {
for _, r := range rs.Required {
p := path.Join(destDir, r)
e, err := exists(p)
if err != nil {
return fmt.Errorf("Error looking for required file: %v: %v", p, err)
}
if !e {
return fmt.Errorf("Required file does not exist, please generate it: %v", p)
}
}
return nil
}