Skip to content

Commit a304307

Browse files
committed
Implement new commandbuilder.connection version
Switch to new style of docker and ssh arguments
1 parent a894fba commit a304307

18 files changed

+135
-84
lines changed

command_deploy.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ func (command *DeployCommand) Execute(args []string) error {
1919
Logger.FatalErrorExit(3, err)
2020
}
2121
Logger.Step("using Server[%s]", server)
22-
Logger.Step("using %s", confServer.Connection.String())
22+
Logger.Step("using %s", confServer.Connection.GetInstance().String())
2323

2424
// --dump
2525
if command.Dump {

command_sync.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ func (command *SyncCommand) Execute(args []string) error {
1919
Logger.FatalErrorExit(3, err)
2020
}
2121
Logger.Step("using Server[%s]", server)
22-
Logger.Step("using %s", confServer.Connection.String())
22+
Logger.Step("using %s", confServer.Connection.GetInstance().String())
2323

2424
// --dump
2525
if command.Dump {

sync/config.go

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@ package sync
33
import (
44
"regexp"
55
"sync"
6-
"github.com/webdevops/go-shell/commandbuilder"
76
)
87

98
var waitGroup sync.WaitGroup
@@ -19,7 +18,7 @@ type Filesystem struct {
1918
Path string
2019
Local string
2120
Filter Filter
22-
Connection commandbuilder.Connection
21+
Connection YamlCommandBuilderConnection
2322
Options struct {
2423
GenerateStubs bool `yaml:"generate-stubs"`
2524
}
@@ -47,7 +46,7 @@ type Database struct {
4746
Password string
4847

4948
Filter Filter
50-
Connection commandbuilder.Connection
49+
Connection YamlCommandBuilderConnection
5150

5251
Local struct {
5352
Type string
@@ -57,7 +56,7 @@ type Database struct {
5756
User string
5857
Password string
5958

60-
Connection commandbuilder.Connection
59+
Connection YamlCommandBuilderConnection
6160
Options DatabaseOptions
6261
}
6362
Options DatabaseOptions
@@ -78,7 +77,7 @@ type Execution struct {
7877

7978
type Server struct {
8079
Path string
81-
Connection commandbuilder.Connection
80+
Connection YamlCommandBuilderConnection
8281
Filesystem []Filesystem
8382
Database []Database
8483
ExecStartup []Execution `yaml:"exec-startup"`

sync/database.go

Lines changed: 21 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -3,12 +3,13 @@ package sync
33
import (
44
"fmt"
55
"strings"
6+
"github.com/mohae/deepcopy"
67
)
78

89
func (database *Database) ApplyDefaults(server *Server) {
910
// set default connection if not set
1011
if database.Connection.IsEmpty() {
11-
database.Connection = server.Connection
12+
database.Connection = deepcopy.Copy(server.Connection).(YamlCommandBuilderConnection)
1213
}
1314
}
1415

@@ -43,20 +44,26 @@ func (database *Database) GetPostgres() DatabasePostgres {
4344

4445
func (database *Database) String(direction string) string {
4546
var parts, remote, local []string
47+
48+
connRemote := database.Connection.GetInstance()
49+
connLocal := database.Local.Connection.GetInstance()
4650

4751
// general
4852
parts = append(parts, fmt.Sprintf("Type:%s", database.Type))
4953

54+
//-------------------------------------------
5055
// remote
5156
remote = append(remote, fmt.Sprintf("Schema:%s", database.Schema))
52-
remote = append(remote, fmt.Sprintf("Connection:%s", database.Connection.GetType()))
57+
remote = append(remote, fmt.Sprintf("Connection:%s", connRemote.GetType()))
5358

54-
if database.Connection.SshConnectionHostnameString() != "" {
55-
remote = append(remote, fmt.Sprintf("SSH:%s", database.Connection.SshConnectionHostnameString()))
59+
if connRemote.IsSsh() {
60+
if connRemote.SshConnectionHostnameString() != "" {
61+
remote = append(remote, fmt.Sprintf("SSH:%s", connRemote.SshConnectionHostnameString()))
62+
}
5663
}
5764

58-
if database.Connection.Docker != "" {
59-
remote = append(remote, fmt.Sprintf("Docker:%s", database.Connection.Docker))
65+
if connRemote.IsDocker() {
66+
remote = append(remote, fmt.Sprintf("Docker:%s", connRemote.Docker))
6067
} else if database.Hostname != "" {
6168
hostname := database.Hostname
6269

@@ -74,16 +81,19 @@ func (database *Database) String(direction string) string {
7481
remote = append(remote, fmt.Sprintf("Passwd:%s", "*****"))
7582
}
7683

84+
//-------------------------------------------
7785
// local
7886
local = append(local, fmt.Sprintf("Schema:%s", database.Local.Schema))
79-
local = append(local, fmt.Sprintf("Connection:%s", database.Local.Connection.GetType()))
87+
local = append(local, fmt.Sprintf("Connection:%s", connLocal.GetType()))
8088

81-
if database.Local.Connection.SshConnectionHostnameString() != "" {
82-
local = append(local, fmt.Sprintf("SSH:%s", database.Local.Connection.SshConnectionHostnameString()))
89+
if connLocal.IsSsh() {
90+
if connLocal.SshConnectionHostnameString() != "" {
91+
local = append(local, fmt.Sprintf("SSH:%s", connLocal.SshConnectionHostnameString()))
92+
}
8393
}
8494

85-
if database.Local.Connection.Docker != "" {
86-
local = append(local, fmt.Sprintf("Docker:%s", database.Local.Connection.Docker))
95+
if connLocal.IsDocker() {
96+
local = append(local, fmt.Sprintf("Docker:%s", connLocal.Docker))
8797
} else if database.Local.Hostname != "" {
8898
hostname := database.Local.Hostname
8999

sync/database_mysql.go

Lines changed: 8 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -11,18 +11,13 @@ type DatabaseMysql struct {
1111
}
1212

1313
func (database *DatabaseMysql) init() {
14+
connLocal := database.Local.Connection.GetInstance()
15+
connRemote := database.Connection.GetInstance()
16+
1417
// LOCAL
15-
if database.Local.Connection.Docker != "" {
16-
queryConn := database.Local.Connection.Clone()
17-
queryConn.Type = "auto"
18-
queryConn.Docker = ""
19-
20-
// docker auto hostname
21-
database.Local.Hostname = "127.0.0.1"
22-
18+
if connLocal.IsDocker() {
2319
if database.Local.User == "" || database.Local.Schema == "" {
24-
containerId := queryConn.DockerGetContainerId(database.Local.Connection.Docker)
25-
containerEnv := queryConn.DockerGetEnvironment(containerId)
20+
containerEnv := connLocal.DockerGetEnvironment()
2621

2722
// try to guess user/password
2823
if database.Local.User == "" {
@@ -62,17 +57,9 @@ func (database *DatabaseMysql) init() {
6257
}
6358

6459
// Remote
65-
if database.Connection.Docker != "" {
66-
queryConn := database.Connection.Clone()
67-
queryConn.Type = "auto"
68-
queryConn.Docker = ""
69-
70-
// docker auto hostname
71-
database.Hostname = "127.0.0.1"
72-
60+
if connRemote.IsDocker() {
7361
if database.User == "" || database.Schema == "" {
74-
containerId := queryConn.DockerGetContainerId(database.Connection.Docker)
75-
containerEnv := queryConn.DockerGetEnvironment(containerId)
62+
containerEnv := connRemote.DockerGetEnvironment()
7663

7764
// try to guess user/password
7865
if database.User == "" {
@@ -118,7 +105,7 @@ func (database *DatabaseMysql) tableFilter(connectionType string) ([]string, []s
118105

119106
var tableList []string
120107

121-
if (connectionType == "local") {
108+
if connectionType == "local" {
122109
if len(database.cacheLocalTableList) == 0 {
123110
Logger.Step("get list of mysql tables for table filter")
124111
database.cacheLocalTableList = database.tableList(connectionType)

sync/database_mysql_local.go

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,10 @@ package sync
33
import "github.com/webdevops/go-shell"
44

55
func (database *DatabaseMysql) localMysqldumpCmdBuilder(additionalArgs []string, useFilter bool) []interface{} {
6-
connection := database.Local.Connection.Clone()
76
var args []string
87

8+
connection := database.Local.Connection.GetInstance().Clone()
9+
910
if database.Local.User != "" {
1011
args = append(args, shell.Quote("-u" + database.Local.User))
1112
}
@@ -49,7 +50,8 @@ func (database *DatabaseMysql) localMysqldumpCmdBuilder(additionalArgs []string,
4950
}
5051

5152
func (database *DatabaseMysql) localMysqlCmdBuilder(args ...string) []interface{} {
52-
connection := database.Local.Connection.Clone()
53+
connection := database.Local.Connection.GetInstance().Clone()
54+
5355
args = append(args, "-BN")
5456

5557
if database.Local.User != "" {

sync/database_mysql_remote.go

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,9 +6,10 @@ import (
66
)
77

88
func (database *DatabaseMysql) remoteMysqldumpCmdBuilder(additionalArgs []string, useFilter bool) []interface{} {
9-
connection := database.Connection.Clone()
109
var args []string
1110

11+
connection := database.Connection.GetInstance().Clone()
12+
1213
if database.User != "" {
1314
args = append(args, shell.Quote("-u" + database.User))
1415
}
@@ -57,7 +58,8 @@ func (database *DatabaseMysql) remoteMysqldumpCmdBuilder(additionalArgs []string
5758
}
5859

5960
func (database *DatabaseMysql) remoteMysqlCmdBuilder(args ...string) []interface{} {
60-
connection := database.Connection.Clone()
61+
connection := database.Connection.GetInstance().Clone()
62+
6163
args = append(args, "-BN")
6264

6365
if database.User != "" {
@@ -90,7 +92,8 @@ func (database *DatabaseMysql) remoteMysqlCmdBuilder(args ...string) []interface
9092

9193

9294
func (database *DatabaseMysql) remoteMysqlCmdBuilderUncompress(args ...string) []interface{} {
93-
connection := database.Connection.Clone()
95+
connection := database.Connection.GetInstance().Clone()
96+
9497
args = append(args, "-BN")
9598

9699
if database.User != "" {

sync/database_postgres.go

Lines changed: 8 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -12,18 +12,14 @@ type DatabasePostgres struct {
1212

1313

1414
func (database *DatabasePostgres) init() {
15-
// LOCAL
16-
if database.Local.Connection.Docker != "" {
17-
queryConn := database.Local.Connection.Clone()
18-
queryConn.Type = "auto"
19-
queryConn.Docker = ""
15+
connLocal := database.Local.Connection.GetInstance()
16+
connRemote := database.Connection.GetInstance()
2017

21-
// docker auto hostname
22-
database.Local.Hostname = "127.0.0.1"
2318

19+
// LOCAL
20+
if connLocal.IsDocker() {
2421
if database.Local.User == "" || database.Local.Schema == "" {
25-
containerId := queryConn.DockerGetContainerId(database.Local.Connection.Docker)
26-
containerEnv := queryConn.DockerGetEnvironment(containerId)
22+
containerEnv := connLocal.DockerGetEnvironment()
2723

2824
// try to guess user/password
2925
if database.Local.User == "" {
@@ -53,17 +49,9 @@ func (database *DatabasePostgres) init() {
5349
}
5450

5551
// Remote
56-
if database.Connection.Docker != "" {
57-
queryConn := database.Connection.Clone()
58-
queryConn.Type = "auto"
59-
queryConn.Docker = ""
60-
61-
// docker auto hostname
62-
database.Hostname = "127.0.0.1"
63-
52+
if connRemote.IsDocker() {
6453
if database.User == "" || database.Schema == "" {
65-
containerId := queryConn.DockerGetContainerId(database.Connection.Docker)
66-
containerEnv := queryConn.DockerGetEnvironment(containerId)
54+
containerEnv := connRemote.DockerGetEnvironment()
6755

6856
// try to guess user/password
6957
if database.User == "" {
@@ -99,7 +87,7 @@ func (database *DatabasePostgres) tableFilter(connectionType string) ([]string,
9987

10088
var tableList []string
10189

102-
if (connectionType == "local") {
90+
if connectionType == "local" {
10391
if len(database.cacheLocalTableList) == 0 {
10492
Logger.Step("get list of postgres tables for table filter")
10593
database.cacheLocalTableList = database.tableList(connectionType)

sync/database_postgres_local.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ package sync
33
import "github.com/webdevops/go-shell"
44

55
func (database *DatabasePostgres) localPgdumpCmdBuilder(additionalArgs []string, useFilter bool) []interface{} {
6-
connection := database.Local.Connection.Clone()
6+
connection := database.Local.Connection.GetInstance().Clone()
77
var args []string
88

99
if database.Local.User != "" {
@@ -49,7 +49,7 @@ func (database *DatabasePostgres) localPgdumpCmdBuilder(additionalArgs []string,
4949
}
5050

5151
func (database *DatabasePostgres) localPsqlCmdBuilder(args ...string) []interface{} {
52-
connection := database.Local.Connection.Clone()
52+
connection := database.Local.Connection.GetInstance().Clone()
5353
args = append(args, "-t")
5454

5555
if database.Local.User != "" {

sync/database_postgres_remote.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ import (
66
)
77

88
func (database *DatabasePostgres) remotePgdumpCmdBuilder(additionalArgs []string, useFilter bool) []interface{} {
9-
connection := database.Connection.Clone()
9+
connection := database.Connection.GetInstance().Clone()
1010
var args []string
1111

1212
if database.User != "" {
@@ -57,7 +57,7 @@ func (database *DatabasePostgres) remotePgdumpCmdBuilder(additionalArgs []string
5757
}
5858

5959
func (database *DatabasePostgres) remotePsqlCmdBuilder(args ...string) []interface{} {
60-
connection := database.Connection.Clone()
60+
connection := database.Connection.GetInstance().Clone()
6161
args = append(args, "-t")
6262

6363
if database.User != "" {
@@ -90,7 +90,7 @@ func (database *DatabasePostgres) remotePsqlCmdBuilder(args ...string) []interfa
9090

9191

9292
func (database *DatabasePostgres) remotePsqlCmdBuilderUncompress(args ...string) []interface{} {
93-
connection := database.Connection.Clone()
93+
connection := database.Connection.GetInstance().Clone()
9494
args = append(args, "-t")
9595

9696
if database.User != "" {

0 commit comments

Comments
 (0)