This commit is contained in:
Dan Sosedoff
2019-11-02 13:00:23 -05:00
parent 7475f398b1
commit 8428d268b1
10 changed files with 51 additions and 46 deletions

View File

@@ -13,24 +13,27 @@ import (
"github.com/sosedoff/pgweb/pkg/shared"
)
// Bookmark contains information about bookmarked database connection
type Bookmark struct {
Url string `json:"url"` // Postgres connection URL
URL string `json:"url"` // Postgres connection URL
Host string `json:"host"` // Server hostname
Port int `json:"port"` // Server port
User string `json:"user"` // Database user
Password string `json:"password"` // User password
Database string `json:"database"` // Database name
Ssl string `json:"ssl"` // Connection SSL mode
Ssh *shared.SSHInfo `json:"ssh"` // SSH tunnel config
SSH *shared.SSHInfo `json:"ssh"` // SSH tunnel config
}
// SSHInfoIsEmpty returns true if ssh configration is not provided
func (b Bookmark) SSHInfoIsEmpty() bool {
return b.Ssh == nil || b.Ssh.User == "" && b.Ssh.Host == "" && b.Ssh.Port == ""
return b.SSH == nil || b.SSH.User == "" && b.SSH.Host == "" && b.SSH.Port == ""
}
// ConvertToOptions returns an options struct from connection details
func (b Bookmark) ConvertToOptions() command.Options {
return command.Options{
Url: b.Url,
URL: b.URL,
Host: b.Host,
Port: b.Port,
User: b.User,
@@ -72,8 +75,8 @@ func readServerConfig(path string) (Bookmark, error) {
}
// Set default SSH port if it's not provided by user
if bookmark.Ssh != nil && bookmark.Ssh.Port == "" {
bookmark.Ssh.Port = "22"
if bookmark.SSH != nil && bookmark.SSH.Port == "" {
bookmark.SSH.Port = "22"
}
return bookmark, err
@@ -84,15 +87,16 @@ func fileBasename(path string) string {
return strings.Replace(filename, filepath.Ext(path), "", 1)
}
// Path returns bookmarks storage path
func Path(overrideDir string) string {
if overrideDir == "" {
path, _ := homedir.Dir()
return fmt.Sprintf("%s/.pgweb/bookmarks", path)
}
return overrideDir
}
// ReadAll returns all available bookmarks
func ReadAll(path string) (map[string]Bookmark, error) {
results := map[string]Bookmark{}
@@ -106,7 +110,7 @@ func ReadAll(path string) (map[string]Bookmark, error) {
continue
}
fullPath := path + "/" + file.Name()
fullPath := filepath.Join(path, file.Name())
key := fileBasename(file.Name())
config, err := readServerConfig(fullPath)
@@ -121,6 +125,7 @@ func ReadAll(path string) (map[string]Bookmark, error) {
return results, nil
}
// GetBookmark reads an existing bookmark
func GetBookmark(bookmarkPath string, bookmarkName string) (Bookmark, error) {
bookmarks, err := ReadAll(bookmarkPath)
if err != nil {

View File

@@ -26,7 +26,7 @@ func Test_Bookmark(t *testing.T) {
assert.Equal(t, "mydatabase", bookmark.Database)
assert.Equal(t, "disable", bookmark.Ssl)
assert.Equal(t, "", bookmark.Password)
assert.Equal(t, "", bookmark.Url)
assert.Equal(t, "", bookmark.URL)
bookmark, err = readServerConfig("../../data/bookmark_invalid_ssl.toml")
assert.Equal(t, nil, err)
@@ -37,7 +37,7 @@ func Test_Bookmark_URL(t *testing.T) {
bookmark, err := readServerConfig("../../data/bookmark_url.toml")
assert.Equal(t, nil, err)
assert.Equal(t, "postgres://username:password@host:port/database?sslmode=disable", bookmark.Url)
assert.Equal(t, "postgres://username:password@host:port/database?sslmode=disable", bookmark.URL)
assert.Equal(t, "", bookmark.Host)
assert.Equal(t, 5432, bookmark.Port)
assert.Equal(t, "", bookmark.User)
@@ -106,19 +106,19 @@ func Test_Bookmark_SSHInfoIsEmpty(t *testing.T) {
User: "postgres",
}
b := Bookmark{Ssh: nil}
b := Bookmark{SSH: nil}
assert.True(t, b.SSHInfoIsEmpty())
b = Bookmark{Ssh: emptySSH}
b = Bookmark{SSH: emptySSH}
assert.True(t, b.SSHInfoIsEmpty())
b.Ssh = populatedSSH
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",
URL: "postgres://username:password@host:port/database?sslmode=disable",
Host: "localhost",
Port: 5432,
User: "postgres",
@@ -128,7 +128,7 @@ func Test_ConvertToOptions(t *testing.T) {
}
expOpt := command.Options{
Url: "postgres://username:password@host:port/database?sslmode=disable",
URL: "postgres://username:password@host:port/database?sslmode=disable",
Host: "localhost",
Port: 5432,
User: "postgres",