Merge pull request #201 from akarki15/Issue-189-take-2
Issue-189 : Allow connecting to bookmarked server from CLI
This commit is contained in:
commit
d74bc06cbd
@ -1,5 +1,5 @@
|
||||
host = "localhost"
|
||||
port = "5432"
|
||||
port = 5432
|
||||
user = "postgres"
|
||||
database = "mydatabase"
|
||||
ssl = "disable"
|
||||
|
@ -1,5 +0,0 @@
|
||||
host = "localhost"
|
||||
port = 5432
|
||||
user = "postgres"
|
||||
database = "mydatabase"
|
||||
ssl = "disable"
|
41
main.go
41
main.go
@ -10,9 +10,11 @@ import (
|
||||
"github.com/jessevdk/go-flags"
|
||||
|
||||
"github.com/sosedoff/pgweb/pkg/api"
|
||||
"github.com/sosedoff/pgweb/pkg/bookmarks"
|
||||
"github.com/sosedoff/pgweb/pkg/client"
|
||||
"github.com/sosedoff/pgweb/pkg/command"
|
||||
"github.com/sosedoff/pgweb/pkg/connection"
|
||||
"github.com/sosedoff/pgweb/pkg/shared"
|
||||
"github.com/sosedoff/pgweb/pkg/util"
|
||||
)
|
||||
|
||||
@ -23,14 +25,45 @@ func exitWithMessage(message string) {
|
||||
os.Exit(1)
|
||||
}
|
||||
|
||||
func initClientUsingBookmark(bookmarkPath, bookmarkName string) (*client.Client, error) {
|
||||
bookmark, err := bookmarks.GetBookmark(bookmarkPath, bookmarkName)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
opt := bookmark.ConvertToOptions()
|
||||
var connStr string
|
||||
if opt.Url != "" { // if the bookmark has url set, use it
|
||||
connStr = opt.Url
|
||||
} else {
|
||||
connStr, err = connection.BuildString(opt)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("error building connection string: %v", err)
|
||||
}
|
||||
}
|
||||
var ssh *shared.SSHInfo
|
||||
if !bookmark.SSHInfoIsEmpty() {
|
||||
ssh = &bookmark.Ssh
|
||||
}
|
||||
return client.NewFromUrl(connStr, ssh)
|
||||
}
|
||||
|
||||
func initClient() {
|
||||
if connection.IsBlank(command.Opts) {
|
||||
if connection.IsBlank(command.Opts) && options.Bookmark == "" {
|
||||
return
|
||||
}
|
||||
|
||||
cl, err := client.New()
|
||||
if err != nil {
|
||||
exitWithMessage(err.Error())
|
||||
var cl *client.Client
|
||||
var err error
|
||||
if options.Bookmark != "" {
|
||||
cl, err = initClientUsingBookmark(bookmarks.Path(), options.Bookmark)
|
||||
if err != nil {
|
||||
exitWithMessage(err.Error())
|
||||
}
|
||||
} else {
|
||||
cl, err = client.New()
|
||||
if err != nil {
|
||||
exitWithMessage(err.Error())
|
||||
}
|
||||
}
|
||||
|
||||
if command.Opts.Debug {
|
||||
|
@ -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
|
||||
|
||||
}
|
||||
|
@ -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)
|
||||
}
|
||||
|
@ -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
|
||||
|
File diff suppressed because one or more lines are too long
Loading…
x
Reference in New Issue
Block a user