Skip to content

Commit

Permalink
add URL method on config
Browse files Browse the repository at this point in the history
  • Loading branch information
strobus committed Oct 7, 2020
1 parent b361bf7 commit 10979ee
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 3 deletions.
13 changes: 13 additions & 0 deletions generate_address.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}

15 changes: 12 additions & 3 deletions tests/unit/generate_address_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"))
})
})
})

0 comments on commit 10979ee

Please sign in to comment.