chore: import upstream snapshot with attribution
This commit is contained in:
@@ -0,0 +1,60 @@
|
||||
// Package sshurl parses ssh:// URLs into components for use with the ssh binary.
|
||||
package sshurl
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"net"
|
||||
"net/url"
|
||||
)
|
||||
|
||||
// Parsed holds the components of an ssh:// URL.
|
||||
type Parsed struct {
|
||||
Host string // hostname or user@hostname
|
||||
Port string // port number, or "" for default
|
||||
Path string // absolute remote path (starts with /)
|
||||
}
|
||||
|
||||
// SSHArgs returns the ssh command arguments for connecting to this host.
|
||||
// If a port is specified, -p is included.
|
||||
func (p Parsed) SSHArgs() []string {
|
||||
args := []string{"-o", "BatchMode=yes", "-o", "StrictHostKeyChecking=yes", "-o", "ConnectTimeout=5"}
|
||||
if p.Port != "" {
|
||||
args = append(args, "-p", p.Port)
|
||||
}
|
||||
args = append(args, p.Host)
|
||||
return args
|
||||
}
|
||||
|
||||
// Parse parses an ssh:// URL into host, port, and path components.
|
||||
// Accepts formats like ssh://host/path, ssh://user@host/path, ssh://host:2222/path.
|
||||
func Parse(raw string) (Parsed, error) {
|
||||
u, err := url.Parse(raw)
|
||||
if err != nil {
|
||||
return Parsed{}, fmt.Errorf("invalid ssh URL %q: %w", raw, err)
|
||||
}
|
||||
if u.Scheme != "ssh" {
|
||||
return Parsed{}, fmt.Errorf("expected ssh:// scheme, got %q", u.Scheme)
|
||||
}
|
||||
if u.Path == "" {
|
||||
return Parsed{}, fmt.Errorf("ssh URL missing path: %s", raw)
|
||||
}
|
||||
|
||||
host := u.Hostname()
|
||||
if u.User != nil {
|
||||
host = u.User.Username() + "@" + host
|
||||
}
|
||||
|
||||
port := u.Port()
|
||||
// net/url splits host:port correctly; verify with SplitHostPort for edge cases.
|
||||
if port == "" && u.Host != host {
|
||||
if _, p, err := net.SplitHostPort(u.Host); err == nil {
|
||||
port = p
|
||||
}
|
||||
}
|
||||
|
||||
return Parsed{
|
||||
Host: host,
|
||||
Port: port,
|
||||
Path: u.Path,
|
||||
}, nil
|
||||
}
|
||||
@@ -0,0 +1,108 @@
|
||||
package sshurl
|
||||
|
||||
import "testing"
|
||||
|
||||
func TestParse(t *testing.T) {
|
||||
tests := []struct {
|
||||
name string
|
||||
input string
|
||||
wantHost string
|
||||
wantPort string
|
||||
wantPath string
|
||||
wantErr bool
|
||||
}{
|
||||
{
|
||||
name: "basic",
|
||||
input: "ssh://myhost/path/to/music",
|
||||
wantHost: "myhost",
|
||||
wantPort: "",
|
||||
wantPath: "/path/to/music",
|
||||
},
|
||||
{
|
||||
name: "with user",
|
||||
input: "ssh://user@myhost/path/to/music",
|
||||
wantHost: "user@myhost",
|
||||
wantPort: "",
|
||||
wantPath: "/path/to/music",
|
||||
},
|
||||
{
|
||||
name: "with port",
|
||||
input: "ssh://myhost:2222/path/to/music",
|
||||
wantHost: "myhost",
|
||||
wantPort: "2222",
|
||||
wantPath: "/path/to/music",
|
||||
},
|
||||
{
|
||||
name: "with user and port",
|
||||
input: "ssh://user@myhost:2222/path/to/music",
|
||||
wantHost: "user@myhost",
|
||||
wantPort: "2222",
|
||||
wantPath: "/path/to/music",
|
||||
},
|
||||
{
|
||||
name: "wrong scheme",
|
||||
input: "http://myhost/path",
|
||||
wantErr: true,
|
||||
},
|
||||
{
|
||||
name: "missing path",
|
||||
input: "ssh://myhost",
|
||||
wantErr: true,
|
||||
},
|
||||
}
|
||||
for _, tt := range tests {
|
||||
t.Run(tt.name, func(t *testing.T) {
|
||||
parsed, err := Parse(tt.input)
|
||||
if tt.wantErr {
|
||||
if err == nil {
|
||||
t.Fatal("expected error, got nil")
|
||||
}
|
||||
return
|
||||
}
|
||||
if err != nil {
|
||||
t.Fatalf("unexpected error: %v", err)
|
||||
}
|
||||
if parsed.Host != tt.wantHost {
|
||||
t.Errorf("Host = %q, want %q", parsed.Host, tt.wantHost)
|
||||
}
|
||||
if parsed.Port != tt.wantPort {
|
||||
t.Errorf("Port = %q, want %q", parsed.Port, tt.wantPort)
|
||||
}
|
||||
if parsed.Path != tt.wantPath {
|
||||
t.Errorf("Path = %q, want %q", parsed.Path, tt.wantPath)
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
func TestSSHArgs(t *testing.T) {
|
||||
tests := []struct {
|
||||
name string
|
||||
parsed Parsed
|
||||
want []string
|
||||
}{
|
||||
{
|
||||
name: "no port",
|
||||
parsed: Parsed{Host: "myhost", Port: "", Path: "/music"},
|
||||
want: []string{"-o", "BatchMode=yes", "-o", "StrictHostKeyChecking=yes", "-o", "ConnectTimeout=5", "myhost"},
|
||||
},
|
||||
{
|
||||
name: "with port",
|
||||
parsed: Parsed{Host: "user@myhost", Port: "2222", Path: "/music"},
|
||||
want: []string{"-o", "BatchMode=yes", "-o", "StrictHostKeyChecking=yes", "-o", "ConnectTimeout=5", "-p", "2222", "user@myhost"},
|
||||
},
|
||||
}
|
||||
for _, tt := range tests {
|
||||
t.Run(tt.name, func(t *testing.T) {
|
||||
got := tt.parsed.SSHArgs()
|
||||
if len(got) != len(tt.want) {
|
||||
t.Fatalf("SSHArgs() = %v (len %d), want %v (len %d)", got, len(got), tt.want, len(tt.want))
|
||||
}
|
||||
for i := range got {
|
||||
if got[i] != tt.want[i] {
|
||||
t.Errorf("SSHArgs()[%d] = %q, want %q", i, got[i], tt.want[i])
|
||||
}
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user