From 1b4902f1968186f2c187b89979b9f247a5f882b5 Mon Sep 17 00:00:00 2001 From: akarki15 Date: Thu, 10 Nov 2016 01:22:21 -0500 Subject: [PATCH] initClient uses bookmark if it exists if options.Bookmark is set, initClient will now use it to create a client from the bookmarked settings. initClientUsingBookmark uses methods introduced in previous commits. It constructs connection string to db from Bookmark.Url if it exists. If not, it uses other fields in Bookmark to construct the connection string. An implication of this is that the Url field of Bookmark takes precedence. --- main.go | 44 ++++++++++++++++++++++++++++++++++++++---- pkg/command/options.go | 1 + 2 files changed, 41 insertions(+), 4 deletions(-) diff --git a/main.go b/main.go index aeb0ccd..04353f7 100644 --- a/main.go +++ b/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,48 @@ 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, err := bookmark.ConvertToOptions() + if err != nil { + return nil, err + } + 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 { diff --git a/pkg/command/options.go b/pkg/command/options.go index b4a7dfd..da87fbb 100644 --- a/pkg/command/options.go +++ b/pkg/command/options.go @@ -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