From 7859870646185c764e7b8a31776d5dcc69b046fa Mon Sep 17 00:00:00 2001 From: Dan Sosedoff Date: Wed, 17 Dec 2014 21:59:26 -0600 Subject: [PATCH] Swtich to use new connection string builder function --- client.go | 6 +++++- main.go | 53 ----------------------------------------------------- 2 files changed, 5 insertions(+), 54 deletions(-) diff --git a/client.go b/client.go index d4d2036..9c3e265 100644 --- a/client.go +++ b/client.go @@ -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 { diff --git a/main.go b/main.go index 6e5f595..627d513 100644 --- a/main.go +++ b/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