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

@@ -13,6 +13,7 @@ import (
"github.com/jmoiron/sqlx"
_ "github.com/lib/pq"
"github.com/sosedoff/pgweb/pkg/bookmarks"
"github.com/sosedoff/pgweb/pkg/command"
"github.com/sosedoff/pgweb/pkg/connection"
"github.com/sosedoff/pgweb/pkg/history"
@@ -137,6 +138,30 @@ func NewFromUrl(url string, sshInfo *shared.SSHInfo) (*Client, error) {
return &client, nil
}
func NewFromBookmark(bookmark *bookmarks.Bookmark) (*Client, error) {
var (
connStr string
err error
)
options := bookmark.ConvertToOptions()
if options.URL != "" {
connStr = options.URL
} else {
connStr, err = connection.BuildStringFromOptions(options)
if err != nil {
return nil, err
}
}
var sshInfo *shared.SSHInfo
if !bookmark.SSHInfoIsEmpty() {
sshInfo = bookmark.SSH
}
return NewFromUrl(connStr, sshInfo)
}
func (client *Client) init() {
if command.Opts.QueryTimeout > 0 {
client.queryTimeout = time.Second * time.Duration(command.Opts.QueryTimeout)

View File

@@ -59,14 +59,13 @@ func parsePrivateKey(keyPath string, keyPass string) (ssh.Signer, error) {
}
signer, err := ssh.ParsePrivateKey(buff)
if err != nil {
if strings.Contains(err.Error(), "cannot decode encrypted private keys") {
if keyPass == "" {
return nil, errors.New("SSH key password is not provided")
}
return sshkeys.ParseEncryptedPrivateKey(buff, []byte(keyPass))
if _, ok := err.(*ssh.PassphraseMissingError); ok {
if keyPass == "" {
return nil, errors.New("SSH key password is not provided")
}
return sshkeys.ParseEncryptedPrivateKey(buff, []byte(keyPass))
}
return signer, err
}