/
util.go
402 lines (377 loc) · 9.05 KB
/
util.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
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
package main
import (
"bytes"
"errors"
"fmt"
"io"
"math/rand"
"path/filepath"
"strings"
"time"
"github.com/tillberg/alog"
"github.com/tillberg/bismuth"
"github.com/tillberg/stringset"
"github.com/tillberg/watcher"
)
func WindowsPathToMingwPath(p string) string {
panic("XXX Does this work?")
// XXX path/filepath might be better for this sort of thing
p = strings.Replace(p, ":", "", 1)
p = strings.Replace(p, "\\", "/", -1)
return "/" + p
}
const defaultNumCores = "4"
func (ctx *SyncContext) GetNumCores() string {
if ctx.IsWindows() {
out, err := ctx.Output("wmic", "CPU", "Get", "NumberOfLogicalProcessors", "/Format:List")
if err != nil {
return defaultNumCores
}
out = strings.TrimSpace(out)
parts := strings.Split(out, "=")
return parts[len(parts)-1]
} else {
out, err := ctx.Output("getconf", "_NPROCESSORS_ONLN")
if err != nil {
return defaultNumCores
}
return strings.TrimSpace(out)
}
}
func FindOpenPorts(numPorts int, ctxs ...*SyncContext) ([]int, error) {
if numPorts == 0 {
return []int{}, nil
}
var netstatsBuf bytes.Buffer
for _, ctx := range ctxs {
opt := "-anl"
if ctx.IsWindows() {
opt = "-an"
}
output, err := ctx.Output("netstat", opt)
if err != nil {
return nil, err
}
netstatsBuf.WriteString(output)
netstatsBuf.WriteString(" ")
}
netstats := netstatsBuf.String()
randomPorts := []int{}
for _, n := range rand.Perm(MaxRandomPort - MinRandomPort) {
randomPorts = append(randomPorts, MinRandomPort+n)
}
ports := []int{}
for _, port := range randomPorts {
portStr := fmt.Sprintf("%d", port)
if !strings.Contains(netstats, portStr) {
ports = append(ports, port)
if len(ports) == numPorts {
return ports, nil
}
}
}
return nil, errors.New("Not enough available ports found")
}
func (ctx *SyncContext) GetCmd(commands ...string) string {
for _, command := range commands {
_, _, retCode, err := ctx.Run("which", command)
if err == nil && retCode == 0 {
return command
}
}
return ""
}
var letters = []rune("abcdefghijklmnopqrstuvwxyz")
func RandSeq(n int) string {
b := make([]rune, n)
for i := range b {
b[i] = letters[rand.Intn(len(letters))]
}
return string(b)
}
var inotifyChangeEvents = []string{"modify", "attrib", "move", "create", "delete"}
func inotifyArgs(ctx *SyncContext, monitor bool) []string {
args := []string{
"inotifywait",
"--quiet",
"--recursive",
}
if monitor {
args = append(args, "--monitor")
formatStr := "%w%f"
excludeStr := "\\.gut/"
if ctx.IsWindows() {
// inotify-win has slightly different semantics (and a completely different regex engine) than inotify-tools
formatStr = "%w\\%f"
excludeStr = "\\.gut($|\\\\)"
}
args = append(args, "--format", formatStr)
args = append(args, "--exclude", excludeStr)
}
if ctx.IsWindows() {
args = append(args, "--event", strings.Join(inotifyChangeEvents, ","))
} else {
for _, event := range inotifyChangeEvents {
args = append(args, "--event", event)
}
}
args = append(args, ".")
return args
}
var bytesNewline = []byte{'\n'}
type LineBuf struct {
lineCallback func([]byte)
buf []byte
}
func (m *LineBuf) Write(b []byte) (int, error) {
m.buf = append(m.buf, b...)
for {
indexNewline := bytes.Index(m.buf, bytesNewline)
if indexNewline < 0 {
break
}
line := m.buf[:indexNewline]
if indexNewline == len(m.buf)-1 {
m.buf = m.buf[:0]
} else {
m.buf = m.buf[indexNewline+1:]
}
m.lineCallback(line)
}
return len(b), nil
}
func (m *LineBuf) Close() error {
if len(m.buf) > 0 {
m.Write(bytesNewline)
}
return nil
}
func NewLineBuf(lineCallback func([]byte)) *LineBuf {
return &LineBuf{lineCallback, []byte{}}
}
func (ctx *SyncContext) WatchedRoot() string {
var watchedRoot string
var err error
if ctx.IsWindows() {
watchedRoot, err = ctx.OutputCwd(ctx.AbsSyncPath(), "cmd", "/c", "cd ,")
} else {
watchedRoot, err = ctx.OutputCwd(ctx.AbsSyncPath(), "pwd", "-P")
}
if err != nil {
alog.Bail(err)
}
return watchedRoot
}
func (ctx *SyncContext) WatchForChanges(fileEventCallback func(string)) {
if ctx.IsLocal() {
ctx.watchForChangesLocal(fileEventCallback)
return
}
watchType := ctx.GetCmd("inotifywait", "fswatch")
status := ctx.NewLogger(watchType)
args := []string{}
if watchType == "inotifywait" {
args = inotifyArgs(ctx, true)
} else if watchType == "fswatch" {
args = []string{"fswatch", "."}
} else {
if ctx.IsDarwin() {
Shutdown("missing fswatch", 1)
} else {
Shutdown("missing inotifywait", 1)
}
}
watchedRoot := ctx.WatchedRoot()
ctx.KillViaPidfile(watchType)
isFirstTime := true
firstTimeChan := make(chan error)
go func() {
var err error
for {
if !isFirstTime {
time.Sleep(2 * time.Second)
err = ctx.KillViaPidfile(watchType)
if err == bismuth.NotConnectedError {
continue
}
if err != nil {
status.Printf("Error killing previous %s process via pidfile: %v\n", watchType, err)
continue
}
}
buf := NewLineBuf(func(b []byte) {
// status.Printf("Received: %q\n", string(b))
p := string(b)
if !filepath.IsAbs(p) {
p = filepath.Join(watchedRoot, p)
}
relPath, err := filepath.Rel(watchedRoot, p)
if err != nil {
status.Bail(err)
}
// status.Printf("relPath: %q from %q\n", relPath, watchedRoot)
fileEventCallback(relPath)
})
chanStdout := make(chan io.Reader)
go func() {
stdout := <-chanStdout
_, err := io.Copy(buf, stdout)
if err != nil {
status.Printf("Error copying from stdout to buf: %s\n", err)
}
}()
pid, retCodeChan, err := ctx.QuoteDaemonCwdPipeOut(watchType, watchedRoot, chanStdout, args...)
if err != nil {
if isFirstTime {
firstTimeChan <- err
return
}
status.Printf("Error starting %s: %v\n", watchType, err)
continue
}
err = ctx.SaveDaemonPid(watchType, pid)
if err != nil {
if isFirstTime {
firstTimeChan <- err
return
}
status.Printf("Error saving PID for %s: %v\n", watchType, err)
continue
}
if isFirstTime {
firstTimeChan <- err
if err != nil {
return
}
isFirstTime = false
}
if err != nil {
status.Printf("Error starting %s: %v\n", watchType, err)
} else {
ctx.Logger().Printf("@(dim:%s started, watching )%s@(dim::)@(path:%s)@(dim:.)\n", watchType, ctx.NameAnsi(), watchedRoot)
<-retCodeChan
ctx.Logger().Printf("@(dim:%s exited.)\n", watchType)
}
}
}()
err := <-firstTimeChan
if err != nil {
status.Bail(err)
}
}
func (ctx *SyncContext) watchForChangesLocal(fileEventCallback func(string)) {
status := ctx.NewLogger("watcher")
listener := watcher.NewListener()
listener.Path = ctx.syncPath
listener.IgnorePart = stringset.New(".gut")
err := listener.Start()
status.BailIf(err)
go func() {
for pathEvent := range listener.NotifyChan {
path := pathEvent.Path
relPath, err := filepath.Rel(ctx.syncPath, path)
if err != nil {
status.Bail(err)
}
fileEventCallback(relPath)
}
}()
}
const GutHashDisplayChars = 7
func TrimCommit(commit string) string {
if len(commit) > GutHashDisplayChars {
return commit[:GutHashDisplayChars]
}
return commit
}
func (ctx *SyncContext) AssertSyncFolderIsEmpty() (err error) {
p := ctx.AbsSyncPath()
bail := func() {
ctx.Logger().Printf("@(error:Refusing to initialize) @(path:%s) @(error:on) %s ", p, ctx.NameAnsi())
ctx.Logger().Printf("@(error:as it is not an empty directory.)\n")
ctx.Logger().Fatalf("@(error:Move or delete it manually first, the try running gut-sync again.)\n")
}
fileInfo, err := ctx.Stat(p)
if err == bismuth.NotFoundError {
return nil
}
if err != nil {
bail()
}
if !fileInfo.IsDir() {
bail()
}
out, err := ctx.Output("ls", "-A", ctx.AbsSyncPath())
if err != nil {
bail()
}
if len(strings.TrimSpace(out)) > 0 {
bail()
}
return nil
}
func CommonPathPrefix(paths ...string) string {
if len(paths) == 0 {
return ""
}
common := paths[0]
for _, path := range paths[1:] {
for len(path) < len(common) || path[:len(common)] != common {
if common[len(common)-1] == '/' {
// Lop off the trailing slash, if there is one
common = common[:len(common)-1]
} else {
lastIndex := strings.LastIndex(common, "/")
if lastIndex == -1 {
// There is no common prefix
return ""
} else {
// Lop off everything after the last slash
common = common[:lastIndex+1]
}
}
}
}
return common
}
func JoinWithAndAndCommas(strs ...string) string {
if len(strs) == 0 {
return ""
}
var buf bytes.Buffer
buf.WriteString(strs[0])
if len(strs) > 1 {
commaStr := alog.Colorify("@(dim:, )")
andStr := alog.Colorify("@(dim:and )")
strs = strs[1:]
for i, str := range strs {
if len(strs) >= 2 {
buf.WriteString(commaStr)
}
if i == len(strs)-1 {
if len(strs) < 2 {
buf.WriteString(" ")
}
buf.WriteString(andStr)
}
buf.WriteString(str)
}
}
return buf.String()
}
func IsGitCommand(s string) bool {
for _, a := range AllGutCommands {
if a == s {
return true
}
}
return false
}
func IsDangerousGitCommand(s string) bool {
for _, a := range DangerousGitCommands {
if a == s {
return true
}
}
return false
}