コード例 #1
0
func Test_Invalid_Url(t *testing.T) {
	opts := command.Options{}
	examples := []string{
		"postgre://foobar",
		"foobar",
	}

	for _, val := range examples {
		opts.Url = val
		str, err := BuildString(opts)

		assert.Equal(t, "", str)
		assert.Error(t, err)
		assert.Equal(t, "Invalid URL. Valid format: postgres://user:password@host:port/db?sslmode=mode", err.Error())
	}
}
コード例 #2
0
func Test_Localhost(t *testing.T) {
	opts := command.Options{
		Host:   "localhost",
		Port:   5432,
		User:   "******",
		Pass:   "******",
		DbName: "db",
	}

	str, err := BuildString(opts)
	assert.Equal(t, nil, err)
	assert.Equal(t, "postgres://*****:*****@localhost:5432/db?sslmode=disable", str)

	opts.Host = "127.0.0.1"
	str, err = BuildString(opts)
	assert.Equal(t, nil, err)
	assert.Equal(t, "postgres://*****:*****@127.0.0.1:5432/db?sslmode=disable", str)
}
コード例 #3
0
ファイル: connection_string.go プロジェクト: daveallie/pgweb
func BuildString(opts command.Options) (string, error) {
	if opts.Url != "" {
		return FormatUrl(opts)
	}

	// Try to detect user from current OS user
	if opts.User == "" {
		u, err := currentUser()
		if err == nil {
			opts.User = u
		}
	}

	// 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", neturl.QueryEscape(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
}