From 1512ca07fbd1a7e6ca97caef99ad1754ed077ffe Mon Sep 17 00:00:00 2001 From: Kade Date: Mon, 11 Jun 2018 19:38:57 +0800 Subject: [PATCH 1/9] parse host string with 'net/url' --- ssh.go | 30 ++++++++++++++---------------- 1 file changed, 14 insertions(+), 16 deletions(-) diff --git a/ssh.go b/ssh.go index eb3cefb..e4d315f 100644 --- a/ssh.go +++ b/ssh.go @@ -5,6 +5,7 @@ import ( "io" "io/ioutil" "net" + "net/url" "os" "os/user" "path/filepath" @@ -43,16 +44,22 @@ func (e ErrConnect) Error() string { // parseHost parses and normalizes @ from a given string. func (c *SSHClient) parseHost(host string) error { - c.host = host + if !strings.Contains(host, "://") { + host = "ssh://" + host + } - // Remove extra "ssh://" schema - if len(c.host) > 6 && c.host[:6] == "ssh://" { - c.host = c.host[6:] + info, err := url.Parse(host) + if err != nil { + return err } - if at := strings.Index(c.host, "@"); at != -1 { - c.user = c.host[:at] - c.host = c.host[at+1:] + c.host = info.Host + c.user = info.User.Username() + + // Add default port, if not set + _, p, _ := net.SplitHostPort(info.Host) + if p == "" { + c.host += ":22" } // Add default user, if not set @@ -64,15 +71,6 @@ 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" - } - return nil } From 8118b0986af735137344b02a38d159c324c4de90 Mon Sep 17 00:00:00 2001 From: kadefor Date: Tue, 12 Jun 2018 20:11:30 +0800 Subject: [PATCH 2/9] check err when get port --- ssh.go | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/ssh.go b/ssh.go index e4d315f..99f05e8 100644 --- a/ssh.go +++ b/ssh.go @@ -57,8 +57,7 @@ func (c *SSHClient) parseHost(host string) error { c.user = info.User.Username() // Add default port, if not set - _, p, _ := net.SplitHostPort(info.Host) - if p == "" { + if _, p, err := net.SplitHostPort(info.Host); err != nil && p == "" { c.host += ":22" } From 608c101e1522d9f99ee4874a4d889957588e92d3 Mon Sep 17 00:00:00 2001 From: Kade Date: Wed, 13 Jun 2018 00:39:43 +0800 Subject: [PATCH 3/9] parse env from host string --- ssh.go | 16 ++++++++++++++++ sup.go | 2 +- 2 files changed, 17 insertions(+), 1 deletion(-) diff --git a/ssh.go b/ssh.go index 99f05e8..2af711e 100644 --- a/ssh.go +++ b/ssh.go @@ -70,6 +70,22 @@ func (c *SSHClient) parseHost(host string) error { c.user = u.Username } + 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 } diff --git a/sup.go b/sup.go index d815068..43cddb6 100644 --- a/sup.go +++ b/sup.go @@ -70,7 +70,7 @@ func (sup *Stackup) Run(network *Network, envVars EnvList, commands ...*Command) // SSH client. remote := &SSHClient{ - env: env + `export SUP_HOST="` + host + `";`, + env: env, user: network.User, color: Colors[i%len(Colors)], } From 5884c6059855ebe60f2f2c32e372d307b5621dde Mon Sep 17 00:00:00 2001 From: Kade Date: Wed, 13 Jun 2018 00:53:33 +0800 Subject: [PATCH 4/9] set ssh client's user when user in host string --- ssh.go | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/ssh.go b/ssh.go index 2af711e..5b55f0b 100644 --- a/ssh.go +++ b/ssh.go @@ -54,7 +54,9 @@ func (c *SSHClient) parseHost(host string) error { } c.host = info.Host - c.user = info.User.Username() + if u := info.User.Username(); u != "" { + c.user = u + } // Add default port, if not set if _, p, err := net.SplitHostPort(info.Host); err != nil && p == "" { From 38cad8ece18a60dcaada9f7e2c9979aa8cabdbc2 Mon Sep 17 00:00:00 2001 From: Kade Date: Wed, 13 Jun 2018 22:46:30 +0800 Subject: [PATCH 5/9] fix logic when set default port --- ssh.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/ssh.go b/ssh.go index 5b55f0b..0c279b4 100644 --- a/ssh.go +++ b/ssh.go @@ -59,7 +59,7 @@ func (c *SSHClient) parseHost(host string) error { } // Add default port, if not set - if _, p, err := net.SplitHostPort(info.Host); err != nil && p == "" { + if _, p, err := net.SplitHostPort(info.Host); err == nil && p == "" { c.host += ":22" } From 6d0e7ce320232e97f24840c1dbd5e7746e074d65 Mon Sep 17 00:00:00 2001 From: Kade Date: Wed, 13 Jun 2018 22:59:37 +0800 Subject: [PATCH 6/9] change host 'info' variable name to 'hostURL' --- ssh.go | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/ssh.go b/ssh.go index 0c279b4..d1b6228 100644 --- a/ssh.go +++ b/ssh.go @@ -48,18 +48,18 @@ func (c *SSHClient) parseHost(host string) error { host = "ssh://" + host } - info, err := url.Parse(host) + hostURL, err := url.Parse(host) if err != nil { return err } - c.host = info.Host - if u := info.User.Username(); u != "" { + c.host = hostURL.Host + if u := hostURL.User.Username(); u != "" { c.user = u } // Add default port, if not set - if _, p, err := net.SplitHostPort(info.Host); err == nil && p == "" { + if _, p, err := net.SplitHostPort(hostURL.Host); err == nil && p == "" { c.host += ":22" } @@ -73,7 +73,7 @@ func (c *SSHClient) parseHost(host string) error { } c.env = c.env + `export SUP_HOST="` + c.host + `";` - if m, _ := url.ParseQuery(info.RawQuery); len(m) > 0 { + if m, _ := url.ParseQuery(hostURL.RawQuery); len(m) > 0 { for k, vs := range m { if len(vs) == 0 || vs[len(vs)-1] == "" { continue From 18fe97facaad824c635795052c493f37b448c7bb Mon Sep 17 00:00:00 2001 From: Kade Date: Wed, 13 Jun 2018 23:15:45 +0800 Subject: [PATCH 7/9] disable parse env from host url --- ssh.go | 14 -------------- 1 file changed, 14 deletions(-) diff --git a/ssh.go b/ssh.go index d1b6228..0471074 100644 --- a/ssh.go +++ b/ssh.go @@ -73,20 +73,6 @@ func (c *SSHClient) parseHost(host string) error { } c.env = c.env + `export SUP_HOST="` + c.host + `";` - if m, _ := url.ParseQuery(hostURL.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 } From 5ad14106c1c19e9434cc7fb53148617869b8862d Mon Sep 17 00:00:00 2001 From: kadefor Date: Fri, 15 Jun 2018 14:31:49 +0800 Subject: [PATCH 8/9] refactor host url parse --- ssh.go | 18 +++++++++++------- 1 file changed, 11 insertions(+), 7 deletions(-) diff --git a/ssh.go b/ssh.go index 0471074..89626da 100644 --- a/ssh.go +++ b/ssh.go @@ -44,7 +44,9 @@ func (e ErrConnect) Error() string { // parseHost parses and normalizes @ from a given string. func (c *SSHClient) parseHost(host string) error { - if !strings.Contains(host, "://") { + // https://golang.org/pkg/net/url/#URL + // [scheme:][//[userinfo@]host][/]path[?query][#fragment] + if !strings.Contains(host, "://") && !strings.HasPrefix(host, "//") { host = "ssh://" + host } @@ -53,14 +55,16 @@ func (c *SSHClient) parseHost(host string) error { return err } - c.host = hostURL.Host - if u := hostURL.User.Username(); u != "" { - c.user = u + // Add default port, if not set + hostname := hostURL.Hostname() + port := hostURL.Port() + if port == "" { + port = "22" } + c.host = net.JoinHostPort(hostname, port) - // Add default port, if not set - if _, p, err := net.SplitHostPort(hostURL.Host); err == nil && p == "" { - c.host += ":22" + if u := hostURL.User.Username(); u != "" { + c.user = u } // Add default user, if not set From bf2dc98859817a8401eee8206024950762e5ac3a Mon Sep 17 00:00:00 2001 From: Kade Date: Tue, 19 Jun 2018 01:50:30 +0800 Subject: [PATCH 9/9] fix a bug when get user from host url --- ssh.go | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/ssh.go b/ssh.go index 89626da..7328f6f 100644 --- a/ssh.go +++ b/ssh.go @@ -63,8 +63,10 @@ func (c *SSHClient) parseHost(host string) error { } c.host = net.JoinHostPort(hostname, port) - if u := hostURL.User.Username(); u != "" { - c.user = u + if hostURL.User != nil { + if u := hostURL.User.Username(); u != "" { + c.user = u + } } // Add default user, if not set