cli/connhelper/ssh: add NewSpec utility

This allows creating a spec from an existing url.URL

Signed-off-by: Sebastiaan van Stijn <github@gone.nl>
This commit is contained in:
Sebastiaan van Stijn 2025-02-04 12:04:56 +01:00
parent 55073c404c
commit 11b53dabc6
No known key found for this signature in database
GPG Key ID: 76698F39D527CE8C

View File

@ -19,7 +19,14 @@ func ParseURL(daemonURL string) (*Spec, error) {
}
return nil, fmt.Errorf("invalid SSH URL: %w", err)
}
s, err := newSpec(u)
return NewSpec(u)
}
// NewSpec creates a [Spec] from the given ssh URL's properties. It returns
// an error if the URL is using the wrong scheme, contains fragments,
// query-parameters, or contains a password.
func NewSpec(sshURL *url.URL) (*Spec, error) {
s, err := newSpec(sshURL)
if err != nil {
return nil, fmt.Errorf("invalid SSH URL: %w", err)
}
@ -27,6 +34,9 @@ func ParseURL(daemonURL string) (*Spec, error) {
}
func newSpec(u *url.URL) (*Spec, error) {
if u == nil {
return nil, errors.New("URL is nil")
}
if u.Scheme == "" {
return nil, errors.New("no scheme provided")
}