Resolve conflict with master

This commit is contained in:
Dan Sosedoff
2016-11-15 21:51:23 -06:00
7 changed files with 141 additions and 18 deletions

View File

@@ -9,13 +9,14 @@ import (
"github.com/BurntSushi/toml"
"github.com/mitchellh/go-homedir"
"github.com/sosedoff/pgweb/pkg/command"
"github.com/sosedoff/pgweb/pkg/shared"
)
type Bookmark struct {
Url string `json:"url"` // Postgres connection URL
Host string `json:"host"` // Server hostname
Port string `json:"port"` // Server port
Port int `json:"port"` // Server port
User string `json:"user"` // Database user
Password string `json:"password"` // User password
Database string `json:"database"` // Database name
@@ -23,6 +24,22 @@ type Bookmark struct {
Ssh shared.SSHInfo `json:"ssh"` // SSH tunnel config
}
func (b Bookmark) SSHInfoIsEmpty() bool {
return b.Ssh.User == "" && b.Ssh.Host == "" && b.Ssh.Port == ""
}
func (b Bookmark) ConvertToOptions() command.Options {
return command.Options{
Url: b.Url,
Host: b.Host,
Port: b.Port,
User: b.User,
Pass: b.Password,
DbName: b.Database,
Ssl: b.Ssl,
}
}
func readServerConfig(path string) (Bookmark, error) {
bookmark := Bookmark{}
@@ -72,3 +89,16 @@ func ReadAll(path string) (map[string]Bookmark, error) {
return results, nil
}
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{}, fmt.Errorf("couldn't find a bookmark with name %s", bookmarkName)
}
return bookmark, nil
}

View File

@@ -3,6 +3,8 @@ package bookmarks
import (
"testing"
"github.com/sosedoff/pgweb/pkg/command"
"github.com/sosedoff/pgweb/pkg/shared"
"github.com/stretchr/testify/assert"
)
@@ -13,10 +15,6 @@ func Test_Invalid_Bookmark_Files(t *testing.T) {
_, err = readServerConfig("../../data/invalid.toml")
assert.Error(t, err)
assert.Equal(t, "Near line 1, key 'invalid encoding': Near line 2: Expected key separator '=', but got '\\n' instead.", err.Error())
_, err = readServerConfig("../../data/invalid_port.toml")
assert.Error(t, err)
assert.Equal(t, "Type mismatch for 'bookmarks.Bookmark.Port': Expected string but found 'int64'.", err.Error())
}
func Test_Bookmark(t *testing.T) {
@@ -24,7 +22,7 @@ func Test_Bookmark(t *testing.T) {
assert.Equal(t, nil, err)
assert.Equal(t, "localhost", bookmark.Host)
assert.Equal(t, "5432", bookmark.Port)
assert.Equal(t, 5432, bookmark.Port)
assert.Equal(t, "postgres", bookmark.User)
assert.Equal(t, "mydatabase", bookmark.Database)
assert.Equal(t, "disable", bookmark.Ssl)
@@ -38,7 +36,7 @@ func Test_Bookmark_URL(t *testing.T) {
assert.Equal(t, nil, err)
assert.Equal(t, "postgres://username:password@host:port/database?sslmode=disable", bookmark.Url)
assert.Equal(t, "", bookmark.Host)
assert.Equal(t, "", bookmark.Port)
assert.Equal(t, 0, bookmark.Port)
assert.Equal(t, "", bookmark.User)
assert.Equal(t, "", bookmark.Database)
assert.Equal(t, "", bookmark.Ssl)
@@ -69,3 +67,69 @@ func Test_ReadBookmarks(t *testing.T) {
assert.Equal(t, nil, err)
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")
expErrStr := "couldn't find a bookmark with name bar"
assert.Equal(t, expErrStr, err.Error())
_, err = GetBookmark("foo", "bookmark")
assert.Error(t, err)
}
func Test_Bookmark_SSHInfoIsEmpty(t *testing.T) {
emptySSH := shared.SSHInfo{
Host: "",
Port: "",
User: "",
}
populatedSSH := shared.SSHInfo{
Host: "localhost",
Port: "8080",
User: "postgres",
}
b := Bookmark{Ssh: emptySSH}
assert.True(t, b.SSHInfoIsEmpty())
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",
Host: "localhost",
Port: 5432,
User: "postgres",
Password: "password",
Database: "mydatabase",
Ssl: "disable",
}
expOpt := command.Options{
Url: "postgres://username:password@host:port/database?sslmode=disable",
Host: "localhost",
Port: 5432,
User: "postgres",
Pass: "password",
DbName: "mydatabase",
Ssl: "disable",
}
opt := b.ConvertToOptions()
assert.Equal(t, expOpt, opt)
}

View File

@@ -27,6 +27,7 @@ type Options struct {
Prefix string `long:"prefix" description:"Add a url prefix"`
ReadOnly bool `long:"readonly" description:"Run database connection in readonly mode"`
LockSession bool `long:"lock-session" description:"Lock session to a single database connection" default:"false"`
Bookmark string `short:"b" long:"bookmark" description:"Bookmark to use for connection. Bookmark files are stored under $HOME/.pgweb/bookmarks/*.toml" default:""`
}
var Opts Options

View File

@@ -359,7 +359,7 @@ func staticJsAppJs() (*asset, error) {
return nil, err
}
info := bindataFileInfo{name: "static/js/app.js", size: 29571, mode: os.FileMode(420), modTime: time.Unix(1479264852, 0)}
info := bindataFileInfo{name: "static/js/app.js", size: 29571, mode: os.FileMode(420), modTime: time.Unix(1479268217, 0)}
a := &asset{bytes: bytes, info: info}
return a, nil
}