Skip to content

Commit

Permalink
sftp: prefer values in url over config
Browse files Browse the repository at this point in the history
  • Loading branch information
ThomasWaldmann committed Sep 15, 2024
1 parent cd49707 commit d6c90d6
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 8 deletions.
10 changes: 5 additions & 5 deletions src/borgstore/backends/sftp.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,11 +26,11 @@ def get_sftp_backend(url):
"""
m = re.match(sftp_regex, url, re.VERBOSE)
if m:
return Sftp(username=m["username"], hostname=m["hostname"], port=int(m["port"] or "22"), path=m["path"])
return Sftp(username=m["username"], hostname=m["hostname"], port=int(m["port"] or "0"), path=m["path"])


class Sftp(BackendBase):
def __init__(self, hostname: str, path: str, port: int = 22, username: Optional[str] = None):
def __init__(self, hostname: str, path: str, port: int = 0, username: Optional[str] = None):
self.username = username
self.hostname = hostname
self.port = port
Expand All @@ -55,9 +55,9 @@ def _connect(self):
pass
host_config = ssh_config.lookup(self.hostname)
ssh.connect(
hostname=host_config.get("hostname", self.hostname),
username=host_config.get("user", self.username),
port=host_config.as_int("port") if "port" in host_config else self.port,
hostname=host_config.get("hostname", self.hostname), # prefer config here
username=self.username if self.username else host_config.get("user"),
port=self.port if self.port else (host_config.as_int("port") if "port" in host_config else 22),
key_filename=host_config.get("identityfile", None), # list of keys
allow_agent=True,
)
Expand Down
6 changes: 3 additions & 3 deletions tests/test_backends.py
Original file line number Diff line number Diff line change
Expand Up @@ -106,16 +106,16 @@ def test_file_url(url, path):
"url,username,hostname,port,path",
[
("sftp://username@hostname:2222/some/path", "username", "hostname", 2222, "/some/path"),
("sftp://username@hostname/some/path", "username", "hostname", 22, "/some/path"),
("sftp://hostname/some/path", None, "hostname", 22, "/some/path"),
("sftp://username@hostname/some/path", "username", "hostname", 0, "/some/path"),
("sftp://hostname/some/path", None, "hostname", 0, "/some/path"),
],
)
def test_sftp_url(url, username, hostname, port, path):
backend = get_sftp_backend(url)
assert isinstance(backend, Sftp)
assert backend.username == username
assert backend.hostname == hostname
assert backend.port == port
assert backend.port == port # note: 0 means "not given" (and will usually mean 22 in the end)
assert backend.base_path == path


Expand Down

0 comments on commit d6c90d6

Please sign in to comment.