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) {
|
func NewClient() (*Client, error) {
|
||||||
str := getConnectionString()
|
str, err := buildConnectionString(options)
|
||||||
|
|
||||||
if options.Debug {
|
if options.Debug {
|
||||||
fmt.Println("Creating a new client with: %s", str)
|
fmt.Println("Creating a new client with: %s", str)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
|
||||||
db, err := sqlx.Open("postgres", str)
|
db, err := sqlx.Open("postgres", str)
|
||||||
|
|
||||||
if err != nil {
|
if err != nil {
|
||||||
|
53
main.go
53
main.go
@ -5,8 +5,6 @@ import (
|
|||||||
"os"
|
"os"
|
||||||
"os/exec"
|
"os/exec"
|
||||||
"os/signal"
|
"os/signal"
|
||||||
"os/user"
|
|
||||||
"strings"
|
|
||||||
|
|
||||||
"github.com/gin-gonic/gin"
|
"github.com/gin-gonic/gin"
|
||||||
"github.com/jessevdk/go-flags"
|
"github.com/jessevdk/go-flags"
|
||||||
@ -40,57 +38,6 @@ func exitWithMessage(message string) {
|
|||||||
os.Exit(1)
|
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() {
|
func initClient() {
|
||||||
if connectionSettingsBlank(options) {
|
if connectionSettingsBlank(options) {
|
||||||
return
|
return
|
||||||
|
Loading…
x
Reference in New Issue
Block a user