-
Notifications
You must be signed in to change notification settings - Fork 181
parse ssh host info from per host string using 'net/url' #143
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
base: master
Are you sure you want to change the base?
Changes from 4 commits
1512ca0
8118b09
608c101
5884c60
38cad8e
6d0e7ce
18fe97f
5ad1410
bf2dc98
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -5,6 +5,7 @@ import ( | |
| "io" | ||
| "io/ioutil" | ||
| "net" | ||
| "net/url" | ||
| "os" | ||
| "os/user" | ||
| "path/filepath" | ||
|
|
@@ -43,16 +44,23 @@ func (e ErrConnect) Error() string { | |
|
|
||
| // parseHost parses and normalizes <user>@<host:port> from a given string. | ||
| func (c *SSHClient) parseHost(host string) error { | ||
| c.host = host | ||
| if !strings.Contains(host, "://") { | ||
| host = "ssh://" + host | ||
| } | ||
|
|
||
| info, err := url.Parse(host) | ||
| if err != nil { | ||
| return err | ||
| } | ||
|
|
||
| // Remove extra "ssh://" schema | ||
| if len(c.host) > 6 && c.host[:6] == "ssh://" { | ||
| c.host = c.host[6:] | ||
| c.host = info.Host | ||
| if u := info.User.Username(); u != "" { | ||
| c.user = u | ||
| } | ||
|
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This is nice refactor, thanks! |
||
|
|
||
| if at := strings.Index(c.host, "@"); at != -1 { | ||
| c.user = c.host[:at] | ||
| c.host = c.host[at+1:] | ||
| // Add default port, if not set | ||
| if _, p, err := net.SplitHostPort(info.Host); err != nil && p == "" { | ||
|
||
| c.host += ":22" | ||
| } | ||
|
|
||
| // Add default user, if not set | ||
|
|
@@ -64,13 +72,20 @@ func (c *SSHClient) parseHost(host string) error { | |
| c.user = u.Username | ||
| } | ||
|
|
||
| if strings.Index(c.host, "/") != -1 { | ||
| return ErrConnect{c.user, c.host, "unexpected slash in the host URL"} | ||
| } | ||
|
|
||
| // Add default port, if not set | ||
| if strings.Index(c.host, ":") == -1 { | ||
| c.host += ":22" | ||
| c.env = c.env + `export SUP_HOST="` + c.host + `";` | ||
| if m, _ := url.ParseQuery(info.RawQuery); len(m) > 0 { | ||
| for k, vs := range m { | ||
| if len(vs) == 0 || vs[len(vs)-1] == "" { | ||
| continue | ||
| } | ||
|
|
||
| v := vs[len(vs)-1] | ||
| if (v[0] == '\'' && v[len(v)-1] == '\'') || (v[0] == '"' && v[len(v)-1] == '"') { | ||
|
||
| c.env = c.env + fmt.Sprintf(`export %s=%s; `, k, vs[len(vs)-1]) | ||
| } else { | ||
| c.env = c.env + fmt.Sprintf(`export %s="%s"; `, k, strings.Trim(vs[len(vs)-1], `'"`)) | ||
| } | ||
| } | ||
| } | ||
|
|
||
| return nil | ||
|
|
||
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.
can we call this
uorurl?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.
the object is about host info, can we change the variable name to "h" or "hostInfo"?
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.
it's host URL :) I prefer
u,urlorhostURLif you want to be more specificThere 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.
it's the same name, "url" and package "net/url". I'll use the "hostURL" :-)