From 61dfe67ca8977d143c1e7b2f2ba3d932a70a1f9e Mon Sep 17 00:00:00 2001 From: akarki15 Date: Thu, 10 Nov 2016 01:22:16 -0500 Subject: [PATCH] Add a method to convert bookmark to options We will use client.NewFromURL to create a client from stored bookmark in next commit. Since client.NewFromURL takes in a command.Options, lets add a method on Bookmark to get corresponding Option. --- pkg/bookmarks/bookmarks.go | 14 ++++++++++++++ pkg/bookmarks/bookmarks_test.go | 27 +++++++++++++++++++++++++++ 2 files changed, 41 insertions(+) diff --git a/pkg/bookmarks/bookmarks.go b/pkg/bookmarks/bookmarks.go index 33b05c0..d11c4ac 100644 --- a/pkg/bookmarks/bookmarks.go +++ b/pkg/bookmarks/bookmarks.go @@ -9,6 +9,7 @@ import ( "github.com/BurntSushi/toml" "github.com/mitchellh/go-homedir" + "github.com/sosedoff/pgweb/pkg/command" "github.com/sosedoff/pgweb/pkg/shared" ) @@ -26,6 +27,19 @@ type Bookmark struct { func (b Bookmark) SSHInfoIsEmpty() bool { return b.Ssh.User == "" && b.Ssh.Host == "" && b.Ssh.Port == "" } + +func (b Bookmark) ConvertToOptions() (command.Options, error) { + return command.Options{ + Url: b.Url, + Host: b.Host, + Port: b.Port, + User: b.User, + Pass: b.Password, + DbName: b.Database, + Ssl: b.Ssl, + }, nil +} + func readServerConfig(path string) (Bookmark, error) { bookmark := Bookmark{} diff --git a/pkg/bookmarks/bookmarks_test.go b/pkg/bookmarks/bookmarks_test.go index ed6185f..2ee6e27 100644 --- a/pkg/bookmarks/bookmarks_test.go +++ b/pkg/bookmarks/bookmarks_test.go @@ -3,6 +3,7 @@ package bookmarks import ( "testing" + "github.com/sosedoff/pgweb/pkg/command" "github.com/sosedoff/pgweb/pkg/shared" "github.com/stretchr/testify/assert" ) @@ -107,3 +108,29 @@ func Test_Bookmark_SSHInfoIsEmpty(t *testing.T) { b.Ssh = populatedSSH assert.False(t, b.SSHInfoIsEmpty()) } + +func Test_ConvertToOptions(t *testing.T) { + b := Bookmark{ + Url: "postgres://username:password@host:port/database?sslmode=disable", + Host: "localhost", + Port: 5432, + User: "postgres", + Password: "password", + Database: "mydatabase", + Ssl: "disable", + } + + expOpt := command.Options{ + Url: "postgres://username:password@host:port/database?sslmode=disable", + Host: "localhost", + Port: 5432, + User: "postgres", + Pass: "password", + DbName: "mydatabase", + Ssl: "disable", + } + opt, err := b.ConvertToOptions() + if assert.NoError(t, err) { + assert.Equal(t, expOpt, opt) + } +}