mirror of
https://github.com/sosedoff/pgweb.git
synced 2024-12-15 20:13:06 +03:00
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:
parent
f4fb5744ef
commit
0e88e3e1f4
@ -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
|
||||
|
||||
}
|
||||
|
@ -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: "",
|
||||
|
Loading…
Reference in New Issue
Block a user