New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Issue-189 : Allow connecting to bookmarked server from CLI #201
Conversation
We will use this method in upcoming commit to create client from stored bookmark.
Given a bookmark path and bookmark name, GetBookmark returns a Bookmark object that corresponds to the stored bookmark settings. In next commits, we will use this method to read stored bookmarks and create a db client.
We know that a port is a number. Lets enforce that rule at type level by setting it so. This commit also adjusts test funcs and helper data to fit Port's new int type.
We will use client.NewFromURL to create a client from stored bookmark in next commit. Since client.NewFromURL takes in a command.Options, lets add a method on Bookmark to get corresponding Option.
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.
@akarki15 I see that you've changed port handling in the bookmarks. Its a breaking change since any existing bookmarks will error out with |
Also, what happens if i specify invalid bookmark (typo, name mismatch, forgot extension, etc). Right now i get |
@sosedoff that's because the port no. in the toml file should be a number. see the example in data folder |
if the port no. is of type string, the user can still pass a non integer. its just that the error would get caught later when we'd be doing What bookmark settings is giving you |
Regarding the change to int - change itself is fine (although its breaking right now) but in future you should keep the PRs scoped to a single feature. As for the error: i removed all bookmarks from the path and specified |
@@ -72,3 +89,22 @@ func ReadAll(path string) (map[string]Bookmark, error) { | |||
|
|||
return results, nil | |||
} | |||
|
|||
type ErrNonExistingBookmark string |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Do not use this error definition pattern. Its not being used anywhere in the codebase.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
i see your point about not using a new pattern to keep things consistent. i would however like to point the advantage of this pattern - having an explicit typed error means that in the unit tests, i can assert equality on types of error rather than doing a vague string assertion. example here -
assert.Equal(t, ErrNonExistingBookmark("bar"), err)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
68db934 gets rid of this err pattern
} | ||
bookmark, ok := bookmarks[bookmarkName] | ||
if !ok { | ||
return Bookmark{}, ErrNonExistingBookmark(bookmarkName) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
When bookmark does not exist, this line triggers panic. It should really be
return Bookmark{}, fmt.Errorf("bookmark does not exist:", bookmarkName)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
i found the reason for panic. i should have type casted error to string before passing it to sprintf-
func (e ErrNonExistingBookmark) Error() string {
return fmt.Sprintf("couldn't find a bookmark with name %s", string(e))
}
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
68db934 - doesn't panic anymore if you pass non existing file
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
github.com/sosedoff/pgweb Issue-189-take-2 ✔ 3m
▶ ./pgweb --bookmark=dummy
Pgweb v0.9.5
Error: couldn't find a bookmark with name dummy
The codebase doesn't yet use the explicitly typed error pattern. To keep things consistent, lets use the generic error type.
return b.Ssh.User == "" && b.Ssh.Host == "" && b.Ssh.Port == "" | ||
} | ||
|
||
func (b Bookmark) ConvertToOptions() (command.Options, error) { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Whats the point in returning error here ?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
you're right. no point in returning error here. forgot to remove the error when i changed port from string to int
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
ConvertToOptions can't return any error.
Resolves #189
Tested that the feature works -