Skip to content

Commit 0ea647a

Browse files
committed
Implement file stub generator
1 parent d540c03 commit 0ea647a

File tree

6 files changed

+105
-2
lines changed

6 files changed

+105
-2
lines changed

examples/gosync.yml

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,12 @@ sync:
3333
- .git
3434
- node_modules/
3535

36+
## Create file stubs for /tmp/remote/other/path to ./other/
37+
- path: /tmp/remote/other/path-big-files
38+
local: ./other-stubs/
39+
options:
40+
generate-stubs: true
41+
3642
#########################
3743
## Database
3844
#########################

sync/config.go

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,9 @@ type Filesystem struct {
1919
Path string
2020
Local string
2121
Filter Filter
22+
Options struct {
23+
GenerateStubs bool `yaml:"generate-stubs"`
24+
}
2225
}
2326

2427

sync/filter.go

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,30 @@ func (filter *Filter) compile() {
2020
}
2121
}
2222

23+
func (filter *Filter) ApplyFilter(lines []string) []string {
24+
filter.compile()
25+
26+
if len(filter.Include) > 0 {
27+
lines = filter.calculateMatching(filter.includeRegexp, lines)
28+
}
29+
30+
if len(filter.Exclude) > 0 {
31+
excludes := filter.calculateMatching(filter.excludeRegexp, lines)
32+
33+
tmp := []string{}
34+
for _, line := range lines {
35+
if ! stringInSlice(line, excludes) {
36+
tmp = append(tmp, line)
37+
}
38+
}
39+
40+
lines = tmp
41+
}
42+
43+
return lines
44+
}
45+
46+
2347
func (filter *Filter) CalcExcludes(lines []string) []string {
2448
filter.compile()
2549
return filter.calculateMatching(filter.excludeRegexp, lines)
@@ -43,3 +67,12 @@ func (filter *Filter) calculateMatching(regexpList []*regexp.Regexp, lines []str
4367

4468
return ret
4569
}
70+
71+
func stringInSlice(a string, list []string) bool {
72+
for _, b := range list {
73+
if b == a {
74+
return true
75+
}
76+
}
77+
return false
78+
}

sync/server_deploy.go

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,13 @@ func (server *Server) Deploy() {
1515
}
1616

1717
func (server *Server) DeployFilesystem() {
18+
// check for generate-stubs option (not allowed)
19+
for _, filesystem := range server.Filesystem {
20+
if filesystem.Options.GenerateStubs {
21+
Logger.FatalExit(2, "Generate Stubs is not allowed for deployment")
22+
}
23+
}
24+
1825
for _, filesystem := range server.Filesystem {
1926
Logger.Main("Starting deploy of %s", filesystem.String(server, "deploy"))
2027
filesystem.Deploy(server)

sync/server_sync.go

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,8 +16,13 @@ func (server *Server) Sync() {
1616

1717
func (server *Server) SyncFilesystem() {
1818
for _, filesystem := range server.Filesystem {
19-
Logger.Main("Starting sync of %s", filesystem.String(server, "sync"))
20-
filesystem.Sync(server)
19+
if filesystem.Options.GenerateStubs {
20+
Logger.Main("Starting stub generator for %s", filesystem.String(server, "sync"))
21+
filesystem.SyncStubs(server)
22+
} else {
23+
Logger.Main("Starting sync of %s", filesystem.String(server, "sync"))
24+
filesystem.Sync(server)
25+
}
2126
}
2227
}
2328

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
package sync
2+
3+
import (
4+
"errors"
5+
"bufio"
6+
"strings"
7+
"path"
8+
"path/filepath"
9+
"github.com/webdevops/go-shell"
10+
"github.com/webdevops/go-stubfilegenerator"
11+
)
12+
13+
// General sync
14+
func (filesystem *Filesystem) SyncStubs(server *Server) {
15+
switch server.Connection.GetType() {
16+
case "ssh":
17+
filesystem.generateStubs(server)
18+
case "docker":
19+
errors.New("Docker not supported")
20+
}
21+
}
22+
23+
// Sync filesystem using rsync
24+
func (filesystem *Filesystem) generateStubs(server *Server) {
25+
cmd := shell.Cmd(server.Connection.CommandBuilder("find", filesystem.Path, "-type f")...)
26+
output := cmd.Run().Stdout.String()
27+
28+
removeBasePath := filesystem.Path
29+
localBasePath := filesystem.localPath(server)
30+
31+
// build list and filter it by user filter list
32+
fileList := []string{}
33+
scanner := bufio.NewScanner(strings.NewReader(output))
34+
for scanner.Scan() {
35+
fileList = append(fileList, strings.TrimPrefix(scanner.Text(), removeBasePath))
36+
}
37+
fileList = filesystem.Filter.ApplyFilter(fileList)
38+
39+
40+
// generate stubs
41+
stubGen := stubfilegenerator.StubGenerator()
42+
for _, filePath := range fileList {
43+
localPath := path.Join(localBasePath, filePath)
44+
45+
stubGen.TemplateVariables["PATH"] = localPath
46+
localAbsPath, _ := filepath.Abs(localPath)
47+
stubGen.GenerateStub(localAbsPath)
48+
}
49+
}

0 commit comments

Comments
 (0)