Establish connections using bookmark ID only (#619)

* Establish connections using bookmark ID only
* Refactor specs
* Extra tests
* Fix homedir assertion for bookmarks path
* Fix newline in the warning message
* Check for bookmark file existence before reading
* Connect code restructure
This commit is contained in:
Dan Sosedoff
2022-12-19 12:33:13 -06:00
committed by GitHub
parent 0b9e7cdb4e
commit 69233cd769
11 changed files with 411 additions and 325 deletions

View File

@@ -142,27 +142,16 @@ func Connect(c *gin.Context) {
return
}
url := c.Request.FormValue("url")
if url == "" {
badRequest(c, errURLRequired)
return
}
var (
cl *client.Client
err error
)
url, err := connection.FormatURL(command.Options{
URL: url,
Passfile: command.Opts.Passfile,
})
if err != nil {
badRequest(c, err)
return
if bookmarkID := c.Request.FormValue("bookmark_id"); bookmarkID != "" {
cl, err = ConnectWithBookmark(bookmarkID)
} else {
cl, err = ConnectWithURL(c)
}
var sshInfo *shared.SSHInfo
if c.Request.FormValue("ssh") != "" {
sshInfo = parseSshInfo(c)
}
cl, err := client.NewFromUrl(url, sshInfo)
if err != nil {
badRequest(c, err)
return
@@ -187,6 +176,39 @@ func Connect(c *gin.Context) {
successResponse(c, info.Format()[0])
}
func ConnectWithURL(c *gin.Context) (*client.Client, error) {
url := c.Request.FormValue("url")
if url == "" {
return nil, errURLRequired
}
url, err := connection.FormatURL(command.Options{
URL: url,
Passfile: command.Opts.Passfile,
})
if err != nil {
return nil, err
}
var sshInfo *shared.SSHInfo
if c.Request.FormValue("ssh") != "" {
sshInfo = parseSshInfo(c)
}
return client.NewFromUrl(url, sshInfo)
}
func ConnectWithBookmark(id string) (*client.Client, error) {
manager := bookmarks.NewManager(command.Opts.BookmarksDir)
bookmark, err := manager.Get(id)
if err != nil {
return nil, err
}
return client.NewFromBookmark(bookmark)
}
// SwitchDb perform database switch for the client connection
func SwitchDb(c *gin.Context) {
if command.Opts.LockSession {
@@ -500,8 +522,9 @@ func HandleQuery(query string, c *gin.Context) {
// GetBookmarks renders the list of available bookmarks
func GetBookmarks(c *gin.Context) {
bookmarks, err := bookmarks.ReadAll(bookmarks.Path(command.Opts.BookmarksDir))
serveResult(c, bookmarks, err)
manager := bookmarks.NewManager(command.Opts.BookmarksDir)
ids, err := manager.ListIDs()
serveResult(c, ids, err)
}
// GetInfo renders the pgweb system information