From 10979ee99c6fa372b59c25577a2306c0207dc7e4 Mon Sep 17 00:00:00 2001 From: strobus <22136798+strobus@users.noreply.github.com> Date: Wed, 7 Oct 2020 12:01:27 -0400 Subject: [PATCH] add URL method on config --- generate_address.go | 13 +++++++++++++ tests/unit/generate_address_test.go | 15 ++++++++++++--- 2 files changed, 25 insertions(+), 3 deletions(-) diff --git a/generate_address.go b/generate_address.go index dcd82f8..bf05aa2 100644 --- a/generate_address.go +++ b/generate_address.go @@ -23,3 +23,16 @@ func (c *PostgresConfig) GenerateAddress() string { } return addr } + +// URL returns a 'postgres://user:pass@host:port/database?sslmode=' style address +func (c PostgresConfig) URL() string { + var sslMode string + if c.SSLEnabled { + sslMode = "require" + } else { + sslMode = "disable" + } + return fmt.Sprintf("postgresql://%s:%s@%s:%d/%s?sslmode=%s", + c.Username, c.Password, c.Host, c.Port, c.Database, sslMode) +} + diff --git a/tests/unit/generate_address_test.go b/tests/unit/generate_address_test.go index 2d25cc3..e5d1b43 100644 --- a/tests/unit/generate_address_test.go +++ b/tests/unit/generate_address_test.go @@ -19,8 +19,17 @@ var ( ) var _ = Describe("Generate Address", func() { - It("Can generate a postgres address", func() { - result := testConfig.GenerateAddress() - Expect(result).To(Equal("host=test port=1 dbname=test user=test password=test sslmode=disable")) + Context("GenerateAddress", func() { + It("Can generate a postgres address", func() { + result := testConfig.GenerateAddress() + Expect(result).To(Equal("host=test port=1 dbname=test user=test password=test sslmode=disable")) + }) + }) + + Context("URL", func() { + It("should generate a postgres url", func() { + result := testConfig.URL() + Expect(result).To(Equal("postgresql://test:test@test:1/test?sslmode=disable")) + }) }) })