Repository: borgbackup/borgstore

Issue:
sftp URL's username should be optional
As noticed by @bket there: https://github.com/borgbackup/borg/issues/8373



Discussion:

Patch:
diff --git a/src/borgstore/backends/sftp.py b/src/borgstore/backends/sftp.py
index 8da9dbb..1d25e0c 100644
--- a/src/borgstore/backends/sftp.py
+++ b/src/borgstore/backends/sftp.py
@@ -6,6 +6,7 @@ from pathlib import Path
 import random
 import re
 import stat
+from typing import Optional
 
 import paramiko
 
@@ -16,10 +17,10 @@ from ..constants import TMP_SUFFIX
 
 def get_sftp_backend(url):
     # sftp://username@hostname:22/var/backups/borgstore/second
-    # note: must give user, host must be a hostname (not IP), must give path
+    # note: username and port optional, host must be a hostname (not IP), must give path
     sftp_regex = r"""
         sftp://
-        (?P<username>[^@]+)@
+        ((?P<username>[^@]+)@)?
         (?P<hostname>([^:/]+))(?::(?P<port>\d+))?
         (?P<path>(/.*))
     """
@@ -29,7 +30,7 @@ def get_sftp_backend(url):
 
 
 class Sftp(BackendBase):
-    def __init__(self, username: str, hostname: str, path: str, port: int = 22):
+    def __init__(self, hostname: str, path: str, port: int = 22, username: Optional[str] = None):
         self.username = username
         self.hostname = hostname
         self.port = port


Test patch:
diff --git a/tests/test_store.py b/tests/test_store.py
index 41e1eb4..374f2ef 100644
--- a/tests/test_store.py
+++ b/tests/test_store.py
@@ -90,6 +90,7 @@ def test_file_url(tmp_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"),
     ],
 )
 def test_sftp_url(url, username, hostname, port, path):