-
Notifications
You must be signed in to change notification settings - Fork 12
/
Copy pathserver_sync_filesystem_stubs.go
60 lines (52 loc) · 1.53 KB
/
server_sync_filesystem_stubs.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
package sync
import (
"errors"
"bufio"
"strings"
"runtime"
"path"
"path/filepath"
"github.com/webdevops/go-shell"
"github.com/webdevops/go-stubfilegenerator"
"github.com/remeh/sizedwaitgroup"
)
// General sync
func (filesystem *Filesystem) SyncStubs() {
switch filesystem.Connection.GetInstance().GetType() {
case "local":
fallthrough
case "ssh":
filesystem.generateStubs()
case "docker":
errors.New("Docker not supported")
}
}
// Sync filesystem using rsync
func (filesystem *Filesystem) generateStubs() {
connection := filesystem.Connection.GetInstance()
cmd := shell.Cmd(connection.CommandBuilder("find", filesystem.Path, "-type", "f")...)
output := cmd.Run().Stdout.String()
removeBasePath := filesystem.Path
localBasePath := filesystem.localPath()
// build list and filter it by user filter list
fileList := []string{}
scanner := bufio.NewScanner(strings.NewReader(output))
for scanner.Scan() {
fileList = append(fileList, strings.TrimPrefix(scanner.Text(), removeBasePath))
}
fileList = filesystem.Filter.ApplyFilter(fileList)
// generate stubs
swg := sizedwaitgroup.New(runtime.GOMAXPROCS(0) * 10)
stubGen := stubfilegenerator.NewStubGenerator()
for _, filePath := range fileList {
swg.Add()
go func(filePath string, stubGen stubfilegenerator.StubGenerator) {
defer swg.Done()
localPath := path.Join(localBasePath, filePath)
localAbsPath, _ := filepath.Abs(localPath)
stubGen.TemplateVariables["PATH"] = localPath
stubGen.Generate(localAbsPath)
} (filePath, stubGen.Clone())
swg.Wait()
}
}