pgweb/connection_string.go

99 lines
2.0 KiB
Go
Raw Normal View History

2014-12-18 06:32:50 +03:00
package main
import (
"errors"
"fmt"
"os"
2014-12-18 06:32:50 +03:00
"os/user"
"strings"
)
func currentUser() (string, error) {
u, err := user.Current()
if err == nil {
return u.Username, nil
}
name := os.Getenv("USER")
if name != "" {
return name, nil
}
return "", errors.New("Unable to detect OS user")
}
2014-12-18 06:32:50 +03:00
func formatConnectionUrl(opts Options) (string, error) {
url := opts.Url
// Make sure to only accept urls in a standard format
if !strings.Contains(url, "postgres://") {
return "", errors.New("Invalid URL. Valid format: postgres://user:password@host:port/db?sslmode=mode")
}
// Special handling for local connections
if strings.Contains(url, "localhost") || strings.Contains(url, "127.0.0.1") {
if !strings.Contains(url, "?sslmode") {
if opts.Ssl == "" {
url += fmt.Sprintf("?sslmode=%s", "disable")
} else {
url += fmt.Sprintf("?sslmode=%s", opts.Ssl)
}
}
}
// Append sslmode parameter only if its defined as a flag and not present
// in the connection string.
if !strings.Contains(url, "?sslmode") && opts.Ssl != "" {
url += fmt.Sprintf("?sslmode=%s", opts.Ssl)
}
return url, nil
}
2014-12-18 06:56:15 +03:00
func connectionSettingsBlank(opts Options) bool {
return opts.Host == "" && opts.User == "" && opts.DbName == "" && opts.Url == ""
}
2014-12-18 06:32:50 +03:00
func buildConnectionString(opts Options) (string, error) {
if opts.Url != "" {
return formatConnectionUrl(opts)
}
// Try to detect user from current OS user
if opts.User == "" {
u, err := currentUser()
2014-12-18 06:32:50 +03:00
if err == nil {
opts.User = u
2014-12-18 06:32:50 +03:00
}
}
// Disable ssl for localhost connections, most users have it disabled
if opts.Host == "localhost" || opts.Host == "127.0.0.1" {
if opts.Ssl == "" {
opts.Ssl = "disable"
}
}
url := "postgres://"
if opts.User != "" {
url += opts.User
}
if opts.Pass != "" {
url += fmt.Sprintf(":%s", opts.Pass)
}
url += fmt.Sprintf("@%s:%d", opts.Host, opts.Port)
if opts.DbName != "" {
url += fmt.Sprintf("/%s", opts.DbName)
}
if opts.Ssl != "" {
url += fmt.Sprintf("?sslmode=%s", opts.Ssl)
}
return url, nil
}