Skip to content

Commit cf12f9b

Browse files
committed
adjust: move ParseURL from models into config, and rename it into ParseConfigURL
1 parent 4d9fd5b commit cf12f9b

File tree

2 files changed

+35
-38
lines changed

2 files changed

+35
-38
lines changed

Diff for: helpers/config.go

+35-3
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,14 @@
11
package helpers
22

33
import (
4+
"fmt"
5+
"net/url"
46
"os"
57
"path/filepath"
68

79
"github.com/pelletier/go-toml/v2"
810

11+
"github.com/jorgerojas26/lazysql/drivers"
912
"github.com/jorgerojas26/lazysql/models"
1013
)
1114

@@ -26,14 +29,43 @@ func LoadConfig() (Config, error) {
2629
return config, err
2730
}
2831

29-
for idx, cfg := range config.Connections {
30-
cfg.ParseURL()
31-
config.Connections[idx].URL = cfg.URL
32+
for idx, conn := range config.Connections {
33+
config.Connections[idx].URL = ParseConfigURL(&conn)
3234
}
3335

3436
return config, nil
3537
}
3638

39+
// ParseConfigURL will manually parse config url if url empty
40+
//
41+
// it main purpose is for handling username & password with special characters
42+
//
43+
// only sqlserver for now
44+
func ParseConfigURL(conn *models.Connection) string {
45+
if conn.URL != "" {
46+
return conn.URL
47+
}
48+
49+
// only sqlserver for now
50+
if conn.Provider != drivers.DriverMSSQL {
51+
return conn.URL
52+
}
53+
54+
user := url.QueryEscape(conn.Username)
55+
pass := url.QueryEscape(conn.Password)
56+
57+
return fmt.Sprintf(
58+
"%s://%s:%s@%s:%s?database=%s%s",
59+
conn.Provider,
60+
user,
61+
pass,
62+
conn.Hostname,
63+
conn.Port,
64+
conn.DBName,
65+
conn.URLParams,
66+
)
67+
}
68+
3769
func LoadConnections() (connections []models.Connection, err error) {
3870
config, err := LoadConfig()
3971
if err != nil {

Diff for: models/models.go

-35
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,6 @@
11
package models
22

33
import (
4-
"fmt"
5-
"net/url"
6-
74
"github.com/rivo/tview"
85
)
96

@@ -25,38 +22,6 @@ type Connection struct {
2522
Commands []*Command
2623
}
2724

28-
// ParseURL will manually parse url if url empty
29-
//
30-
// for handling username & password with special characters
31-
//
32-
// only sqlserver for now
33-
//
34-
// need to refactor if wanted to reuse driver list in drivers/constants.go
35-
func (c *Connection) ParseURL() {
36-
if c.URL != "" {
37-
return
38-
}
39-
40-
// only sqlserver for now
41-
if c.Provider != "sqlserver" {
42-
return
43-
}
44-
45-
user := url.QueryEscape(c.Username)
46-
pass := url.QueryEscape(c.Password)
47-
48-
c.URL = fmt.Sprintf(
49-
"%s://%s:%s@%s:%s?database=%s%s",
50-
c.Provider,
51-
user,
52-
pass,
53-
c.Hostname,
54-
c.Port,
55-
c.DBName,
56-
c.URLParams,
57-
)
58-
}
59-
6025
type Command struct {
6126
Command string
6227
WaitForPort string

0 commit comments

Comments
 (0)