Add a func to read bookmark from stored toml file

Given a bookmark path and bookmark name, GetBookmark
returns a Bookmark object that corresponds to the
stored bookmark settings.

In next commits, we will use this method to read
stored bookmarks and create a db client.
This commit is contained in:
akarki15 2016-11-10 01:22:07 -05:00
parent f4fb5744ef
commit 0e88e3e1f4
2 changed files with 41 additions and 0 deletions

View File

@ -75,3 +75,22 @@ func ReadAll(path string) (map[string]Bookmark, error) {
return results, nil
}
type ErrNonExistingBookmark string
func (e ErrNonExistingBookmark) Error() string {
return fmt.Sprintf("couldn't find a bookmark with name %s", e)
}
func GetBookmark(bookmarkPath string, bookmarkName string) (Bookmark, error) {
bookmarks, err := ReadAll(bookmarkPath)
if err != nil {
return Bookmark{}, err
}
bookmark, ok := bookmarks[bookmarkName]
if !ok {
return Bookmark{}, ErrNonExistingBookmark(bookmarkName)
}
return bookmark, nil
}

View File

@ -71,6 +71,28 @@ func Test_ReadBookmarks(t *testing.T) {
assert.Equal(t, 2, len(bookmarks))
}
func Test_GetBookmark(t *testing.T) {
expBookmark := Bookmark{
Host: "localhost",
Port: "5432",
User: "postgres",
Password: "",
Database: "mydatabase",
Ssl: "disable",
}
b, err := GetBookmark("../../data", "bookmark")
if assert.NoError(t, err) {
assert.Equal(t, expBookmark, b)
}
_, err = GetBookmark("../../data", "bar")
assert.Equal(t, ErrNonExistingBookmark("bar"), err)
_, err = GetBookmark("foo", "bookmark")
assert.Error(t, err)
}
func Test_Bookmark_SSHInfoIsEmpty(t *testing.T) {
emptySSH := shared.SSHInfo{
Host: "",