2018-02-19 04:31:29 -05:00
|
|
|
// Package ssh provides the connection helper for ssh:// URL.
|
|
|
|
package ssh
|
|
|
|
|
|
|
|
import (
|
|
|
|
"net/url"
|
|
|
|
|
|
|
|
"github.com/pkg/errors"
|
|
|
|
)
|
|
|
|
|
2019-03-01 09:34:36 -05:00
|
|
|
// ParseURL parses URL
|
|
|
|
func ParseURL(daemonURL string) (*Spec, error) {
|
2018-02-19 04:31:29 -05:00
|
|
|
u, err := url.Parse(daemonURL)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
if u.Scheme != "ssh" {
|
2018-11-09 02:35:34 -05:00
|
|
|
return nil, errors.Errorf("expected scheme ssh, got %q", u.Scheme)
|
2018-02-19 04:31:29 -05:00
|
|
|
}
|
|
|
|
|
2019-03-01 09:34:36 -05:00
|
|
|
var sp Spec
|
2018-02-19 04:31:29 -05:00
|
|
|
|
|
|
|
if u.User != nil {
|
2019-03-01 09:34:36 -05:00
|
|
|
sp.User = u.User.Username()
|
2018-02-19 04:31:29 -05:00
|
|
|
if _, ok := u.User.Password(); ok {
|
2018-11-09 02:35:34 -05:00
|
|
|
return nil, errors.New("plain-text password is not supported")
|
2018-02-19 04:31:29 -05:00
|
|
|
}
|
|
|
|
}
|
2019-03-01 09:34:36 -05:00
|
|
|
sp.Host = u.Hostname()
|
|
|
|
if sp.Host == "" {
|
2018-11-09 02:35:34 -05:00
|
|
|
return nil, errors.Errorf("no host specified")
|
2018-02-19 04:31:29 -05:00
|
|
|
}
|
2019-03-01 09:34:36 -05:00
|
|
|
sp.Port = u.Port()
|
2023-03-04 07:53:25 -05:00
|
|
|
sp.Path = u.Path
|
2018-02-19 04:31:29 -05:00
|
|
|
if u.RawQuery != "" {
|
2018-11-09 02:35:34 -05:00
|
|
|
return nil, errors.Errorf("extra query after the host: %q", u.RawQuery)
|
2018-02-19 04:31:29 -05:00
|
|
|
}
|
|
|
|
if u.Fragment != "" {
|
2018-11-09 02:35:34 -05:00
|
|
|
return nil, errors.Errorf("extra fragment after the host: %q", u.Fragment)
|
2018-02-19 04:31:29 -05:00
|
|
|
}
|
|
|
|
return &sp, err
|
|
|
|
}
|
|
|
|
|
2019-03-01 09:34:36 -05:00
|
|
|
// Spec of SSH URL
|
|
|
|
type Spec struct {
|
|
|
|
User string
|
|
|
|
Host string
|
|
|
|
Port string
|
2023-03-04 07:53:25 -05:00
|
|
|
Path string
|
2018-02-19 04:31:29 -05:00
|
|
|
}
|
|
|
|
|
2020-05-18 14:12:52 -04:00
|
|
|
// Args returns args except "ssh" itself combined with optional additional command args
|
|
|
|
func (sp *Spec) Args(add ...string) []string {
|
2018-02-19 04:31:29 -05:00
|
|
|
var args []string
|
2019-03-01 09:34:36 -05:00
|
|
|
if sp.User != "" {
|
|
|
|
args = append(args, "-l", sp.User)
|
2018-02-19 04:31:29 -05:00
|
|
|
}
|
2019-03-01 09:34:36 -05:00
|
|
|
if sp.Port != "" {
|
|
|
|
args = append(args, "-p", sp.Port)
|
2018-02-19 04:31:29 -05:00
|
|
|
}
|
2020-05-18 14:12:52 -04:00
|
|
|
args = append(args, "--", sp.Host)
|
|
|
|
args = append(args, add...)
|
2018-02-19 04:31:29 -05:00
|
|
|
return args
|
|
|
|
}
|