mirror of
https://github.com/sosedoff/pgweb.git
synced 2024-12-14 19:21:46 +03:00
Swtich to use new connection string builder function
This commit is contained in:
parent
58ee238db4
commit
7859870646
@ -23,12 +23,16 @@ type Result struct {
|
||||
}
|
||||
|
||||
func NewClient() (*Client, error) {
|
||||
str := getConnectionString()
|
||||
str, err := buildConnectionString(options)
|
||||
|
||||
if options.Debug {
|
||||
fmt.Println("Creating a new client with: %s", str)
|
||||
}
|
||||
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
db, err := sqlx.Open("postgres", str)
|
||||
|
||||
if err != nil {
|
||||
|
53
main.go
53
main.go
@ -5,8 +5,6 @@ import (
|
||||
"os"
|
||||
"os/exec"
|
||||
"os/signal"
|
||||
"os/user"
|
||||
"strings"
|
||||
|
||||
"github.com/gin-gonic/gin"
|
||||
"github.com/jessevdk/go-flags"
|
||||
@ -40,57 +38,6 @@ func exitWithMessage(message string) {
|
||||
os.Exit(1)
|
||||
}
|
||||
|
||||
func getConnectionString() string {
|
||||
if options.Url != "" {
|
||||
url := options.Url
|
||||
|
||||
if strings.Contains(url, "postgresql://") {
|
||||
fmt.Println("Invalid URL format. It should match: postgres://user:password@host:port/db?sslmode=mode")
|
||||
os.Exit(1)
|
||||
}
|
||||
|
||||
// Append sslmode parameter only if its defined as a flag and not present
|
||||
// in the connection string.
|
||||
if options.Ssl != "" && !strings.Contains(url, "sslmode") {
|
||||
url += fmt.Sprintf("?sslmode=%s", options.Ssl)
|
||||
}
|
||||
|
||||
return url
|
||||
}
|
||||
|
||||
// Try to detect user from current OS user
|
||||
if options.User == "" {
|
||||
user, err := user.Current()
|
||||
|
||||
if err == nil {
|
||||
options.User = user.Username
|
||||
}
|
||||
}
|
||||
|
||||
str := fmt.Sprintf(
|
||||
"host=%s port=%d user=%s dbname=%s",
|
||||
options.Host, options.Port,
|
||||
options.User, options.DbName,
|
||||
)
|
||||
|
||||
if options.Ssl == "" {
|
||||
// Disable ssl for localhost connections, most users have it disabled
|
||||
if options.Host == "localhost" || options.Host == "127.0.0.1" {
|
||||
options.Ssl = "disable"
|
||||
}
|
||||
}
|
||||
|
||||
if options.Ssl != "" {
|
||||
str += fmt.Sprintf(" sslmode=%s", options.Ssl)
|
||||
}
|
||||
|
||||
if options.Pass != "" {
|
||||
str += fmt.Sprintf(" password=%s", options.Pass)
|
||||
}
|
||||
|
||||
return str
|
||||
}
|
||||
|
||||
func initClient() {
|
||||
if connectionSettingsBlank(options) {
|
||||
return
|
||||
|
Loading…
Reference in New Issue
Block a user