Chore(clan/clan_uri): Remove ClanURI class from clan_cli

This commit is contained in:
Johannes Kirschbauer
2025-04-23 16:53:11 +02:00
parent 771901f6aa
commit 7076f1b0e6
9 changed files with 9 additions and 9 deletions

View File

@@ -1,61 +0,0 @@
# Import the urllib.parse, enum and dataclasses modules
import urllib.parse
import urllib.request
from dataclasses import dataclass
from pathlib import Path
from clan_cli.flake import Flake
# Define the ClanURI class
@dataclass
class ClanURI:
flake: Flake
machine_name: str
def get_url(self) -> str:
return str(self.flake)
@classmethod
def from_str(
cls,
url: str,
machine_name: str | None = None,
) -> "ClanURI":
uri = url
if machine_name:
uri += f"#{machine_name}"
# Users might copy whitespace along with the URI
uri = uri.strip()
# Check if the URI starts with clan://
# If it does, remove the clan:// prefix
prefix = "clan://"
if uri.startswith(prefix):
uri = uri[len(prefix) :]
# Fix missing colon (caused by browsers like Firefox)
if "//" in uri and ":" not in uri.split("//", 1)[0]:
# If there's a `//` but no colon before it, add one before the `//`
parts = uri.split("//", 1)
uri = f"{parts[0]}://{parts[1]}"
# Parse the URI into components
# url://netloc/path;parameters?query#fragment
components: urllib.parse.ParseResult = urllib.parse.urlparse(uri)
# Replace the query string in the components with the new query string
clean_comps = components._replace(query=components.query, fragment="")
# Parse the URL into a ClanUrl object
if clean_comps.path and Path(clean_comps.path).exists():
flake = Flake(clean_comps.path)
else:
flake = Flake(clean_comps.geturl())
machine_name = "defaultVM"
if components.fragment:
machine_name = components.fragment
return cls(flake, machine_name)

View File

@@ -207,7 +207,7 @@ def complete_secrets(
"""
Provides completion functionality for clan secrets
"""
from .clan_uri import Flake
from . import Flake
from .secrets.secrets import list_secrets
flake = clan_dir_result if (clan_dir_result := clan_dir(None)) is not None else "."

View File

@@ -3,9 +3,8 @@ import logging
import shutil
from pathlib import Path
from clan_cli import inventory
from clan_cli import Flake, inventory
from clan_cli.api import API
from clan_cli.clan_uri import Flake
from clan_cli.completions import add_dynamic_completer, complete_machines
from clan_cli.dirs import specific_machine_dir
from clan_cli.secrets.folders import sops_secrets_folder

View File

@@ -1,99 +0,0 @@
from pathlib import Path
import pytest
from clan_cli.clan_uri import ClanURI
from clan_cli.flake import Flake
from clan_cli.tests.fixtures_flakes import ClanFlake
def test_get_url() -> None:
# Create a ClanURI object from a remote URI with parameters
uri = ClanURI.from_str("clan://https://example.com?password=1234#myVM")
assert uri.get_url() == "https://example.com?password=1234"
uri = ClanURI.from_str("clan://~/Downloads")
assert uri.get_url().endswith("/Downloads")
uri = ClanURI.from_str("clan:///home/user/Downloads")
assert uri.get_url() == "/home/user/Downloads"
uri = ClanURI.from_str("clan://file:///home/user/Downloads")
assert uri.get_url() == "file:///home/user/Downloads"
@pytest.mark.impure
def test_is_local(flake: ClanFlake) -> None:
uri = ClanURI.from_str(f"clan://git+file://{flake.path}")
assert uri.get_url() == str(flake.path)
assert uri.flake.is_local
myflake = Flake(f"git+file://{flake.path}")
assert myflake.is_local
def test_firefox_strip_uri() -> None:
uri = ClanURI.from_str("clan://git+https//git.clan.lol/clan/democlan.git")
assert uri.get_url() == "git+https://git.clan.lol/clan/democlan.git"
def test_local_uri(temp_dir: Path) -> None:
flake_nix = temp_dir / "flake.nix"
flake_nix.write_text("outputs = _: {}")
# Create a ClanURI object from a local URI
uri = ClanURI.from_str(f"clan://file://{temp_dir}")
assert uri.flake.path == temp_dir
def test_is_remote() -> None:
# Create a ClanURI object from a remote URI
uri = ClanURI.from_str("clan://https://example.com")
assert uri.flake.identifier == "https://example.com"
def test_direct_local_path() -> None:
# Create a ClanURI object from a remote URI
uri = ClanURI.from_str("clan://~/Downloads")
assert uri.get_url().endswith("/Downloads")
def test_direct_local_path2() -> None:
# Create a ClanURI object from a remote URI
uri = ClanURI.from_str("clan:///home/user/Downloads")
assert uri.get_url() == "/home/user/Downloads"
def test_remote_with_clanparams() -> None:
# Create a ClanURI object from a remote URI with parameters
uri = ClanURI.from_str("clan://https://example.com")
assert uri.machine_name == "defaultVM"
assert uri.flake.identifier == "https://example.com"
def test_from_str_remote() -> None:
uri = ClanURI.from_str(url="https://example.com", machine_name="myVM")
assert uri.get_url() == "https://example.com"
assert uri.machine_name == "myVM"
assert uri.flake.identifier == "https://example.com"
def test_from_str_local(temp_dir: Path) -> None:
flake_nix = temp_dir / "flake.nix"
flake_nix.write_text("outputs = _: {}")
uri = ClanURI.from_str(url=str(temp_dir), machine_name="myVM")
assert uri.get_url().endswith(str(temp_dir))
assert uri.machine_name == "myVM"
assert uri.flake.is_local
assert str(uri.flake).endswith(str(temp_dir))
def test_from_str_local_no_machine(temp_dir: Path) -> None:
flake_nix = temp_dir / "flake.nix"
flake_nix.write_text("outputs = _: {}")
uri = ClanURI.from_str(str(temp_dir))
assert uri.get_url().endswith(str(temp_dir))
assert uri.machine_name == "defaultVM"
assert uri.flake.is_local
assert str(uri.flake).endswith(str(temp_dir))