Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 12 additions & 1 deletion cloudisk/cli/parser.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,13 @@
import typer

from cloudisk.cli.vars import HEADER_ART
from cloudisk.fs.commands import create_space, init_cloudisk_root, link_path, unlink_path
from cloudisk.fs.commands import (
create_space,
init_cloudisk_root,
link_path,
list_spaces,
unlink_path,
)
from cloudisk.http import server
from cloudisk.vars import CLOUDISK_ROOT

Expand Down Expand Up @@ -54,6 +60,11 @@ def create(
create_space(name, protect)


@app.command(help="Lists all created spaces")
def list():
list_spaces()


@app.command(help=f"Creates a symlink inside '{CLOUDISK_ROOT}'")
def link(
path: Annotated[
Expand Down
17 changes: 16 additions & 1 deletion cloudisk/db/models/space.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
from sqlalchemy.exc import IntegrityError
from sqlmodel import Field, Session, SQLModel
from sqlmodel import Field, Session, SQLModel, select

from cloudisk.db.models.base import ModelManager

Expand Down Expand Up @@ -59,3 +59,18 @@ def create(self, name: str, protect: bool) -> SpaceModel:
session.refresh(space)

return space

def list(self) -> list[str]:
"""
List all instances of `SpaceModel`.

Returns
-------
list[str]
The names of the instances.
"""
with Session(self.engine) as session:
statement = select(self.model.name)
results = session.exec(statement)

return results.all()
30 changes: 29 additions & 1 deletion cloudisk/fs/commands.py
Original file line number Diff line number Diff line change
@@ -1,11 +1,13 @@
import os
from pathlib import Path

import typer

from cloudisk.db.models import Space
from cloudisk.fs.utils import ask_remove_dir, ask_remove_path
from cloudisk.logger import get_logger
from cloudisk.tools.settings import Settings
from cloudisk.vars import CLOUDISK_ROOT
from cloudisk.vars import CLOUDISK_DB_FILE, CLOUDISK_ROOT

logger = get_logger("cloudisk.fs")

Expand Down Expand Up @@ -121,3 +123,29 @@ def create_space(name: str, protect: bool) -> None:
Space().create(name=name, protect=protect)

logger.info(f"Created the '{name}' space")


# TODO maybe a space is in the database but not found in ROOT
def list_spaces() -> None:
spaces = Space().list()

if spaces:
typer.echo("Tracked spaces:")
for space in spaces:
typer.echo(f"- {space}")

root = os.listdir(CLOUDISK_ROOT)
root = [x for x in root if x != CLOUDISK_DB_FILE]

if len(root):
untracked = list(filter(lambda x: x not in spaces, root))

message = "Untracked spaces:"
if spaces:
message = "\n" + message

typer.echo(message)
for space in untracked:
typer.echo(f"- {space}")

return
11 changes: 11 additions & 0 deletions tests/db/models/test_space.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,3 +29,14 @@ def test_create_raises_AlreadyExists():

with pytest.raises(Space.AlreadyExists):
manager.create(name="test", protect=True)


def test_list():
manager = Space()

manager.create(name="test", protect=True)

result = manager.list()
execpted = ["test"]

assert result == execpted
37 changes: 36 additions & 1 deletion tests/fs/test_commands.py
Original file line number Diff line number Diff line change
@@ -1,16 +1,18 @@
import os
import shutil
from pathlib import Path
from unittest.mock import patch
from unittest.mock import MagicMock, patch

import pytest

from cloudisk.db.models.space import Space
from cloudisk.fs import commands
from cloudisk.fs.commands import (
_try_link,
create_space,
init_cloudisk_root,
link_path,
list_spaces,
unlink_path,
)

Expand All @@ -25,6 +27,13 @@ def fake_root(tmp_path, monkeypatch) -> Path:
return fake_path


@pytest.fixture
def mock_echo(monkeypatch):
echo_mock = MagicMock()
monkeypatch.setattr("cloudisk.fs.commands.typer.echo", echo_mock)
return echo_mock


def test_init_cloudisk_root_ok(fake_root):
fake_root.rmdir()

Expand Down Expand Up @@ -222,3 +231,29 @@ def test_create_space_ask_remove_dir_is_False(fake_root):
create_space(name=space_name, protect=True)

assert space_path.exists()


def test_list_spaces_full(fake_root):
Space().create(name="test", protect=False)

untracked = fake_root / "untracked"
untracked.mkdir()

list_spaces()


def test_list_spaces_only_tracked(fake_root):
Space().create(name="test", protect=False)

list_spaces()


def test_list_spaces_only_untracked(fake_root):
untracked = fake_root / "untracked"
untracked.mkdir()

list_spaces()


def test_list_space_empty():
list_spaces()