Swtich to use new connection string builder function

This commit is contained in:
Dan Sosedoff 2014-12-17 21:59:26 -06:00
parent 58ee238db4
commit 7859870646
2 changed files with 5 additions and 54 deletions

View File

@ -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
View File

@ -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