Skip to content

Commit 966bb85

Browse files
committed
Refactor connection handling
Setting connection if connection is empty and not passign server around
1 parent 0ea647a commit 966bb85

11 files changed

+95
-59
lines changed

sync/config.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ type Filesystem struct {
1919
Path string
2020
Local string
2121
Filter Filter
22+
Connection commandbuilder.Connection
2223
Options struct {
2324
GenerateStubs bool `yaml:"generate-stubs"`
2425
}
@@ -52,7 +53,7 @@ type Database struct {
5253
cacheRemoteTableList []string
5354
cacheLocalTableList []string
5455

55-
remoteConnection commandbuilder.Connection
56+
Connection commandbuilder.Connection
5657
}
5758

5859
type Execution struct {

sync/database.go

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,12 @@ import (
77
"github.com/webdevops/go-shell/commandbuilder"
88
)
99

10+
func (database *Database) ApplyDefaults(server *Server) {
11+
// set default connection if not set
12+
if (commandbuilder.Connection{}) == database.Connection {
13+
database.Connection = server.Connection
14+
}
15+
}
1016

1117
func (database *Database) mysqlTableFilter(connection *commandbuilder.Connection, connectionType string) ([]string, []string) {
1218
var exclude []string

sync/database_remote.go

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ func (database *Database) remoteMysqldumpCmdBuilder(additionalArgs []string, use
2929
}
3030

3131
// exclude
32-
excludeArgs, includeArgs := database.mysqlTableFilter(&database.remoteConnection, "remote");
32+
excludeArgs, includeArgs := database.mysqlTableFilter(&database.Connection, "remote");
3333
if useFilter && len(excludeArgs) > 0 {
3434
args = append(args, excludeArgs...)
3535
}
@@ -46,7 +46,7 @@ func (database *Database) remoteMysqldumpCmdBuilder(additionalArgs []string, use
4646
cmd = append(cmd, shell.QuoteValues(args...)...)
4747
cmd = append(cmd, "|", "gzip", "--stdout")
4848

49-
return database.remoteConnection.RawShellCommandBuilder(cmd...)
49+
return database.Connection.RawShellCommandBuilder(cmd...)
5050
}
5151

5252
func (database *Database) remoteMysqlCmdBuilder(args ...string) []interface{} {
@@ -72,7 +72,7 @@ func (database *Database) remoteMysqlCmdBuilder(args ...string) []interface{} {
7272
args = append(args, database.Schema)
7373
}
7474

75-
return database.remoteConnection.CommandBuilder("mysql", args...)
75+
return database.Connection.CommandBuilder("mysql", args...)
7676
}
7777

7878

@@ -101,5 +101,5 @@ func (database *Database) remoteMysqlCmdBuilderUncompress(args ...string) []inte
101101

102102
cmd := []string{"gunzip", "--stdout", "|", "mysql", strings.Join(shell.QuoteValues(args...), " ")}
103103

104-
return database.remoteConnection.RawShellCommandBuilder(cmd...)
104+
return database.Connection.RawShellCommandBuilder(cmd...)
105105
}

sync/filesystem.go

Lines changed: 17 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -3,26 +3,35 @@ package sync
33
import (
44
"fmt"
55
"strings"
6+
"github.com/webdevops/go-shell/commandbuilder"
67
)
78

8-
func (filesystem *Filesystem) localPath(server *Server) string {
9-
if filesystem.Local != "" {
10-
return filesystem.Local
11-
} else {
12-
return server.GetLocalPath()
9+
func (filesystem *Filesystem) ApplyDefaults(server *Server) {
10+
// set default connection if not set
11+
if (commandbuilder.Connection{}) == filesystem.Connection {
12+
filesystem.Connection = server.Connection
1313
}
14+
15+
// set default path
16+
if filesystem.Local == "" {
17+
filesystem.Local = server.GetLocalPath()
18+
}
19+
}
20+
21+
func (filesystem *Filesystem) localPath() string {
22+
return filesystem.Local
1423
}
1524

16-
func (filesystem *Filesystem) String(server *Server, direction string) string {
25+
func (filesystem *Filesystem) String(direction string) string {
1726
var parts []string
1827

1928
switch direction {
2029
case "sync":
2130
parts = append(parts, fmt.Sprintf("Path:%s", filesystem.Path))
2231
parts = append(parts, "->")
23-
parts = append(parts, fmt.Sprintf("Local:%s", filesystem.localPath(server)))
32+
parts = append(parts, fmt.Sprintf("Local:%s", filesystem.localPath()))
2433
case "deploy":
25-
parts = append(parts, fmt.Sprintf("Local:%s", filesystem.localPath(server)))
34+
parts = append(parts, fmt.Sprintf("Local:%s", filesystem.localPath()))
2635
parts = append(parts, "->")
2736
parts = append(parts, fmt.Sprintf("Path:%s", filesystem.Path))
2837
}

sync/server_deploy.go

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -23,14 +23,17 @@ func (server *Server) DeployFilesystem() {
2323
}
2424

2525
for _, filesystem := range server.Filesystem {
26-
Logger.Main("Starting deploy of %s", filesystem.String(server, "deploy"))
27-
filesystem.Deploy(server)
26+
filesystem.ApplyDefaults(server)
27+
28+
Logger.Main("Starting deploy of %s", filesystem.String( "deploy"))
29+
filesystem.Deploy()
2830
}
2931
}
3032

3133
func (server *Server) DeployDatabases() {
3234
for _, database := range server.Database {
35+
database.ApplyDefaults(server)
3336
Logger.Main("Starting deploy of %s", database.String("deploy"))
34-
database.Deploy(server)
37+
database.Deploy()
3538
}
3639
}

sync/server_deploy_database.go

Lines changed: 7 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -5,19 +5,17 @@ import (
55
"fmt"
66
)
77

8-
func (database *Database) Deploy(server *Server) {
9-
database.remoteConnection = server.Connection
10-
8+
func (database *Database) Deploy() {
119
if database.Options.ClearDatabase {
12-
database.deployClearDatabase(server)
10+
database.deployClearDatabase()
1311
}
1412

15-
database.deployStructure(server)
16-
database.deployData(server)
13+
database.deployStructure()
14+
database.deployData()
1715
}
1816

1917
// Deploy database structure
20-
func (database *Database) deployClearDatabase(server *Server) {
18+
func (database *Database) deployClearDatabase() {
2119

2220
// don't use database which we're trying to drop, instead use "mysql"
2321
schema := database.Schema
@@ -37,7 +35,7 @@ func (database *Database) deployClearDatabase(server *Server) {
3735
}
3836

3937
// Deploy database structure
40-
func (database *Database) deployStructure(server *Server) {
38+
func (database *Database) deployStructure() {
4139
Logger.Step("deploy database structure")
4240

4341
// Deploy structure only
@@ -49,7 +47,7 @@ func (database *Database) deployStructure(server *Server) {
4947
}
5048

5149
// Deploy database data
52-
func (database *Database) deployData(server *Server) {
50+
func (database *Database) deployData() {
5351
Logger.Step("deploy database data")
5452

5553
// Deploy data only

sync/server_deploy_filesystem.go

Lines changed: 14 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -8,17 +8,19 @@ import (
88
)
99

1010
// General sync
11-
func (filesystem *Filesystem) Deploy(server *Server) {
12-
switch server.Connection.GetType() {
11+
func (filesystem *Filesystem) Deploy() {
12+
switch filesystem.Connection.GetType() {
13+
case "local":
14+
fallthrough
1315
case "ssh":
14-
filesystem.deployRsync(server)
16+
filesystem.deployRsync()
1517
case "docker":
1618
errors.New("Docker not supported")
1719
}
1820
}
1921

2022
// Sync filesystem using rsync
21-
func (filesystem *Filesystem) deployRsync(server *Server) {
23+
func (filesystem *Filesystem) deployRsync() {
2224
args := []string{"-rlptD", "--delete-after", "--progress", "--human-readable"}
2325

2426
// include filter
@@ -40,8 +42,14 @@ func (filesystem *Filesystem) deployRsync(server *Server) {
4042
}
4143

4244
// build source and target paths
43-
sourcePath := filesystem.localPath(server)
44-
targetPath := fmt.Sprintf("%s:%s", server.Connection.SshConnectionHostnameString(), filesystem.Path)
45+
sourcePath := filesystem.localPath()
46+
targetPath := ""
47+
switch filesystem.Connection.GetType() {
48+
case "ssh":
49+
targetPath = fmt.Sprintf("%s:%s", filesystem.Connection.SshConnectionHostnameString(), filesystem.Path)
50+
case "local":
51+
targetPath = filesystem.Path
52+
}
4553

4654
// make sure source/target paths are using suffix slash
4755
args = append(args, RsyncPath(sourcePath), RsyncPath(targetPath))

sync/server_sync.go

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

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

2931
func (server *Server) SyncDatabases() {
3032
for _, database := range server.Database {
33+
database.ApplyDefaults(server)
3134
Logger.Main("Starting sync of %s", database.String("sync"))
32-
database.Sync(server)
35+
database.Sync()
3336
}
3437
}

sync/server_sync_database.go

Lines changed: 7 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -7,19 +7,17 @@ import (
77
"os"
88
)
99

10-
func (database *Database) Sync(server *Server) {
11-
database.remoteConnection = server.Connection
12-
10+
func (database *Database) Sync() {
1311
if database.Options.ClearDatabase {
14-
database.syncClearDatabase(server)
12+
database.syncClearDatabase()
1513
}
1614

17-
database.syncStructure(server)
18-
database.syncData(server)
15+
database.syncStructure()
16+
database.syncData()
1917
}
2018

2119
// Sync database structure
22-
func (database *Database) syncClearDatabase(server *Server) {
20+
func (database *Database) syncClearDatabase() {
2321

2422
// don't use database which we're trying to drop, instead use "mysql"
2523
schema := database.Local.Schema
@@ -39,7 +37,7 @@ func (database *Database) syncClearDatabase(server *Server) {
3937
}
4038

4139
// Sync database structure
42-
func (database *Database) syncStructure(server *Server) {
40+
func (database *Database) syncStructure() {
4341
Logger.Step("syncing database structure")
4442

4543
tmpfile, err := ioutil.TempFile("", "dump")
@@ -58,7 +56,7 @@ func (database *Database) syncStructure(server *Server) {
5856
}
5957

6058
// Sync database data
61-
func (database *Database) syncData(server *Server) {
59+
func (database *Database) syncData() {
6260
Logger.Step("syncing database data")
6361

6462
tmpfile, err := ioutil.TempFile("", "dump")

sync/server_sync_filesystem.go

Lines changed: 15 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -8,17 +8,19 @@ import (
88
)
99

1010
// General sync
11-
func (filesystem *Filesystem) Sync(server *Server) {
12-
switch server.Connection.GetType() {
11+
func (filesystem *Filesystem) Sync() {
12+
switch filesystem.Connection.GetType() {
13+
case "local":
14+
fallthrough
1315
case "ssh":
14-
filesystem.syncRsync(server)
16+
filesystem.syncRsync()
1517
case "docker":
1618
errors.New("Docker not supported")
1719
}
1820
}
1921

2022
// Sync filesystem using rsync
21-
func (filesystem *Filesystem) syncRsync(server *Server) {
23+
func (filesystem *Filesystem) syncRsync() {
2224
args := []string{"-rlptD", "--delete-after", "--progress", "--human-readable"}
2325

2426
// include filter
@@ -40,8 +42,15 @@ func (filesystem *Filesystem) syncRsync(server *Server) {
4042
}
4143

4244
// build source and target paths
43-
sourcePath := fmt.Sprintf("%s:%s", server.Connection.SshConnectionHostnameString(), filesystem.Path)
44-
targetPath := filesystem.localPath(server)
45+
sourcePath := ""
46+
switch filesystem.Connection.GetType() {
47+
case "ssh":
48+
sourcePath = fmt.Sprintf("%s:%s", filesystem.Connection.SshConnectionHostnameString(), filesystem.Path)
49+
case "local":
50+
sourcePath = filesystem.Path
51+
}
52+
53+
targetPath := filesystem.localPath()
4554

4655
// make sure source/target paths are using suffix slash
4756
args = append(args, RsyncPath(sourcePath), RsyncPath(targetPath))

0 commit comments

Comments
 (0)