-
Notifications
You must be signed in to change notification settings - Fork 177
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
parse ssh host info from per host string using 'net/url' #143
base: master
Are you sure you want to change the base?
Conversation
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.
Thanks for your contribution. Few questions/suggestions. Looks good otherwise.
ssh.go
Outdated
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 == "" { |
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.
this condition looks suspicious
should this be err == nil && p == ""
?
or err != nil || p == ""
?
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.
oh, It's my mistake! Thanks for your review! :-)
ssh.go
Outdated
host = "ssh://" + host | ||
} | ||
|
||
info, err := url.Parse(host) |
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 u
or url
?
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
, url
or hostURL
if you want to be more specific
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 the same name, "url" and package "net/url". I'll use the "hostURL" :-)
ssh.go
Outdated
c.host = c.host[6:] | ||
c.host = info.Host | ||
if u := info.User.Username(); u != "" { | ||
c.user = u | ||
} |
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.
This is nice refactor, thanks!
ssh.go
Outdated
} | ||
|
||
v := vs[len(vs)-1] | ||
if (v[0] == '\'' && v[len(v)-1] == '\'') || (v[0] == '"' && v[len(v)-1] == '"') { |
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'm not sure what this complex condition is for.
Why don't you strings.Trim()
it without these checks?
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 reason is the difference between '
and "
in shell
example:
ssh://root:123@[email protected]:22/~/.ssh/id_rsa?MY_VAR="aa bb cc dd"&ABC_VAR='help, run: echo $HOME'&XYZ_VAR="my home dir is $HOME"&other_var='abc$xyz'
if no the 'complex condition' and just use strings.Trim()
, the shell variable in c.env will be:
export MY_VAR="aa bb cc dd";
export ABC_VAR="help, run: echo $HOME";
export XYZ_VAR="my home dir is $HOME";
export other_var="abc$xyz";
but, I want:
export MY_VAR="aa bb cc dd";
export ABC_VAR='help, run: echo $HOME';
export XYZ_VAR="my home dir is $HOME";
export other_var='abc$xyz';
the difference:
mac-dev:~ kadefor$ abc='help, run: echo $HOME'
mac-dev:~ kadefor$ echo $abc
help, run: echo $HOME
mac-dev:~ kadefor$ abc="help, run: echo $HOME"
mac-dev:~ kadefor$ echo $abc
help, run: echo /Users/kadefor
mac-dev:~ kadefor$ abc='abc$xyz'
mac-dev:~ kadefor$ echo $abc
abc$xyz
mac-dev:~ kadefor$ abc="abc$xyz"
mac-dev:~ kadefor$ echo $abc
abc
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.
Sorry, I don't understand what ssh://root:123@[email protected]:22/~/.ssh/id_rsa?MY_VAR="aa bb cc dd"&ABC_VAR='help, run: echo $HOME'&XYZ_VAR="my home dir is $HOME"&other_var='abc$xyz'
represents.
We should be parsing pure SSH connection string. Imho, there shouldn't be any path after the [scheme://username:passwd@]host[:port]
.
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 think it is a way to set env for per host, currently. If have a new and better method in the future( save host info to struct in Supfile?), we can disable the feature.
Need to disable it in this PR now? :-)
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 can set env vars via Supfile :)
imho we should just strip off hostUrl.Path
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.
currently, how to set env for per host in Supfile? (about #111)
Could I leave it as an experimental function? If not, remove it :-D
Thank you for your review, again!
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.
Interesting, I don't think sup
supports setting env vars per host right now. It supports setting env vars globally or per network.
Yes please, remove this feature for now. You can create a new issue for it and we'll see if there's any more interest for it out there. I doubt it, though.
We might think about per-host env vars, though. I like it.
Or, I think you could even set those env vars on the server side in the ~/.ssh/authorized_keys file:
command="export VAR=1; /bin/sh" ssh-key
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.
ok, I'll remove it. Thanks for your suggestions! :)
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.
LGTM.
Any volunteers to test this feature?
ssh.go
Outdated
host = "ssh://" + host | ||
} | ||
|
||
hostURL, err := url.Parse(host) |
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 you test this with:
127.0.0.1
localhost
localhost:22
ssh://localhost
etc.?
I know url.Parse(host)
had some issues with localhost URLs.
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.
@VojtechVitek what is the issues?
here is a simple test:
sup kadefor$ cat ssh_test.go
package sup
import (
"fmt"
"testing"
)
func Test_parseHost(t *testing.T) {
hosts := []string{
"127.0.0.1",
"localhost",
"localhost:22",
"ssh://localhost",
"[email protected]",
"tom@localhost",
"tom@localhost:22",
"ssh://tom@localhost",
"tom:11@@[email protected]",
"tom:11@@33@localhost",
"tom:11@@33@localhost:22",
"ssh://tom:11@@33@localhost",
}
for _, host := range hosts {
c := &SSHClient{}
err := c.parseHost(host)
if err != nil {
fmt.Println(err)
} else {
fmt.Printf("%s\n\t%v\n", host, c)
}
}
}
sup kadefor$ go test
127.0.0.1
&{<nil> <nil> kadefor 127.0.0.1:22 <nil> <nil> <nil> false false false export SUP_HOST="127.0.0.1:22"; }
localhost
&{<nil> <nil> kadefor localhost:22 <nil> <nil> <nil> false false false export SUP_HOST="localhost:22"; }
localhost:22
&{<nil> <nil> kadefor localhost:22 <nil> <nil> <nil> false false false export SUP_HOST="localhost:22"; }
ssh://localhost
&{<nil> <nil> kadefor localhost:22 <nil> <nil> <nil> false false false export SUP_HOST="localhost:22"; }
[email protected]
&{<nil> <nil> tom 127.0.0.1:22 <nil> <nil> <nil> false false false export SUP_HOST="127.0.0.1:22"; }
tom@localhost
&{<nil> <nil> tom localhost:22 <nil> <nil> <nil> false false false export SUP_HOST="localhost:22"; }
tom@localhost:22
&{<nil> <nil> tom localhost:22 <nil> <nil> <nil> false false false export SUP_HOST="localhost:22"; }
ssh://tom@localhost
&{<nil> <nil> tom localhost:22 <nil> <nil> <nil> false false false export SUP_HOST="localhost:22"; }
tom:11@@[email protected]
&{<nil> <nil> tom 127.0.0.1:22 <nil> <nil> <nil> false false false export SUP_HOST="127.0.0.1:22"; }
tom:11@@33@localhost
&{<nil> <nil> tom localhost:22 <nil> <nil> <nil> false false false export SUP_HOST="localhost:22"; }
tom:11@@33@localhost:22
&{<nil> <nil> tom localhost:22 <nil> <nil> <nil> false false false export SUP_HOST="localhost:22"; }
ssh://tom:11@@33@localhost
&{<nil> <nil> tom localhost:22 <nil> <nil> <nil> false false false export SUP_HOST="localhost:22"; }
PASS
ok github.com/pressly/sup 0.014s
@VojtechVitek @kadefor any updates on this? thanks. |
This is pending QA. Someone from the community should test this PR and then we can merge. I don't have time to test this out at this time. |
Currently, get some info from host string:
ssh env variable (per host env vars #111)